Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #21000 -- Made cached_db session backend respect SESSION_CACHE_…

…ALIAS
  • Loading branch information...
commit ed9cd4fd8b425ab5010b27aefc7ef6e50d55fb54 1 parent a2374bc
CHI Cheng authored timgraham committed
View
14 django/contrib/sessions/backends/cached_db.py
@@ -4,8 +4,9 @@
import logging
+from django.conf import settings
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.utils import timezone
from django.utils.encoding import force_text
@@ -19,6 +20,7 @@ class SessionStore(DBStore):
"""
def __init__(self, session_key=None):
+ self._cache = get_cache(settings.SESSION_CACHE_ALIAS)
super(SessionStore, self).__init__(session_key)
@property
@@ -27,7 +29,7 @@ def cache_key(self):
def load(self):
try:
- data = cache.get(self.cache_key, None)
+ data = self._cache.get(self.cache_key, None)
except Exception:
# Some backends (e.g. memcache) raise an exception on invalid
# cache keys. If this happens, reset the session. See #17810.
@@ -42,7 +44,7 @@ def load(self):
expire_date__gt=timezone.now()
)
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))
except (Session.DoesNotExist, SuspiciousOperation) as e:
if isinstance(e, SuspiciousOperation):
@@ -54,13 +56,13 @@ def load(self):
return data
def exists(self, session_key):
- if (KEY_PREFIX + session_key) in cache:
+ if (KEY_PREFIX + session_key) in self._cache:
return True
return super(SessionStore, self).exists(session_key)
def save(self, must_create=False):
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):
super(SessionStore, self).delete(session_key)
@@ -68,7 +70,7 @@ def delete(self, session_key=None):
if self.session_key is None:
return
session_key = self.session_key
- cache.delete(KEY_PREFIX + session_key)
+ self._cache.delete(KEY_PREFIX + session_key)
def flush(self):
"""
View
6 django/contrib/sessions/tests.py
@@ -16,6 +16,7 @@
from django.contrib.sessions.models import Session
from django.contrib.sessions.middleware import SessionMiddleware
from django.core.cache import get_cache
+from django.core.cache.backends.base import InvalidCacheBackendError
from django.core import management
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse
@@ -386,6 +387,11 @@ def test_load_overlong_key(self):
self.session._session_key = (string.ascii_letters + string.digits) * 20
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)
class CacheDBSessionWithTimeZoneTests(CacheDBSessionTests):
View
7 docs/releases/1.7.txt
@@ -154,6 +154,13 @@ Minor features
follow the :setting:`SESSION_COOKIE_SECURE` and
: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`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
View
5 docs/topics/http/sessions.txt
@@ -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
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
-------------------------

2 comments on commit ed9cd4f

@nuklea

Backport that to 1.5, please!

@valkolovos

+1 on backport!

Please sign in to comment.
Something went wrong with that request. Please try again.