Ticket #6791: cached_db_docs_and_tests-2.diff
File cached_db_docs_and_tests-2.diff, 4.7 KB (added by , 17 years ago) |
---|
-
django/contrib/sessions/tests.py
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 … … 23 24 >>> db_session.exists(db_session.session_key) 24 25 False 25 26 27 >>> cdb_session = CacheDBSession() 28 >>> cdb_session.modified 29 False 30 >>> cdb_session['cat'] = "dog" 31 >>> cdb_session.modified 32 True 33 >>> cdb_session.pop('cat') 34 'dog' 35 >>> cdb_session.pop('some key', 'does not exist') 36 'does not exist' 37 >>> cdb_session.save() 38 >>> cdb_session.exists(cdb_session.session_key) 39 True 40 >>> cdb_session.delete(cdb_session.session_key) 41 >>> cdb_session.exists(cdb_session.session_key) 42 False 43 26 44 >>> file_session = FileSession() 27 45 >>> file_session.modified 28 46 False -
django/contrib/sessions/backends/cached_db.py
1 from django.conf import settings 2 from django.contrib.sessions.backends.db import SessionStore as DBStore 3 from django.core.cache import cache 4 5 class SessionStore(DBStore): 6 """ 7 Implements cached database session store 8 """ 9 def __init__(self, session_key=None): 10 super(SessionStore, self).__init__(session_key) 11 12 def _get_cache_key(self): 13 return 'django_session_backend_cache_%s' % (self.session_key) 14 15 def load(self): 16 cache_key = self._get_cache_key() 17 data = cache.get(cache_key, None) 18 if data is None: 19 data = super(SessionStore, self).load() 20 cache.set(cache_key, data, settings.SESSION_COOKIE_AGE) 21 return data 22 23 def exists(self, session_key): 24 return super(SessionStore, self).exists(session_key) 25 26 def save(self): 27 super(SessionStore, self).save() 28 cache.set(self._get_cache_key(), self._session, settings.SESSION_COOKIE_AGE) 29 30 def delete(self, session_key): 31 super(SessionStore, self).delete(session_key) 32 cache.delete(self._get_cache_key()) -
docs/sessions.txt
53 53 where Django stores session files. Be sure to check that your Web server has 54 54 permissions to read and write to this location. 55 55 56 Using cache -based sessions57 --------------------- -----56 Using cached sessions 57 --------------------- 58 58 59 To store session data using Django's cache system, set ``SESSION_ENGINE`` 60 to ``"django.contrib.sessions.backends.cache"``. You'll want to make sure 61 you've configured your cache; see the `cache documentation`_ for details. 59 There are two cache backends for Django. Setting ``SESSION_ENGINE`` to 60 ``"django.contrib.sessions.backends.cache"`` uses a simple caching session 61 store, while ``"django.contrib.sessions.backends.cached_db"`` uses a 62 write-through cache. In a write-through cache, every write to the cache 63 causes a write to the backing store, while reads only use the backing store 64 on a cache miss. Both session stores offer high performance, but the simple 65 cache store trades more performance for persistence. 62 66 67 You'll want to make sure you've configured your cache; see the 68 `cache documentation`_ for details. 69 63 70 .. _cache documentation: ../cache/ 64 71 65 72 .. note:: 73 If you decide to use the ``cache`` session store, you should 74 probably only use the memcached cache backend. The local memory and simple 75 cache backends don't retain data long enough to be good choices; It'll be 76 faster to use file or database sessions directly instead of sending 77 everything through the file or database cache backends. 66 78 67 You should probably only use cache-based sessions if you're using the68 memcached cache backend. The local memory and simple cache backends69 don't retain data long enough to be good choices, and it'll be faster70 to use file or database sessions directly instead of sending everything71 through the file or database cache backends.72 73 79 Using sessions in views 74 80 ======================= 75 81