Ticket #6791: cached_db_docs_and_tests.diff

File cached_db_docs_and_tests.diff, 5.5 KB (added by Jeremy Dunck, 16 years ago)

Changed default session backend to cached_db. Changed docs to be betterer. ;-) Added tests.

  • django/conf/global_settings.py

     
    279279# SESSIONS #
    280280############
    281281
    282 SESSION_COOKIE_NAME = 'sessionid'                       # Cookie name. This can be whatever you want.
    283 SESSION_COOKIE_AGE = 60 * 60 * 24 * 7 * 2               # Age of cookie, in seconds (default: 2 weeks).
    284 SESSION_COOKIE_DOMAIN = None                            # A string like ".lawrence.com", or None for standard domain cookie.
    285 SESSION_COOKIE_SECURE = False                           # Whether the session cookie should be secure (https:// only).
    286 SESSION_COOKIE_PATH = '/'                               # The path of the session cookie.
    287 SESSION_SAVE_EVERY_REQUEST = False                      # Whether to save the session data on every request.
    288 SESSION_EXPIRE_AT_BROWSER_CLOSE = False                 # Whether sessions expire when a user closes his browser.
    289 SESSION_ENGINE = 'django.contrib.sessions.backends.db'  # The module to store session data
    290 SESSION_FILE_PATH = '/tmp/'                             # Directory to store session files if using the file session module
     282SESSION_COOKIE_NAME = 'sessionid'                              # Cookie name. This can be whatever you want.
     283SESSION_COOKIE_AGE = 60 * 60 * 24 * 7 * 2                      # Age of cookie, in seconds (default: 2 weeks).
     284SESSION_COOKIE_DOMAIN = None                                   # A string like ".lawrence.com", or None for standard domain cookie.
     285SESSION_COOKIE_SECURE = False                                  # Whether the session cookie should be secure (https:// only).
     286SESSION_COOKIE_PATH = '/'                                      # The path of the session cookie.
     287SESSION_SAVE_EVERY_REQUEST = False                             # Whether to save the session data on every request.
     288SESSION_EXPIRE_AT_BROWSER_CLOSE = False                        # Whether sessions expire when a user closes his browser.
     289SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'  # The module to store session data
     290SESSION_FILE_PATH = '/tmp/'                                    # Directory to store session files if using the file session module
    291291
    292292#########
    293293# CACHE #
  • 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
  • docs/sessions.txt

     
    5353sure to check that your Web server has permissions to read and write to
    5454this 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.
    7279
     80
    7381Using sessions in views
    7482=======================
    7583
Back to Top