2012-10-31 04:59:23 +08:00
|
|
|
from django.conf import settings
|
2016-01-09 00:08:08 +08:00
|
|
|
from django.contrib.sessions.backends.base import (
|
|
|
|
CreateError, SessionBase, UpdateError,
|
|
|
|
)
|
2013-10-19 06:49:24 +08:00
|
|
|
from django.core.cache import caches
|
2007-09-16 05:29:14 +08:00
|
|
|
|
2011-09-10 08:46:48 +08:00
|
|
|
KEY_PREFIX = "django.contrib.sessions.cache"
|
|
|
|
|
2012-03-23 17:32:11 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
class SessionStore(SessionBase):
|
|
|
|
"""
|
2008-06-08 04:28:06 +08:00
|
|
|
A cache-based session store.
|
2007-09-16 05:29:14 +08:00
|
|
|
"""
|
2014-05-17 00:18:34 +08:00
|
|
|
cache_key_prefix = KEY_PREFIX
|
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
def __init__(self, session_key=None):
|
2013-10-19 06:49:24 +08:00
|
|
|
self._cache = caches[settings.SESSION_CACHE_ALIAS]
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(session_key)
|
2008-06-08 04:28:06 +08:00
|
|
|
|
2011-11-28 01:52:24 +08:00
|
|
|
@property
|
|
|
|
def cache_key(self):
|
2014-05-17 00:18:34 +08:00
|
|
|
return self.cache_key_prefix + self._get_or_create_session_key()
|
2011-11-28 01:52:24 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
def load(self):
|
2012-03-23 13:31:11 +08:00
|
|
|
try:
|
2015-05-14 02:51:18 +08:00
|
|
|
session_data = self._cache.get(self.cache_key)
|
2012-03-24 00:14:46 +08:00
|
|
|
except Exception:
|
|
|
|
# Some backends (e.g. memcache) raise an exception on invalid
|
|
|
|
# cache keys. If this happens, reset the session. See #17810.
|
2012-03-23 13:31:11 +08:00
|
|
|
session_data = None
|
2008-08-14 11:57:18 +08:00
|
|
|
if session_data is not None:
|
|
|
|
return session_data
|
2015-06-11 05:45:20 +08:00
|
|
|
self._session_key = None
|
2008-08-16 23:54:36 +08:00
|
|
|
return {}
|
2007-09-16 05:29:14 +08:00
|
|
|
|
2008-08-14 11:57:18 +08:00
|
|
|
def create(self):
|
2008-08-27 16:58:51 +08:00
|
|
|
# Because a cache can fail silently (e.g. memcache), we don't know if
|
|
|
|
# we are failing to create a new session because of a key collision or
|
|
|
|
# because the cache is missing. So we try for a (large) number of times
|
|
|
|
# and then raise an exception. That's the risk you shoulder if using
|
|
|
|
# cache backing.
|
2014-12-13 21:04:36 +08:00
|
|
|
for i in range(10000):
|
2011-11-28 01:52:24 +08:00
|
|
|
self._session_key = self._get_new_session_key()
|
2008-08-14 11:57:18 +08:00
|
|
|
try:
|
|
|
|
self.save(must_create=True)
|
|
|
|
except CreateError:
|
|
|
|
continue
|
|
|
|
self.modified = True
|
|
|
|
return
|
2012-10-28 19:40:10 +08:00
|
|
|
raise RuntimeError(
|
|
|
|
"Unable to create a new session key. "
|
|
|
|
"It is likely that the cache is unavailable.")
|
2008-08-14 11:57:18 +08:00
|
|
|
|
|
|
|
def save(self, must_create=False):
|
2015-06-11 05:45:20 +08:00
|
|
|
if self.session_key is None:
|
|
|
|
return self.create()
|
2008-08-14 11:57:18 +08:00
|
|
|
if must_create:
|
|
|
|
func = self._cache.add
|
2016-04-04 06:09:10 +08:00
|
|
|
elif self._cache.get(self.cache_key) is not None:
|
2008-08-14 11:57:18 +08:00
|
|
|
func = self._cache.set
|
2016-01-09 00:08:08 +08:00
|
|
|
else:
|
|
|
|
raise UpdateError
|
2011-11-28 01:52:24 +08:00
|
|
|
result = func(self.cache_key,
|
|
|
|
self._get_session(no_load=must_create),
|
|
|
|
self.get_expiry_age())
|
2008-08-14 11:57:18 +08:00
|
|
|
if must_create and not result:
|
|
|
|
raise CreateError
|
2007-09-16 05:29:14 +08:00
|
|
|
|
|
|
|
def exists(self, session_key):
|
2017-05-03 22:09:28 +08:00
|
|
|
return bool(session_key) and (self.cache_key_prefix + session_key) in self._cache
|
2008-06-08 04:28:06 +08:00
|
|
|
|
2008-08-14 11:57:46 +08:00
|
|
|
def delete(self, session_key=None):
|
|
|
|
if session_key is None:
|
2011-11-28 01:52:24 +08:00
|
|
|
if self.session_key is None:
|
2008-08-15 22:59:11 +08:00
|
|
|
return
|
2011-11-28 01:52:24 +08:00
|
|
|
session_key = self.session_key
|
2014-05-17 00:18:34 +08:00
|
|
|
self._cache.delete(self.cache_key_prefix + session_key)
|
2012-10-28 05:12:08 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def clear_expired(cls):
|
|
|
|
pass
|