Ticket #6791: cached_db_docs_and_tests-2.diff

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

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

  • django/contrib/sessions/tests.py

     
    33>>> from django.conf import settings
    44>>> from django.contrib.sessions.backends.db import SessionStore as DatabaseSession
    55>>> from django.contrib.sessions.backends.cache import SessionStore as CacheSession
     6>>> from django.contrib.sessions.backends.cached_db import SessionStore as CacheDBSession
    67>>> from django.contrib.sessions.backends.file import SessionStore as FileSession
    78>>> from django.contrib.sessions.backends.base import SessionBase
    89
     
    2324>>> db_session.exists(db_session.session_key)
    2425False
    2526
     27>>> cdb_session = CacheDBSession()
     28>>> cdb_session.modified
     29False
     30>>> cdb_session['cat'] = "dog"
     31>>> cdb_session.modified
     32True
     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)
     39True
     40>>> cdb_session.delete(cdb_session.session_key)
     41>>> cdb_session.exists(cdb_session.session_key)
     42False
     43
    2644>>> file_session = FileSession()
    2745>>> file_session.modified
    2846False
  • django/contrib/sessions/backends/cached_db.py

     
     1from django.conf import settings
     2from django.contrib.sessions.backends.db import SessionStore as DBStore
     3from django.core.cache import cache
     4 
     5class 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

     
    5353where Django stores session files. Be sure to check that your Web server has
    5454permissions to read and write to this location.
    5555
    56 Using cache-based sessions
    57 --------------------------
     56Using cached sessions
     57---------------------
    5858
    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.
     59There are two cache backends for Django.  Setting ``SESSION_ENGINE`` to
     60``"django.contrib.sessions.backends.cache"`` uses a simple caching session
     61store, while ``"django.contrib.sessions.backends.cached_db"`` uses a
     62write-through cache.  In a write-through cache, every write to the cache
     63causes a write to the backing store, while reads only use the backing store
     64on a cache miss.  Both session stores offer high performance, but the simple
     65cache store trades more performance for persistence.
    6266
     67You'll want to make sure you've configured your cache; see the
     68`cache documentation`_ for details.
     69
    6370.. _cache documentation: ../cache/
    6471
    6572.. 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.
    6678
    67     You should probably only use cache-based sessions if you're using the
    68     memcached cache backend. The local memory and simple cache backends
    69     don't retain data long enough to be good choices, and it'll be faster
    70     to use file or database sessions directly instead of sending everything
    71     through the file or database cache backends.
    72 
    7379Using sessions in views
    7480=======================
    7581
Back to Top