Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6791: added a write-through cache session backend: session dat…

…a is written through the cache to the database, but read from the cache for speed. Thanks to jhenry, mcroydon, and jdunck.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9727 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 299e1e814fa7c5f8033872213b8876fc12fcd7be 1 parent d32c290
Jacob Kaplan-Moss authored January 10, 2009
1  AUTHORS
@@ -201,6 +201,7 @@ answer newbie questions, and generally made Django that much better:
201 201
     james_027@yahoo.com
202 202
     jcrasta@gmail.com
203 203
     jdetaeye
  204
+    jhenry <jhenry@theonion.com>
204 205
     Zak Johnson <zakj@nox.cx>
205 206
     Nis Jørgensen <nis@superlativ.dk>
206 207
     Michael Josephson <http://www.sdjournal.com/>
42  django/contrib/sessions/backends/cached_db.py
... ...
@@ -0,0 +1,42 @@
  1
+"""
  2
+Cached, database-backed sessions.
  3
+"""
  4
+
  5
+from django.conf import settings
  6
+from django.contrib.sessions.backends.db import SessionStore as DBStore
  7
+from django.core.cache import cache
  8
+
  9
+class SessionStore(DBStore):
  10
+    """
  11
+    Implements cached, database backed sessions.
  12
+    """
  13
+
  14
+    def __init__(self, session_key=None):
  15
+        super(SessionStore, self).__init__(session_key)
  16
+
  17
+    def load(self):
  18
+        data = cache.get(self.session_key, None)
  19
+        if data is None:
  20
+            data = super(SessionStore, self).load()
  21
+            cache.set(self.session_key, data, settings.SESSION_COOKIE_AGE)
  22
+        return data
  23
+
  24
+    def exists(self, session_key):
  25
+        return super(SessionStore, self).exists(session_key)
  26
+
  27
+    def save(self, must_create=False):
  28
+        super(SessionStore, self).save(must_create)
  29
+        cache.set(self.session_key, self._session, settings.SESSION_COOKIE_AGE)
  30
+
  31
+    def delete(self, session_key=None):
  32
+        super(SessionStore, self).delete(session_key)
  33
+        cache.delete(session_key or self.session_key)
  34
+
  35
+    def flush(self):
  36
+        """
  37
+        Removes the current session data from the database and regenerates the
  38
+        key.
  39
+        """
  40
+        self.clear()
  41
+        self.delete(self.session_key)
  42
+        self.create()
26  django/contrib/sessions/tests.py
@@ -3,6 +3,7 @@
3 3
 >>> from django.conf import settings
4 4
 >>> from django.contrib.sessions.backends.db import SessionStore as DatabaseSession
5 5
 >>> from django.contrib.sessions.backends.cache import SessionStore as CacheSession
  6
+>>> from django.contrib.sessions.backends.cached_db import SessionStore as CacheDBSession
6 7
 >>> from django.contrib.sessions.backends.file import SessionStore as FileSession
7 8
 >>> from django.contrib.sessions.backends.base import SessionBase
8 9
 >>> from django.contrib.sessions.models import Session
@@ -54,6 +55,31 @@
54 55
 >>> db_session.save()
55 56
 >>> DatabaseSession('1').get('cat')
56 57
 
  58
+#
  59
+# Cached DB session tests
  60
+#
  61
+
  62
+>>> cdb_session = CacheDBSession()
  63
+>>> cdb_session.modified
  64
+False
  65
+>>> cdb_session['cat'] = "dog"
  66
+>>> cdb_session.modified
  67
+True
  68
+>>> cdb_session.pop('cat')
  69
+'dog'
  70
+>>> cdb_session.pop('some key', 'does not exist')
  71
+'does not exist'
  72
+>>> cdb_session.save()
  73
+>>> cdb_session.exists(cdb_session.session_key)
  74
+True
  75
+>>> cdb_session.delete(cdb_session.session_key)
  76
+>>> cdb_session.exists(cdb_session.session_key)
  77
+False
  78
+
  79
+#
  80
+# File session tests.
  81
+#
  82
+
57 83
 # Do file session tests in an isolated directory, and kill it after we're done.
58 84
 >>> original_session_file_path = settings.SESSION_FILE_PATH
59 85
 >>> import tempfile
57  docs/topics/http/sessions.txt
@@ -43,6 +43,46 @@ By default, Django stores sessions in your database (using the model
43 43
 some setups it's faster to store session data elsewhere, so Django can be
44 44
 configured to store session data on your filesystem or in your cache.
45 45
 
  46
+Using cached sessions
  47
+---------------------
  48
+
  49
+For better performance, you may want to use a cache-based session backend.
  50
+
  51
+.. versionchanged:: 1.1
  52
+   Django 1.0 did not include the ``cached_db`` session backend.
  53
+
  54
+To store session data using Django's cache system, you'll first need to make
  55
+sure you've configured your cache; see the :ref:`cache documentation
  56
+<topics-cache>` for details.
  57
+
  58
+.. warning::
  59
+
  60
+    You should only use cache-based sessions if you're using the Memcached cache
  61
+    backend. The local-memory cache backend doesn't retain data long enough to
  62
+    be a good choice, and it'll be faster to use file or database sessions
  63
+    directly instead of sending everything through the file or database cache
  64
+    backends.
  65
+
  66
+Once your cache in configured, you've got two choices for how to store data in
  67
+the cache:
  68
+
  69
+    * Set :setting:`SESSION_ENGINE` to
  70
+      ``"django.contrib.sessions.backends.cache"`` for a simple caching session
  71
+      store. Session data will be stored directly your cache. However, session
  72
+      data may not be persistant: cached data can be evicted if the cache fills
  73
+      up or if the cache server is restarted.
  74
+
  75
+    * For persistant, cached data, set :setting:`SESSION_ENGINE` to
  76
+      ``"django.contrib.sessions.backends.cached_db"``. This uses a
  77
+      write-through cache -- every write to the cache will also be written to
  78
+      the database. Session reads only use the database if the data is not
  79
+      already in the cache.
  80
+
  81
+Both session stores are quite fast, but the simple cache is faster because it
  82
+disreguards persistance. In most cases, the ``cached_db`` backend will be fast
  83
+enough, but if you need that last bit of performance, and are willing to let
  84
+session data be expunged from time to time, the ``cache`` backend is for you.
  85
+
46 86
 Using file-based sessions
47 87
 -------------------------
48 88
 
@@ -54,23 +94,6 @@ to output from ``tempfile.gettempdir()``, most likely ``/tmp``) to control
54 94
 where Django stores session files. Be sure to check that your Web server has
55 95
 permissions to read and write to this location.
56 96
 
57  
-Using cache-based sessions
58  
---------------------------
59  
-
60  
-To store session data using Django's cache system, set ``SESSION_ENGINE``
61  
-to ``"django.contrib.sessions.backends.cache"``. You'll want to make sure
62  
-you've configured your cache; see the :ref:`cache documentation <topics-cache>` for details.
63  
-
64  
-.. _cache documentation: ../cache/
65  
-
66  
-.. note::
67  
-
68  
-    You should probably only use cache-based sessions if you're using the
69  
-    Memcached cache backend. The local-memory cache backend doesn't retain data
70  
-    long enough to be a good choice, and it'll be faster to use file or
71  
-    database sessions directly instead of sending everything through the file
72  
-    or database cache backends.
73  
-
74 97
 Using sessions in views
75 98
 =======================
76 99
 

0 notes on commit 299e1e8

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