Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

[1.0.X] The database cache wasn't correctly handling expired keys. Fi…

…xed now.

The cache tests have been failing for a long time with the db backend. This
change makes them pass again, so no test changes required here.

Backport of r9942 from trunk.

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 95763538358e6a3358bf0e253093576a21c3b74a 1 parent ec5a3e7
@malcolmt malcolmt authored
Showing with 6 additions and 3 deletions.
  1. +6 −3 django/core/cache/backends/
9 django/core/cache/backends/
@@ -54,9 +54,11 @@ def _base_set(self, mode, key, value, timeout=None):
if num > self._max_entries:
self._cull(cursor, now)
encoded = base64.encodestring(pickle.dumps(value, 2)).strip()
- cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key])
+ cursor.execute("SELECT cache_key, expires FROM %s WHERE cache_key = %%s" % self._table, [key])
- if mode == 'set' and cursor.fetchone():
+ result = cursor.fetchone()
+ if result and (mode == 'set' or
+ (mode == 'add' and result[1] < now)):
cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key])
cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)])
@@ -73,8 +75,9 @@ def delete(self, key):
def has_key(self, key):
+ now =
cursor = connection.cursor()
- cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key])
+ cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s and expires > %%s" % self._table, [key, now])
return cursor.fetchone() is not None
def _cull(self, cursor, now):

0 comments on commit 9576353

Please sign in to comment.
Something went wrong with that request. Please try again.