Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fixed #17083 -- Allow cached based sessions to use a custom cache backend #169

Closed
wants to merge 1 commit into from

3 participants

Alex Tomkins Florian Apolloner Aymeric Augustin
Alex Tomkins

Django currently lacks a way of using a custom cache backend in django.contrib.sessions - it always uses the default backend.

https://code.djangoproject.com/ticket/17083

This commit will default to the "sessions" backend, but will fall back to the "default" backend, this is similar to django.contrib.staticfiles behaviour.

Florian Apolloner
Owner

Can you please add tests which check that the backend in question is actually used?

Aymeric Augustin aaugustin closed this October 27, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 22, 2012
Alex Tomkins Fixed #17083 -- Allow cached based sessions to use a custom cache bac…
…kend
fa82a6d
This page is out of date. Refresh to see the latest.
7  django/contrib/sessions/backends/cache.py
... ...
@@ -1,5 +1,5 @@
1 1
 from django.contrib.sessions.backends.base import SessionBase, CreateError
2  
-from django.core.cache import cache
  2
+from django.core.cache import get_cache, InvalidCacheBackendError, cache
3 3
 
4 4
 KEY_PREFIX = "django.contrib.sessions.cache"
5 5
 
@@ -10,6 +10,11 @@ class SessionStore(SessionBase):
10 10
     """
11 11
     def __init__(self, session_key=None):
12 12
         self._cache = cache
  13
+        try:
  14
+            self._cache = get_cache('sessions')
  15
+        except InvalidCacheBackendError:
  16
+            # Use the default backend
  17
+            self._cache = cache
13 18
         super(SessionStore, self).__init__(session_key)
14 19
 
15 20
     @property
4  docs/releases/1.5.txt
@@ -102,6 +102,10 @@ Django 1.5 also includes several smaller improvements worth noting:
102 102
 * In the localflavor for Canada, "pq" was added to the acceptable codes for
103 103
   Quebec. It's an old abbreviation.
104 104
 
  105
+* :ref:`Cached-based sessions <cached-sessions-backend>` can now use a custom
  106
+  cache backend. Add a ``'sessions'`` entry to the :setting:`CACHES` setting to
  107
+  use a different backend instead of the ``'default'`` backend.
  108
+
105 109
 Backwards incompatible changes in 1.5
106 110
 =====================================
107 111
 
7  docs/topics/http/sessions.txt
@@ -45,6 +45,8 @@ If you want to use a database-backed session, you need to add
45 45
 Once you have configured your installation, run ``manage.py syncdb``
46 46
 to install the single database table that stores session data.
47 47
 
  48
+.. _cached-sessions-backend:
  49
+
48 50
 Using cached sessions
49 51
 ---------------------
50 52
 
@@ -77,6 +79,11 @@ the cache:
77 79
   the database. Session reads only use the database if the data is not
78 80
   already in the cache.
79 81
 
  82
+.. versionchanged:: 1.5
  83
+    Cached sessions use the ``'sessions'`` entry in the :setting:`CACHES`
  84
+    setting, but will fall back to the ``'default'`` cache backend if this
  85
+    doesn't exist.
  86
+
80 87
 Both session stores are quite fast, but the simple cache is faster because it
81 88
 disregards persistence. In most cases, the ``cached_db`` backend will be fast
82 89
 enough, but if you need that last bit of performance, and are willing to let
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.