Fixed #21000 -- Made cached_db session backend respect SESSION_CACHE_ALIAS
This commit is contained in:
parent
a2374bcf47
commit
ed9cd4fd8b
|
@ -4,8 +4,9 @@ Cached, database-backed sessions.
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
from django.contrib.sessions.backends.db import SessionStore as DBStore
|
from django.contrib.sessions.backends.db import SessionStore as DBStore
|
||||||
from django.core.cache import cache
|
from django.core.cache import get_cache
|
||||||
from django.core.exceptions import SuspiciousOperation
|
from django.core.exceptions import SuspiciousOperation
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
|
@ -19,6 +20,7 @@ class SessionStore(DBStore):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, session_key=None):
|
def __init__(self, session_key=None):
|
||||||
|
self._cache = get_cache(settings.SESSION_CACHE_ALIAS)
|
||||||
super(SessionStore, self).__init__(session_key)
|
super(SessionStore, self).__init__(session_key)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -27,7 +29,7 @@ class SessionStore(DBStore):
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
try:
|
try:
|
||||||
data = cache.get(self.cache_key, None)
|
data = self._cache.get(self.cache_key, None)
|
||||||
except Exception:
|
except Exception:
|
||||||
# Some backends (e.g. memcache) raise an exception on invalid
|
# Some backends (e.g. memcache) raise an exception on invalid
|
||||||
# cache keys. If this happens, reset the session. See #17810.
|
# cache keys. If this happens, reset the session. See #17810.
|
||||||
|
@ -42,7 +44,7 @@ class SessionStore(DBStore):
|
||||||
expire_date__gt=timezone.now()
|
expire_date__gt=timezone.now()
|
||||||
)
|
)
|
||||||
data = self.decode(s.session_data)
|
data = self.decode(s.session_data)
|
||||||
cache.set(self.cache_key, data,
|
self._cache.set(self.cache_key, data,
|
||||||
self.get_expiry_age(expiry=s.expire_date))
|
self.get_expiry_age(expiry=s.expire_date))
|
||||||
except (Session.DoesNotExist, SuspiciousOperation) as e:
|
except (Session.DoesNotExist, SuspiciousOperation) as e:
|
||||||
if isinstance(e, SuspiciousOperation):
|
if isinstance(e, SuspiciousOperation):
|
||||||
|
@ -54,13 +56,13 @@ class SessionStore(DBStore):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def exists(self, session_key):
|
def exists(self, session_key):
|
||||||
if (KEY_PREFIX + session_key) in cache:
|
if (KEY_PREFIX + session_key) in self._cache:
|
||||||
return True
|
return True
|
||||||
return super(SessionStore, self).exists(session_key)
|
return super(SessionStore, self).exists(session_key)
|
||||||
|
|
||||||
def save(self, must_create=False):
|
def save(self, must_create=False):
|
||||||
super(SessionStore, self).save(must_create)
|
super(SessionStore, self).save(must_create)
|
||||||
cache.set(self.cache_key, self._session, self.get_expiry_age())
|
self._cache.set(self.cache_key, self._session, self.get_expiry_age())
|
||||||
|
|
||||||
def delete(self, session_key=None):
|
def delete(self, session_key=None):
|
||||||
super(SessionStore, self).delete(session_key)
|
super(SessionStore, self).delete(session_key)
|
||||||
|
@ -68,7 +70,7 @@ class SessionStore(DBStore):
|
||||||
if self.session_key is None:
|
if self.session_key is None:
|
||||||
return
|
return
|
||||||
session_key = self.session_key
|
session_key = self.session_key
|
||||||
cache.delete(KEY_PREFIX + session_key)
|
self._cache.delete(KEY_PREFIX + session_key)
|
||||||
|
|
||||||
def flush(self):
|
def flush(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -16,6 +16,7 @@ from django.contrib.sessions.backends.signed_cookies import SessionStore as Cook
|
||||||
from django.contrib.sessions.models import Session
|
from django.contrib.sessions.models import Session
|
||||||
from django.contrib.sessions.middleware import SessionMiddleware
|
from django.contrib.sessions.middleware import SessionMiddleware
|
||||||
from django.core.cache import get_cache
|
from django.core.cache import get_cache
|
||||||
|
from django.core.cache.backends.base import InvalidCacheBackendError
|
||||||
from django.core import management
|
from django.core import management
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
@ -386,6 +387,11 @@ class CacheDBSessionTests(SessionTestsMixin, TestCase):
|
||||||
self.session._session_key = (string.ascii_letters + string.digits) * 20
|
self.session._session_key = (string.ascii_letters + string.digits) * 20
|
||||||
self.assertEqual(self.session.load(), {})
|
self.assertEqual(self.session.load(), {})
|
||||||
|
|
||||||
|
@override_settings(SESSION_CACHE_ALIAS='sessions')
|
||||||
|
def test_non_default_cache(self):
|
||||||
|
#21000 - CacheDB backend should respect SESSION_CACHE_ALIAS.
|
||||||
|
self.assertRaises(InvalidCacheBackendError, self.backend)
|
||||||
|
|
||||||
|
|
||||||
@override_settings(USE_TZ=True)
|
@override_settings(USE_TZ=True)
|
||||||
class CacheDBSessionWithTimeZoneTests(CacheDBSessionTests):
|
class CacheDBSessionWithTimeZoneTests(CacheDBSessionTests):
|
||||||
|
|
|
@ -154,6 +154,13 @@ Minor features
|
||||||
follow the :setting:`SESSION_COOKIE_SECURE` and
|
follow the :setting:`SESSION_COOKIE_SECURE` and
|
||||||
:setting:`SESSION_COOKIE_HTTPONLY` settings.
|
:setting:`SESSION_COOKIE_HTTPONLY` settings.
|
||||||
|
|
||||||
|
:mod:`django.contrib.sessions`
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
* The ``"django.contrib.sessions.backends.cached_db"`` session backend now
|
||||||
|
respects :setting:`SESSION_CACHE_ALIAS`. In previous versions, it always used
|
||||||
|
the `default` cache.
|
||||||
|
|
||||||
:mod:`django.contrib.sitemaps`
|
:mod:`django.contrib.sitemaps`
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
|
|
@ -93,6 +93,11 @@ session data be expunged from time to time, the ``cache`` backend is for you.
|
||||||
If you use the ``cached_db`` session backend, you also need to follow the
|
If you use the ``cached_db`` session backend, you also need to follow the
|
||||||
configuration instructions for the `using database-backed sessions`_.
|
configuration instructions for the `using database-backed sessions`_.
|
||||||
|
|
||||||
|
.. versionchanged:: 1.7
|
||||||
|
|
||||||
|
Before version 1.7, the ``cached_db`` backend always used the ``default`` cache
|
||||||
|
rather than the :setting:`SESSION_CACHE_ALIAS`.
|
||||||
|
|
||||||
Using file-based sessions
|
Using file-based sessions
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue