Ticket #6791: cached_db_with_documentation.diff

File cached_db_with_documentation.diff, 3.5 KB (added by mcroydon, 17 years ago)

Added documentation to patch against trunk

  • django/contrib/sessions/backends/cached_db.py

     
     1from django.conf import settings
     2from django.contrib.sessions.models import Session
     3from django.contrib.sessions.backends.base import SessionBase
     4from django.core.exceptions import SuspiciousOperation
     5from django.core.cache import cache
     6import datetime
     7
     8class SessionStore(SessionBase):
     9    """
     10    Implements cached database session store
     11    """
     12    def __init__(self, session_key=None):
     13        super(SessionStore, self).__init__(session_key)
     14
     15    def _get_cache_key(self):
     16        return 'django_session_backend_cache_%s' % (self.session_key)
     17   
     18    def load(self):
     19        try:
     20            data = cache.get(self._get_cache_key(), None)
     21            if data is None:
     22                s = Session.objects.get(
     23                    session_key = self.session_key,
     24                    expire_date__gt=datetime.datetime.now()
     25                    )
     26                data = self.decode(s.session_data)
     27                cache.set(self._get_cache_key(), data, settings.SESSION_COOKIE_AGE)
     28            return data
     29        except (Session.DoesNotExist, SuspiciousOperation):
     30            # Create a new session_key for extra security.
     31            self.session_key = self._get_new_session_key()
     32            self._session_cache = {}
     33
     34            # Save immediately to minimize collision
     35            self.save()
     36            return {}
     37           
     38    def exists(self, session_key):
     39        try:
     40            Session.objects.get(session_key=session_key)
     41        except Session.DoesNotExist:
     42            return False
     43        return True
     44           
     45    def save(self):
     46        cache.set(self._get_cache_key(), self._session, settings.SESSION_COOKIE_AGE)
     47        Session.objects.create(
     48            session_key = self.session_key,
     49            session_data = self.encode(self._session),
     50            expire_date = datetime.datetime.now() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE))
     51   
     52    def delete(self, session_key):
     53        try:
     54            cache.delete(self._get_cache_key())
     55            Session.objects.get(session_key=session_key).delete()
     56        except Session.DoesNotExist:
     57            pass
  • docs/sessions.txt

     
    5656Using cache-based sessions
    5757--------------------------
    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-based session stores to choose from.  To directly
     60store session data using Django's cache system, set ``SESSION_ENGINE``
     61to ``"django.contrib.sessions.backends.cache"``.  To store sessions in
     62the database but cache read operations using Django's cache system, set
     63``SESSION_ENGINE`` to ``"django.contrib.sessions.backend.db_cache"``.
     64This session store is useful if your cache is reset or flushed frequently
     65and combines efficient read operations with persistent storage. You'll
     66want to make sure you've configured your cache; see the
     67`cache documentation`_ for details.
    6268
    6369.. _cache documentation: ../cache/
    6470
Back to Top