2006-02-24 14:07:01 +08:00
|
|
|
"Database cache backend."
|
2011-07-13 17:35:51 +08:00
|
|
|
import base64
|
2006-02-24 14:07:01 +08:00
|
|
|
from datetime import datetime
|
2011-07-13 17:35:51 +08:00
|
|
|
|
2006-02-24 14:07:01 +08:00
|
|
|
try:
|
2012-07-20 22:16:57 +08:00
|
|
|
from django.utils.six.moves import cPickle as pickle
|
2006-02-24 14:07:01 +08:00
|
|
|
except ImportError:
|
|
|
|
import pickle
|
|
|
|
|
2011-11-20 18:17:12 +08:00
|
|
|
from django.conf import settings
|
2013-05-18 18:54:59 +08:00
|
|
|
from django.core.cache.backends.base import BaseCache, DEFAULT_TIMEOUT
|
2013-03-12 05:16:37 +08:00
|
|
|
from django.db import connections, transaction, router, DatabaseError
|
2013-09-23 23:04:48 +08:00
|
|
|
from django.db.backends.utils import typecast_timestamp
|
2012-11-11 00:39:59 +08:00
|
|
|
from django.utils import timezone, six
|
2012-08-29 02:59:56 +08:00
|
|
|
from django.utils.encoding import force_bytes
|
2011-07-13 17:35:51 +08:00
|
|
|
|
|
|
|
|
2010-08-05 10:13:32 +08:00
|
|
|
class Options(object):
|
|
|
|
"""A class that will quack like a Django model _meta class.
|
|
|
|
|
|
|
|
This allows cache operations to be controlled by the router
|
|
|
|
"""
|
|
|
|
def __init__(self, table):
|
|
|
|
self.db_table = table
|
|
|
|
self.app_label = 'django_cache'
|
2013-02-05 17:16:07 +08:00
|
|
|
self.model_name = 'cacheentry'
|
2010-08-05 10:13:32 +08:00
|
|
|
self.verbose_name = 'cache entry'
|
|
|
|
self.verbose_name_plural = 'cache entries'
|
2013-03-12 05:16:37 +08:00
|
|
|
self.object_name = 'CacheEntry'
|
2010-08-05 10:13:32 +08:00
|
|
|
self.abstract = False
|
|
|
|
self.managed = True
|
|
|
|
self.proxy = False
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
class BaseDatabaseCache(BaseCache):
|
|
|
|
def __init__(self, table, params):
|
|
|
|
BaseCache.__init__(self, params)
|
2010-08-05 10:13:32 +08:00
|
|
|
self._table = table
|
|
|
|
|
|
|
|
class CacheEntry(object):
|
|
|
|
_meta = Options(table)
|
|
|
|
self.cache_model_class = CacheEntry
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
class DatabaseCache(BaseDatabaseCache):
|
2011-11-20 18:17:12 +08:00
|
|
|
|
|
|
|
# This class uses cursors provided by the database connection. This means
|
|
|
|
# it reads expiration values as aware or naive datetimes depending on the
|
|
|
|
# value of USE_TZ. They must be compared to aware or naive representations
|
|
|
|
# of "now" respectively.
|
|
|
|
|
|
|
|
# But it bypasses the ORM for write operations. As a consequence, aware
|
|
|
|
# datetimes aren't made naive for databases that don't support time zones.
|
|
|
|
# We work around this problem by always using naive datetimes when writing
|
|
|
|
# expiration values, in UTC when USE_TZ = True and in local time otherwise.
|
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def get(self, key, default=None, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2010-08-05 10:13:32 +08:00
|
|
|
db = router.db_for_read(self.cache_model_class)
|
|
|
|
table = connections[db].ops.quote_name(self._table)
|
|
|
|
cursor = connections[db].cursor()
|
|
|
|
|
2011-11-20 18:17:12 +08:00
|
|
|
cursor.execute("SELECT cache_key, value, expires FROM %s "
|
|
|
|
"WHERE cache_key = %%s" % table, [key])
|
2006-02-24 14:07:01 +08:00
|
|
|
row = cursor.fetchone()
|
|
|
|
if row is None:
|
|
|
|
return default
|
2011-11-20 18:17:12 +08:00
|
|
|
now = timezone.now()
|
2013-09-23 23:04:48 +08:00
|
|
|
expires = row[2]
|
|
|
|
if connections[db].features.needs_datetime_string_cast and not isinstance(expires, datetime):
|
|
|
|
# Note: typecasting is needed by some 3rd party database backends.
|
|
|
|
# All core backends work without typecasting, so be careful about
|
|
|
|
# changes here - test suite will NOT pick regressions here.
|
|
|
|
expires = typecast_timestamp(str(expires))
|
|
|
|
if expires < now:
|
2010-08-05 10:13:32 +08:00
|
|
|
db = router.db_for_write(self.cache_model_class)
|
|
|
|
cursor = connections[db].cursor()
|
2011-11-20 18:17:12 +08:00
|
|
|
cursor.execute("DELETE FROM %s "
|
|
|
|
"WHERE cache_key = %%s" % table, [key])
|
2006-02-24 14:07:01 +08:00
|
|
|
return default
|
2010-08-05 10:13:32 +08:00
|
|
|
value = connections[db].ops.process_clob(row[1])
|
2012-08-29 02:59:56 +08:00
|
|
|
return pickle.loads(base64.b64decode(force_bytes(value)))
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2013-05-18 18:54:59 +08:00
|
|
|
def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
|
2010-11-19 23:39:35 +08:00
|
|
|
key = self.make_key(key, version=version)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2008-08-10 11:52:21 +08:00
|
|
|
self._base_set('set', key, value, timeout)
|
2007-10-20 23:16:34 +08:00
|
|
|
|
2013-05-18 18:54:59 +08:00
|
|
|
def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
|
2010-11-19 23:39:35 +08:00
|
|
|
key = self.make_key(key, version=version)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2007-10-22 03:19:32 +08:00
|
|
|
return self._base_set('add', key, value, timeout)
|
|
|
|
|
2013-05-18 18:54:59 +08:00
|
|
|
def _base_set(self, mode, key, value, timeout=DEFAULT_TIMEOUT):
|
2013-09-24 00:40:19 +08:00
|
|
|
timeout = self.get_backend_timeout(timeout)
|
2010-08-05 10:13:32 +08:00
|
|
|
db = router.db_for_write(self.cache_model_class)
|
|
|
|
table = connections[db].ops.quote_name(self._table)
|
|
|
|
cursor = connections[db].cursor()
|
|
|
|
|
|
|
|
cursor.execute("SELECT COUNT(*) FROM %s" % table)
|
2006-02-24 14:07:01 +08:00
|
|
|
num = cursor.fetchone()[0]
|
2011-11-20 18:17:12 +08:00
|
|
|
now = timezone.now()
|
|
|
|
now = now.replace(microsecond=0)
|
2013-05-18 18:54:59 +08:00
|
|
|
if timeout is None:
|
|
|
|
exp = datetime.max
|
|
|
|
elif settings.USE_TZ:
|
2013-09-24 00:40:19 +08:00
|
|
|
exp = datetime.utcfromtimestamp(timeout)
|
2011-11-20 18:17:12 +08:00
|
|
|
else:
|
2013-09-24 00:40:19 +08:00
|
|
|
exp = datetime.fromtimestamp(timeout)
|
2011-11-20 18:17:12 +08:00
|
|
|
exp = exp.replace(microsecond=0)
|
2006-02-24 14:07:01 +08:00
|
|
|
if num > self._max_entries:
|
2010-08-05 10:13:32 +08:00
|
|
|
self._cull(db, cursor, now)
|
2011-11-22 06:25:49 +08:00
|
|
|
pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
|
2012-11-11 00:39:59 +08:00
|
|
|
b64encoded = base64.b64encode(pickled)
|
|
|
|
# The DB column is expecting a string, so make sure the value is a
|
|
|
|
# string, not bytes. Refs #19274.
|
|
|
|
if six.PY3:
|
|
|
|
b64encoded = b64encoded.decode('latin1')
|
2006-02-24 14:07:01 +08:00
|
|
|
try:
|
2013-09-23 23:04:48 +08:00
|
|
|
# Note: typecasting for datetimes is needed by some 3rd party
|
|
|
|
# database backends. All core backends work without typecasting,
|
|
|
|
# so be careful about changes here - test suite will NOT pick
|
|
|
|
# regressions.
|
2013-03-13 21:08:32 +08:00
|
|
|
with transaction.atomic(using=db):
|
2013-03-12 05:16:37 +08:00
|
|
|
cursor.execute("SELECT cache_key, expires FROM %s "
|
|
|
|
"WHERE cache_key = %%s" % table, [key])
|
|
|
|
result = cursor.fetchone()
|
2013-09-23 23:04:48 +08:00
|
|
|
if result:
|
|
|
|
current_expires = result[1]
|
|
|
|
if (connections[db].features.needs_datetime_string_cast and not
|
|
|
|
isinstance(current_expires, datetime)):
|
|
|
|
current_expires = typecast_timestamp(str(current_expires))
|
2013-03-12 05:16:37 +08:00
|
|
|
exp = connections[db].ops.value_to_db_datetime(exp)
|
2013-09-23 23:04:48 +08:00
|
|
|
if result and (mode == 'set' or (mode == 'add' and current_expires < now)):
|
2013-03-12 05:16:37 +08:00
|
|
|
cursor.execute("UPDATE %s SET value = %%s, expires = %%s "
|
|
|
|
"WHERE cache_key = %%s" % table,
|
|
|
|
[b64encoded, exp, key])
|
|
|
|
else:
|
|
|
|
cursor.execute("INSERT INTO %s (cache_key, value, expires) "
|
|
|
|
"VALUES (%%s, %%s, %%s)" % table,
|
|
|
|
[key, b64encoded, exp])
|
2006-02-24 14:07:01 +08:00
|
|
|
except DatabaseError:
|
|
|
|
# To be threadsafe, updates/inserts are allowed to fail silently
|
2008-08-10 11:52:21 +08:00
|
|
|
return False
|
2006-02-24 14:07:01 +08:00
|
|
|
else:
|
2008-08-10 11:52:21 +08:00
|
|
|
return True
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def delete(self, key, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2010-11-19 23:39:35 +08:00
|
|
|
|
2010-08-05 10:13:32 +08:00
|
|
|
db = router.db_for_write(self.cache_model_class)
|
|
|
|
table = connections[db].ops.quote_name(self._table)
|
|
|
|
cursor = connections[db].cursor()
|
|
|
|
|
|
|
|
cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % table, [key])
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2010-11-19 23:39:35 +08:00
|
|
|
def has_key(self, key, version=None):
|
|
|
|
key = self.make_key(key, version=version)
|
2010-09-13 02:45:26 +08:00
|
|
|
self.validate_key(key)
|
2010-11-19 23:39:35 +08:00
|
|
|
|
2010-08-05 10:13:32 +08:00
|
|
|
db = router.db_for_read(self.cache_model_class)
|
|
|
|
table = connections[db].ops.quote_name(self._table)
|
|
|
|
cursor = connections[db].cursor()
|
|
|
|
|
2011-11-20 18:17:12 +08:00
|
|
|
if settings.USE_TZ:
|
|
|
|
now = datetime.utcnow()
|
|
|
|
else:
|
|
|
|
now = datetime.now()
|
|
|
|
now = now.replace(microsecond=0)
|
|
|
|
cursor.execute("SELECT cache_key FROM %s "
|
|
|
|
"WHERE cache_key = %%s and expires > %%s" % table,
|
2010-08-05 10:13:32 +08:00
|
|
|
[key, connections[db].ops.value_to_db_datetime(now)])
|
2006-02-24 14:07:01 +08:00
|
|
|
return cursor.fetchone() is not None
|
|
|
|
|
2010-08-05 10:13:32 +08:00
|
|
|
def _cull(self, db, cursor, now):
|
2006-02-24 14:07:01 +08:00
|
|
|
if self._cull_frequency == 0:
|
2010-01-27 16:21:35 +08:00
|
|
|
self.clear()
|
2006-02-24 14:07:01 +08:00
|
|
|
else:
|
2011-11-20 18:17:12 +08:00
|
|
|
# When USE_TZ is True, 'now' will be an aware datetime in UTC.
|
|
|
|
now = now.replace(tzinfo=None)
|
2010-08-31 08:44:46 +08:00
|
|
|
table = connections[db].ops.quote_name(self._table)
|
2010-08-05 10:13:32 +08:00
|
|
|
cursor.execute("DELETE FROM %s WHERE expires < %%s" % table,
|
|
|
|
[connections[db].ops.value_to_db_datetime(now)])
|
|
|
|
cursor.execute("SELECT COUNT(*) FROM %s" % table)
|
2006-02-24 14:07:01 +08:00
|
|
|
num = cursor.fetchone()[0]
|
|
|
|
if num > self._max_entries:
|
2012-08-18 17:15:05 +08:00
|
|
|
cull_num = num // self._cull_frequency
|
2012-07-05 22:20:48 +08:00
|
|
|
cursor.execute(
|
|
|
|
connections[db].ops.cache_key_culling_sql() % table,
|
|
|
|
[cull_num])
|
2011-11-20 18:17:12 +08:00
|
|
|
cursor.execute("DELETE FROM %s "
|
|
|
|
"WHERE cache_key < %%s" % table,
|
|
|
|
[cursor.fetchone()[0]])
|
2010-01-27 16:21:35 +08:00
|
|
|
|
|
|
|
def clear(self):
|
2010-08-05 10:13:32 +08:00
|
|
|
db = router.db_for_write(self.cache_model_class)
|
|
|
|
table = connections[db].ops.quote_name(self._table)
|
|
|
|
cursor = connections[db].cursor()
|
|
|
|
cursor.execute('DELETE FROM %s' % table)
|
2010-12-21 23:19:19 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
# For backwards compatibility
|
|
|
|
class CacheClass(DatabaseCache):
|
|
|
|
pass
|