Browse Source

Fixed #21000 -- Made cached_db session backend respect SESSION_CACHE_ALIAS

CHI Cheng 11 years ago
parent
commit
ed9cd4fd8b

+ 8 - 6
django/contrib/sessions/backends/cached_db.py

@@ -4,8 +4,9 @@ Cached, database-backed sessions.
 
 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 @@ class SessionStore(DBStore):
 
     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 @@ class SessionStore(DBStore):
                     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 @@ class SessionStore(DBStore):
         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 @@ class SessionStore(DBStore):
             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):
         """

+ 6 - 0
django/contrib/sessions/tests.py

@@ -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.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 @@ class CacheDBSessionTests(SessionTestsMixin, TestCase):
             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):

+ 7 - 0
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`
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 

+ 5 - 0
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
 -------------------------