diff --git a/django/contrib/sessions/backends/cached_db.py b/django/contrib/sessions/backends/cached_db.py index 580e907c30..342aa600d3 100644 --- a/django/contrib/sessions/backends/cached_db.py +++ b/django/contrib/sessions/backends/cached_db.py @@ -28,6 +28,8 @@ class SessionStore(DBStore): return data def exists(self, session_key): + if (KEY_PREFIX + session_key) in cache: + return True return super(SessionStore, self).exists(session_key) def save(self, must_create=False): diff --git a/django/contrib/sessions/tests.py b/django/contrib/sessions/tests.py index f94753cbcf..50297dce2f 100644 --- a/django/contrib/sessions/tests.py +++ b/django/contrib/sessions/tests.py @@ -293,6 +293,11 @@ class CacheDBSessionTests(SessionTestsMixin, TestCase): backend = CacheDBSession + def test_exists_searches_cache_first(self): + self.session.save() + with self.assertNumQueries(0): + self.assertTrue(self.session.exists(self.session.session_key)) + CacheDBSessionWithTimeZoneTests = override_settings(USE_TZ=True)(CacheDBSessionTests)