PageRenderTime 112ms CodeModel.GetById 70ms app.highlight 4ms RepoModel.GetById 37ms app.codeStats 0ms

/django/contrib/sessions/backends/cached_db.py

https://code.google.com/p/mango-py/
Python | 46 lines | 27 code | 9 blank | 10 comment | 2 complexity | 9dc87669641de61a5bae2279ba91793e MD5 | raw file
 1"""
 2Cached, database-backed sessions.
 3"""
 4
 5from django.conf import settings
 6from django.contrib.sessions.backends.db import SessionStore as DBStore
 7from django.core.cache import cache
 8
 9KEY_PREFIX = "django.contrib.sessions.cached_db"
10
11class SessionStore(DBStore):
12    """
13    Implements cached, database backed sessions.
14    """
15
16    def __init__(self, session_key=None):
17        super(SessionStore, self).__init__(session_key)
18
19    def load(self):
20        data = cache.get(KEY_PREFIX + self.session_key, None)
21        if data is None:
22            data = super(SessionStore, self).load()
23            cache.set(KEY_PREFIX + self.session_key, data, 
24                      settings.SESSION_COOKIE_AGE)
25        return data
26
27    def exists(self, session_key):
28        return super(SessionStore, self).exists(session_key)
29
30    def save(self, must_create=False):
31        super(SessionStore, self).save(must_create)
32        cache.set(KEY_PREFIX + self.session_key, self._session, 
33                  settings.SESSION_COOKIE_AGE)
34
35    def delete(self, session_key=None):
36        super(SessionStore, self).delete(session_key)
37        cache.delete(KEY_PREFIX + (session_key or self.session_key))
38
39    def flush(self):
40        """
41        Removes the current session data from the database and regenerates the
42        key.
43        """
44        self.clear()
45        self.delete(self.session_key)
46        self.create()