1 from django
.contrib
.sessions
.backends
.base
import SessionBase
, CreateError
2 from django
.core
.cache
import cache
4 class SessionStore(SessionBase
):
6 A cache-based session store.
8 def __init__(self
, session_key
=None):
10 super(SessionStore
, self
).__init
__(session_key
)
13 session_data
= self
._cache
.get(self
.session_key
)
14 if session_data
is not None:
20 # Because a cache can fail silently (e.g. memcache), we don't know if
21 # we are failing to create a new session because of a key collision or
22 # because the cache is missing. So we try for a (large) number of times
23 # and then raise an exception. That's the risk you shoulder if using
25 for i
in xrange(10000):
26 self
.session_key
= self
._get
_new
_session
_key
()
28 self
.save(must_create
=True)
33 raise RuntimeError("Unable to create a new session key.")
35 def save(self
, must_create
=False):
37 func
= self
._cache
.add
39 func
= self
._cache
.set
40 result
= func(self
.session_key
, self
._get
_session
(no_load
=must_create
),
41 self
.get_expiry_age())
42 if must_create
and not result
:
45 def exists(self
, session_key
):
46 if self
._cache
.get(session_key
):
50 def delete(self
, session_key
=None):
51 if session_key
is None:
52 if self
._session
_key
is None:
54 session_key
= self
._session
_key
55 self
._cache
.delete(session_key
)