app/django/contrib/sessions/backends/cache.py
author Sverre Rabbelier <srabbelier@gmail.com>
Thu, 09 Oct 2008 23:48:20 +0000
changeset 294 1fdaab4a6ef2
parent 54 03e267d67478
child 323 ff1a9aa48cfd
permissions -rw-r--r--
Refactor existing code to use the new access module Instead of ending up with many different ways to do access control, we end up having only one centralized place wher access control is done. Patch by: Sverre Rabbelier Reviewed by: Pawel Solyga, Augie Fackler, Todd Larsen Reviewed at: http://codereviews.googleopensourceprograms.com/1601 Review id: 1601

from django.conf import settings
from django.contrib.sessions.backends.base import SessionBase
from django.core.cache import cache

class SessionStore(SessionBase):
    """
    A cache-based session store. 
    """
    def __init__(self, session_key=None):
        self._cache = cache
        super(SessionStore, self).__init__(session_key)
        
    def load(self):
        session_data = self._cache.get(self.session_key)
        return session_data or {}

    def save(self):
        self._cache.set(self.session_key, self._session, settings.SESSION_COOKIE_AGE)

    def exists(self, session_key):
        if self._cache.get(session_key):
            return True
        return False
        
    def delete(self, session_key):
        self._cache.delete(session_key)