Commit 146ed13a authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Fixed #17083 -- Allowed sessions to use non-default cache.

parent 68847135
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -445,6 +445,7 @@ MIDDLEWARE_CLASSES = (
# SESSIONS #
############

SESSION_CACHE_ALIAS = 'default'                         # Cache to store session data if using the cache session backend.
SESSION_COOKIE_NAME = 'sessionid'                       # Cookie name. This can be whatever you want.
SESSION_COOKIE_AGE = 60 * 60 * 24 * 7 * 2               # Age of cookie, in seconds (default: 2 weeks).
SESSION_COOKIE_DOMAIN = None                            # A string like ".example.com", or None for standard domain cookie.
+3 −2
Original line number Diff line number Diff line
from django.conf import settings
from django.contrib.sessions.backends.base import SessionBase, CreateError
from django.core.cache import cache
from django.core.cache import get_cache
from django.utils.six.moves import xrange

KEY_PREFIX = "django.contrib.sessions.cache"
@@ -10,7 +11,7 @@ class SessionStore(SessionBase):
    A cache-based session store.
    """
    def __init__(self, session_key=None):
        self._cache = cache
        self._cache = get_cache(settings.SESSION_CACHE_ALIAS)
        super(SessionStore, self).__init__(session_key)

    @property
+22 −4
Original line number Diff line number Diff line
@@ -13,8 +13,8 @@ from django.contrib.sessions.backends.file import SessionStore as FileSession
from django.contrib.sessions.backends.signed_cookies import SessionStore as CookieSession
from django.contrib.sessions.models import Session
from django.contrib.sessions.middleware import SessionMiddleware
from django.core.cache import get_cache
from django.core import management
from django.core.cache import DEFAULT_CACHE_ALIAS
from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
from django.http import HttpResponse
from django.test import TestCase, RequestFactory
@@ -136,8 +136,8 @@ class SessionTestsMixin(object):
        self.assertTrue(self.session.modified)

    def test_save(self):
        if (hasattr(self.session, '_cache') and
                'DummyCache' in settings.CACHES[DEFAULT_CACHE_ALIAS]['BACKEND']):
        if (hasattr(self.session, '_cache') and'DummyCache' in
            settings.CACHES[settings.SESSION_CACHE_ALIAS]['BACKEND']):
            raise unittest.SkipTest("Session saving tests require a real cache backend")
        self.session.save()
        self.assertTrue(self.session.exists(self.session.session_key))
@@ -355,7 +355,8 @@ class CacheDBSessionTests(SessionTestsMixin, TestCase):

    backend = CacheDBSession

    @unittest.skipIf('DummyCache' in settings.CACHES[DEFAULT_CACHE_ALIAS]['BACKEND'],
    @unittest.skipIf('DummyCache' in
        settings.CACHES[settings.SESSION_CACHE_ALIAS]['BACKEND'],
        "Session saving tests require a real cache backend")
    def test_exists_searches_cache_first(self):
        self.session.save()
@@ -454,6 +455,23 @@ class CacheSessionTests(SessionTestsMixin, unittest.TestCase):
            self.session._session_key = (string.ascii_letters + string.digits) * 20
            self.assertEqual(self.session.load(), {})

    def test_default_cache(self):
        self.session.save()
        self.assertNotEqual(get_cache('default').get(self.session.cache_key), None)

    @override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
        },
        'sessions': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        },
    }, SESSION_CACHE_ALIAS='sessions')
    def test_non_default_cache(self):
        self.session.save()
        self.assertEqual(get_cache('default').get(self.session.cache_key), None)
        self.assertNotEqual(get_cache('sessions').get(self.session.cache_key), None)


class SessionMiddlewareTests(unittest.TestCase):

+10 −0
Original line number Diff line number Diff line
@@ -1693,6 +1693,16 @@ This is useful if you have multiple Django instances running under the same
hostname. They can use different cookie paths, and each instance will only see
its own session cookie.

.. setting:: SESSION_CACHE_ALIAS

SESSION_CACHE_ALIAS
-------------------

Default: ``default``

If you're using :ref:`cache-based session storage <cached-sessions-backend>`,
this selects the cache to use.

.. setting:: SESSION_COOKIE_SECURE

SESSION_COOKIE_SECURE
+3 −0
Original line number Diff line number Diff line
@@ -299,6 +299,9 @@ Django 1.5 also includes several smaller improvements worth noting:
* RemoteUserMiddleware now forces logout when the REMOTE_USER header
  disappears during the same browser session.

* The :ref:`cache-based session backend <cached-sessions-backend>` can store
  session data in a non-default cache.

Backwards incompatible changes in 1.5
=====================================

Loading