Code

Ticket #6791: cached_db_docs_and_tests-2.diff

File cached_db_docs_and_tests-2.diff, 4.7 KB (added by jdunck, 6 years ago)

Last patch didn't include the actual code for cached_db backend. :-/

Line 
1Index: django/contrib/sessions/tests.py
2===================================================================
3--- django/contrib/sessions/tests.py    (revision 7360)
4+++ django/contrib/sessions/tests.py    (working copy)
5@@ -3,6 +3,7 @@
6 >>> from django.conf import settings
7 >>> from django.contrib.sessions.backends.db import SessionStore as DatabaseSession
8 >>> from django.contrib.sessions.backends.cache import SessionStore as CacheSession
9+>>> from django.contrib.sessions.backends.cached_db import SessionStore as CacheDBSession
10 >>> from django.contrib.sessions.backends.file import SessionStore as FileSession
11 >>> from django.contrib.sessions.backends.base import SessionBase
12 
13@@ -23,6 +24,23 @@
14 >>> db_session.exists(db_session.session_key)
15 False
16 
17+>>> cdb_session = CacheDBSession()
18+>>> cdb_session.modified
19+False
20+>>> cdb_session['cat'] = "dog"
21+>>> cdb_session.modified
22+True
23+>>> cdb_session.pop('cat')
24+'dog'
25+>>> cdb_session.pop('some key', 'does not exist')
26+'does not exist'
27+>>> cdb_session.save()
28+>>> cdb_session.exists(cdb_session.session_key)
29+True
30+>>> cdb_session.delete(cdb_session.session_key)
31+>>> cdb_session.exists(cdb_session.session_key)
32+False
33+
34 >>> file_session = FileSession()
35 >>> file_session.modified
36 False
37Index: django/contrib/sessions/backends/cached_db.py
38===================================================================
39--- django/contrib/sessions/backends/cached_db.py       (revision 0)
40+++ django/contrib/sessions/backends/cached_db.py       (revision 0)
41@@ -0,0 +1,32 @@
42+from django.conf import settings
43+from django.contrib.sessions.backends.db import SessionStore as DBStore
44+from django.core.cache import cache
45+
46+class SessionStore(DBStore):
47+    """
48+    Implements cached database session store
49+    """
50+    def __init__(self, session_key=None):
51+        super(SessionStore, self).__init__(session_key)
52+
53+    def _get_cache_key(self):
54+        return 'django_session_backend_cache_%s' % (self.session_key)
55+     
56+    def load(self):
57+        cache_key = self._get_cache_key()
58+        data = cache.get(cache_key, None)
59+        if data is None:
60+            data = super(SessionStore, self).load()
61+            cache.set(cache_key, data, settings.SESSION_COOKIE_AGE)
62+        return data
63+             
64+    def exists(self, session_key):
65+        return super(SessionStore, self).exists(session_key)
66+             
67+    def save(self):
68+        super(SessionStore, self).save()
69+        cache.set(self._get_cache_key(), self._session, settings.SESSION_COOKIE_AGE)
70+     
71+    def delete(self, session_key):
72+        super(SessionStore, self).delete(session_key)
73+        cache.delete(self._get_cache_key())
74Index: docs/sessions.txt
75===================================================================
76--- docs/sessions.txt   (revision 7360)
77+++ docs/sessions.txt   (working copy)
78@@ -53,23 +53,29 @@
79 where Django stores session files. Be sure to check that your Web server has
80 permissions to read and write to this location.
81 
82-Using cache-based sessions
83---------------------------
84+Using cached sessions
85+---------------------
86 
87-To store session data using Django's cache system, set ``SESSION_ENGINE``
88-to ``"django.contrib.sessions.backends.cache"``. You'll want to make sure
89-you've configured your cache; see the `cache documentation`_ for details.
90+There are two cache backends for Django.  Setting ``SESSION_ENGINE`` to
91+``"django.contrib.sessions.backends.cache"`` uses a simple caching session
92+store, while ``"django.contrib.sessions.backends.cached_db"`` uses a
93+write-through cache.  In a write-through cache, every write to the cache
94+causes a write to the backing store, while reads only use the backing store
95+on a cache miss.  Both session stores offer high performance, but the simple
96+cache store trades more performance for persistence.
97 
98+You'll want to make sure you've configured your cache; see the
99+`cache documentation`_ for details.
100+
101 .. _cache documentation: ../cache/
102 
103 .. note::
104+     If you decide to use the ``cache`` session store, you should
105+     probably only use the memcached cache backend. The local memory and simple
106+     cache backends don't retain data long enough to be good choices; It'll be
107+     faster to use file or database sessions directly instead of sending
108+     everything through the file or database cache backends.
109 
110-    You should probably only use cache-based sessions if you're using the
111-    memcached cache backend. The local memory and simple cache backends
112-    don't retain data long enough to be good choices, and it'll be faster
113-    to use file or database sessions directly instead of sending everything
114-    through the file or database cache backends.
115-
116 Using sessions in views
117 =======================
118