2009-01-11 06:18:14 +08:00
|
|
|
"""
|
|
|
|
Cached, database-backed sessions.
|
|
|
|
"""
|
|
|
|
|
2013-08-30 12:03:23 +08:00
|
|
|
from django.conf import settings
|
2009-01-11 06:18:14 +08:00
|
|
|
from django.contrib.sessions.backends.db import SessionStore as DBStore
|
2013-10-19 06:49:24 +08:00
|
|
|
from django.core.cache import caches
|
2009-01-11 06:18:14 +08:00
|
|
|
|
2011-09-10 08:46:48 +08:00
|
|
|
KEY_PREFIX = "django.contrib.sessions.cached_db"
|
|
|
|
|
2012-03-23 17:32:11 +08:00
|
|
|
|
2009-01-11 06:18:14 +08:00
|
|
|
class SessionStore(DBStore):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Implement cached, database backed sessions.
|
2009-01-11 06:18:14 +08:00
|
|
|
"""
|
2014-05-17 00:18:34 +08:00
|
|
|
cache_key_prefix = KEY_PREFIX
|
2009-01-11 06:18: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)
|
2009-01-11 06:18:14 +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
|
|
|
|
2009-01-11 06:18:14 +08:00
|
|
|
def load(self):
|
2012-03-23 13:31:11 +08:00
|
|
|
try:
|
2015-05-14 02:51:18 +08:00
|
|
|
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
|
|
|
data = None
|
2012-10-28 01:18:03 +08:00
|
|
|
|
2009-01-11 06:18:14 +08:00
|
|
|
if data is None:
|
2017-12-08 23:51:16 +08:00
|
|
|
s = self._get_session_from_db()
|
|
|
|
if s:
|
2012-10-28 01:18:03 +08:00
|
|
|
data = self.decode(s.session_data)
|
2016-03-29 06:33:29 +08:00
|
|
|
self._cache.set(self.cache_key, data, self.get_expiry_age(expiry=s.expire_date))
|
2017-12-08 23:51:16 +08:00
|
|
|
else:
|
2012-10-28 01:18:03 +08:00
|
|
|
data = {}
|
2009-01-11 06:18:14 +08:00
|
|
|
return data
|
|
|
|
|
|
|
|
def exists(self, session_key):
|
2018-01-04 07:52:12 +08:00
|
|
|
return session_key and (self.cache_key_prefix + session_key) in self._cache or super().exists(session_key)
|
2009-01-11 06:18:14 +08:00
|
|
|
|
|
|
|
def save(self, must_create=False):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().save(must_create)
|
2013-08-30 12:03:23 +08:00
|
|
|
self._cache.set(self.cache_key, self._session, self.get_expiry_age())
|
2009-01-11 06:18:14 +08:00
|
|
|
|
|
|
|
def delete(self, session_key=None):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().delete(session_key)
|
2011-11-28 01:52:24 +08:00
|
|
|
if session_key is None:
|
|
|
|
if self.session_key is None:
|
|
|
|
return
|
|
|
|
session_key = self.session_key
|
2014-05-17 00:18:34 +08:00
|
|
|
self._cache.delete(self.cache_key_prefix + session_key)
|
2009-01-11 06:18:14 +08:00
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Remove the current session data from the database and regenerate the
|
2009-01-11 06:18:14 +08:00
|
|
|
key.
|
|
|
|
"""
|
|
|
|
self.clear()
|
|
|
|
self.delete(self.session_key)
|
2015-05-11 21:58:43 +08:00
|
|
|
self._session_key = None
|