Fixed #515 (again) - renamed "key" field in SQL cache to "cache_key" because

MySQL reserves "key".


git-svn-id: http://code.djangoproject.com/svn/django/trunk@695 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jacob Kaplan-Moss 2005-09-26 03:14:37 +00:00
parent a978dbe779
commit 871ffbe7ee
2 changed files with 11 additions and 10 deletions

View File

@ -377,13 +377,13 @@ class _DBCache(_Cache):
def get(self, key, default=None): def get(self, key, default=None):
cursor = db.cursor() cursor = db.cursor()
cursor.execute("SELECT key, value, expires FROM %s WHERE key = %%s" % self._table, [key]) cursor.execute("SELECT cache_key, value, expires FROM %s WHERE cache_key = %%s" % self._table, [key])
row = cursor.fetchone() row = cursor.fetchone()
if row is None: if row is None:
return default return default
now = datetime.now() now = datetime.now()
if row[2] < now: if row[2] < now:
cursor.execute("DELETE FROM %s WHERE key = %%s" % self._table, [key]) cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key])
db.commit() db.commit()
return default return default
return pickle.loads(base64.decodestring(row[1])) return pickle.loads(base64.decodestring(row[1]))
@ -399,21 +399,21 @@ class _DBCache(_Cache):
if num > self._max_entries: if num > self._max_entries:
self._cull(cursor, now) self._cull(cursor, now)
encoded = base64.encodestring(pickle.dumps(value, 2)).strip() encoded = base64.encodestring(pickle.dumps(value, 2)).strip()
cursor.execute("SELECT key FROM %s WHERE key = %%s" % self._table, [key]) cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key])
if cursor.fetchone(): if cursor.fetchone():
cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE key = %%s" % self._table, [encoded, str(exp), key]) cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key])
else: else:
cursor.execute("INSERT INTO %s (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, str(exp)])
db.commit() db.commit()
def delete(self, key): def delete(self, key):
cursor = db.cursor() cursor = db.cursor()
cursor.execute("DELETE FROM %s WHERE key = %%s" % self._table, [key]) cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key])
db.commit() db.commit()
def has_key(self, key): def has_key(self, key):
cursor = db.cursor() cursor = db.cursor()
cursor.execute("SELECT key FROM %s WHERE key = %%s" % self._table, [key]) cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key])
return cursor.fetchone() is not None return cursor.fetchone() is not None
def _cull(self, cursor, now): def _cull(self, cursor, now):
@ -424,8 +424,8 @@ class _DBCache(_Cache):
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:
cursor.execute("SELECT key FROM %s ORDER BY key LIMIT 1 OFFSET %%s" % self._table, [num / self._cull_frequency]) cursor.execute("SELECT cache_key FROM %s ORDER BY cache_key LIMIT 1 OFFSET %%s" % self._table, [num / self._cull_frequency])
cursor.execute("DELETE FROM %s WHERE key < %%s" % self._table, [cursor.fetchone()[0]]) cursor.execute("DELETE FROM %s WHERE cache_key < %%s" % self._table, [cursor.fetchone()[0]])
########################################## ##########################################
# Read settings and load a cache backend # # Read settings and load a cache backend #

View File

@ -626,7 +626,8 @@ def createcachetable(tablename):
"Creates the table needed to use the SQL cache backend" "Creates the table needed to use the SQL cache backend"
from django.core import db, meta from django.core import db, meta
fields = ( fields = (
meta.CharField(name='key', maxlength=255, unique=True, primary_key=True), # "key" is a reserved word in MySQL, so use "cache_key" instead.
meta.CharField(name='cache_key', maxlength=255, unique=True, primary_key=True),
meta.TextField(name='value'), meta.TextField(name='value'),
meta.DateTimeField(name='expires', db_index=True), meta.DateTimeField(name='expires', db_index=True),
) )