Skip to content

Commit

Permalink
[1.1.X] Fixed #11483 -- Modified db cache backend to use db backend f…
Browse files Browse the repository at this point in the history
…unctions for date conversion, avoiding problems under Jython. Thanks to Leo Soto for the report and patch.

Merge of r12411 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12415 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Feb 11, 2010
1 parent d4a34b5 commit f764d7f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
9 changes: 6 additions & 3 deletions django/core/cache/backends/db.py
Expand Up @@ -60,9 +60,11 @@ def _base_set(self, mode, key, value, timeout=None):
result = cursor.fetchone() result = cursor.fetchone()
if result and (mode == 'set' or if result and (mode == 'set' or
(mode == 'add' and result[1] < now)): (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("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table,
[encoded, connection.ops.value_to_db_datetime(exp), key])
else: else:
cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)]) cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table,
[key, encoded, connection.ops.value_to_db_datetime(exp)])
except DatabaseError: except DatabaseError:
# To be threadsafe, updates/inserts are allowed to fail silently # To be threadsafe, updates/inserts are allowed to fail silently
transaction.rollback_unless_managed() transaction.rollback_unless_managed()
Expand All @@ -86,7 +88,8 @@ def _cull(self, cursor, now):
if self._cull_frequency == 0: if self._cull_frequency == 0:
cursor.execute("DELETE FROM %s" % self._table) cursor.execute("DELETE FROM %s" % self._table)
else: else:
cursor.execute("DELETE FROM %s WHERE expires < %%s" % self._table, [str(now)]) cursor.execute("DELETE FROM %s WHERE expires < %%s" % self._table,
[connection.ops.value_to_db_datetime(now)])
cursor.execute("SELECT COUNT(*) FROM %s" % self._table) cursor.execute("SELECT COUNT(*) FROM %s" % self._table)
num = cursor.fetchone()[0] num = cursor.fetchone()[0]
if num > self._max_entries: if num > self._max_entries:
Expand Down
4 changes: 0 additions & 4 deletions tests/regressiontests/cache/tests.py
Expand Up @@ -290,10 +290,6 @@ def test_long_timeout(self):
self.cache.add('key2', 'ham', 60*60*24*30 + 1) self.cache.add('key2', 'ham', 60*60*24*30 + 1)
self.assertEqual(self.cache.get('key2'), 'ham') self.assertEqual(self.cache.get('key2'), 'ham')


self.cache.set_many({'key3': 'sausage', 'key4': 'lobster bisque'}, 60*60*24*30 + 1)
self.assertEqual(self.cache.get('key3'), 'sausage')
self.assertEqual(self.cache.get('key4'), 'lobster bisque')

class DBCacheTests(unittest.TestCase, BaseCacheTests): class DBCacheTests(unittest.TestCase, BaseCacheTests):
def setUp(self): def setUp(self):
# Spaces are used in the table name to ensure quoting/escaping is working # Spaces are used in the table name to ensure quoting/escaping is working
Expand Down

0 comments on commit f764d7f

Please sign in to comment.