diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2009-03-02 03:15:27 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2009-03-02 03:15:27 +0000 |
| commit | 8f28a84a9816812fc98b3f593c6f3f2537475dd9 (patch) | |
| tree | 0125dc8aa3f1aae62ab9b921d1d244099ec313bb | |
| parent | e2cb2725eb144ddb549555498ff09d01144dc110 (diff) | |
The database cache wasn't correctly handling expired keys. Fixed 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.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9942 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/core/cache/backends/db.py | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/django/core/cache/backends/db.py b/django/core/cache/backends/db.py index d2b422af83..06a99d11a6 100644 --- a/django/core/cache/backends/db.py +++ b/django/core/cache/backends/db.py @@ -54,9 +54,11 @@ class CacheClass(BaseCache): 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]) try: - 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]) else: cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)]) @@ -73,8 +75,9 @@ class CacheClass(BaseCache): transaction.commit_unless_managed() def has_key(self, key): + now = datetime.now().replace(microsecond=0) 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): |
