summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCHI Cheng <git@chicheng.me>2013-08-30 14:03:23 +1000
committerTim Graham <timograham@gmail.com>2013-09-05 10:47:58 -0400
commited9cd4fd8b425ab5010b27aefc7ef6e50d55fb54 (patch)
tree1592d2a4215526e0d276b2eab376c142cbf47b3c
parenta2374bcf476b49441793f1ad213126b07543da54 (diff)
Fixed #21000 -- Made cached_db session backend respect SESSION_CACHE_ALIAS
-rw-r--r--django/contrib/sessions/backends/cached_db.py14
-rw-r--r--django/contrib/sessions/tests.py6
-rw-r--r--docs/releases/1.7.txt7
-rw-r--r--docs/topics/http/sessions.txt5
4 files changed, 26 insertions, 6 deletions
diff --git a/django/contrib/sessions/backends/cached_db.py b/django/contrib/sessions/backends/cached_db.py
index be22c1f97a..15b7172f85 100644
--- a/django/contrib/sessions/backends/cached_db.py
+++ b/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):
"""
diff --git a/django/contrib/sessions/tests.py b/django/contrib/sessions/tests.py
index 4caefe938c..30814e466b 100644
--- a/django/contrib/sessions/tests.py
+++ b/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):
diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt
index 10826829a8..aa68b722b6 100644
--- a/docs/releases/1.7.txt
+++ b/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`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/docs/topics/http/sessions.txt b/docs/topics/http/sessions.txt
index 8bef7f2ee5..fb66034e7b 100644
--- a/docs/topics/http/sessions.txt
+++ b/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
-------------------------