Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…ALIAS
  • Loading branch information...
commit ed9cd4fd8b425ab5010b27aefc7ef6e50d55fb54 1 parent a2374bc
authored August 30, 2013 timgraham committed September 05, 2013
14  django/contrib/sessions/backends/cached_db.py
@@ -4,8 +4,9 @@
4 4
 
5 5
 import logging
6 6
 
  7
+from django.conf import settings
7 8
 from django.contrib.sessions.backends.db import SessionStore as DBStore
8  
-from django.core.cache import cache
  9
+from django.core.cache import get_cache
9 10
 from django.core.exceptions import SuspiciousOperation
10 11
 from django.utils import timezone
11 12
 from django.utils.encoding import force_text
@@ -19,6 +20,7 @@ class SessionStore(DBStore):
19 20
     """
20 21
 
21 22
     def __init__(self, session_key=None):
  23
+        self._cache = get_cache(settings.SESSION_CACHE_ALIAS)
22 24
         super(SessionStore, self).__init__(session_key)
23 25
 
24 26
     @property
@@ -27,7 +29,7 @@ def cache_key(self):
27 29
 
28 30
     def load(self):
29 31
         try:
30  
-            data = cache.get(self.cache_key, None)
  32
+            data = self._cache.get(self.cache_key, None)
31 33
         except Exception:
32 34
             # Some backends (e.g. memcache) raise an exception on invalid
33 35
             # cache keys. If this happens, reset the session. See #17810.
@@ -42,7 +44,7 @@ def load(self):
42 44
                     expire_date__gt=timezone.now()
43 45
                 )
44 46
                 data = self.decode(s.session_data)
45  
-                cache.set(self.cache_key, data,
  47
+                self._cache.set(self.cache_key, data,
46 48
                     self.get_expiry_age(expiry=s.expire_date))
47 49
             except (Session.DoesNotExist, SuspiciousOperation) as e:
48 50
                 if isinstance(e, SuspiciousOperation):
@@ -54,13 +56,13 @@ def load(self):
54 56
         return data
55 57
 
56 58
     def exists(self, session_key):
57  
-        if (KEY_PREFIX + session_key) in cache:
  59
+        if (KEY_PREFIX + session_key) in self._cache:
58 60
             return True
59 61
         return super(SessionStore, self).exists(session_key)
60 62
 
61 63
     def save(self, must_create=False):
62 64
         super(SessionStore, self).save(must_create)
63  
-        cache.set(self.cache_key, self._session, self.get_expiry_age())
  65
+        self._cache.set(self.cache_key, self._session, self.get_expiry_age())
64 66
 
65 67
     def delete(self, session_key=None):
66 68
         super(SessionStore, self).delete(session_key)
@@ -68,7 +70,7 @@ def delete(self, session_key=None):
68 70
             if self.session_key is None:
69 71
                 return
70 72
             session_key = self.session_key
71  
-        cache.delete(KEY_PREFIX + session_key)
  73
+        self._cache.delete(KEY_PREFIX + session_key)
72 74
 
73 75
     def flush(self):
74 76
         """
6  django/contrib/sessions/tests.py
@@ -16,6 +16,7 @@
16 16
 from django.contrib.sessions.models import Session
17 17
 from django.contrib.sessions.middleware import SessionMiddleware
18 18
 from django.core.cache import get_cache
  19
+from django.core.cache.backends.base import InvalidCacheBackendError
19 20
 from django.core import management
20 21
 from django.core.exceptions import ImproperlyConfigured
21 22
 from django.http import HttpResponse
@@ -386,6 +387,11 @@ def test_load_overlong_key(self):
386 387
             self.session._session_key = (string.ascii_letters + string.digits) * 20
387 388
             self.assertEqual(self.session.load(), {})
388 389
 
  390
+    @override_settings(SESSION_CACHE_ALIAS='sessions')
  391
+    def test_non_default_cache(self):
  392
+        #21000 - CacheDB backend should respect SESSION_CACHE_ALIAS.
  393
+        self.assertRaises(InvalidCacheBackendError, self.backend)
  394
+
389 395
 
390 396
 @override_settings(USE_TZ=True)
391 397
 class CacheDBSessionWithTimeZoneTests(CacheDBSessionTests):
7  docs/releases/1.7.txt
@@ -154,6 +154,13 @@ Minor features
154 154
   follow the :setting:`SESSION_COOKIE_SECURE` and
155 155
   :setting:`SESSION_COOKIE_HTTPONLY` settings.
156 156
 
  157
+:mod:`django.contrib.sessions`
  158
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  159
+
  160
+* The ``"django.contrib.sessions.backends.cached_db"`` session backend now
  161
+  respects :setting:`SESSION_CACHE_ALIAS`. In previous versions, it always used
  162
+  the `default` cache.
  163
+
157 164
 :mod:`django.contrib.sitemaps`
158 165
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
159 166
 
5  docs/topics/http/sessions.txt
@@ -93,6 +93,11 @@ session data be expunged from time to time, the ``cache`` backend is for you.
93 93
 If you use the ``cached_db`` session backend, you also need to follow the
94 94
 configuration instructions for the `using database-backed sessions`_.
95 95
 
  96
+.. versionchanged:: 1.7
  97
+
  98
+Before version 1.7, the ``cached_db`` backend always used the ``default`` cache
  99
+rather than the :setting:`SESSION_CACHE_ALIAS`.
  100
+
96 101
 Using file-based sessions
97 102
 -------------------------
98 103
 

2 notes on commit ed9cd4f

Vladislav

Backport that to 1.5, please!

Val Kolovos

+1 on backport!

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