From 0d48087a53c84eb109f6c85472530899dfc94fc8 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Fri, 15 Aug 2008 14:59:11 +0000 Subject: [PATCH] Made a few small tweaks to reduce persistent storage accesses in the session backend. Refs #8311, although doesn't fix the problem there. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8381 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/sessions/backends/base.py | 6 +++++- django/contrib/sessions/backends/cache.py | 2 ++ django/contrib/sessions/backends/db.py | 2 ++ django/contrib/sessions/backends/file.py | 2 ++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/django/contrib/sessions/backends/base.py b/django/contrib/sessions/backends/base.py index f64ef852cf..b553fa4d58 100644 --- a/django/contrib/sessions/backends/base.py +++ b/django/contrib/sessions/backends/base.py @@ -121,7 +121,11 @@ class SessionBase(object): return self._session.iteritems() def clear(self): - self._session.clear() + # To avoid unnecessary persistent storage accesses, we set up the + # internals directly (loading data wastes time, since we are going to + # set it to an empty dict anyway). + self._session_cache = {} + self.accessed = True self.modified = True def _get_new_session_key(self): diff --git a/django/contrib/sessions/backends/cache.py b/django/contrib/sessions/backends/cache.py index 157d18a586..dad5117ea3 100644 --- a/django/contrib/sessions/backends/cache.py +++ b/django/contrib/sessions/backends/cache.py @@ -42,6 +42,8 @@ class SessionStore(SessionBase): def delete(self, session_key=None): if session_key is None: + if self._session_key is None: + return session_key = self._session_key self._cache.delete(session_key) diff --git a/django/contrib/sessions/backends/db.py b/django/contrib/sessions/backends/db.py index e697bc56b1..0d68f13e68 100644 --- a/django/contrib/sessions/backends/db.py +++ b/django/contrib/sessions/backends/db.py @@ -63,6 +63,8 @@ class SessionStore(SessionBase): def delete(self, session_key=None): if session_key is None: + if self._session_key is None: + return session_key = self._session_key try: Session.objects.get(session_key=session_key).delete() diff --git a/django/contrib/sessions/backends/file.py b/django/contrib/sessions/backends/file.py index 3438b097c4..1540139710 100644 --- a/django/contrib/sessions/backends/file.py +++ b/django/contrib/sessions/backends/file.py @@ -94,6 +94,8 @@ class SessionStore(SessionBase): def delete(self, session_key=None): if session_key is None: + if self._session_key is None: + return session_key = self._session_key try: os.unlink(self._key_to_file(session_key))