mirror of
https://github.com/django/django.git
synced 2025-05-05 14:37:31 +00:00
Refactored datetime handling in the database cache backend.
Took advantage of the new database adapters and converters.
This commit is contained in:
parent
2a05a82311
commit
e2a652fac1
86
django/core/cache/backends/db.py
vendored
86
django/core/cache/backends/db.py
vendored
@ -4,8 +4,7 @@ from datetime import datetime
|
|||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache
|
from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache
|
||||||
from django.db import DatabaseError, connections, router, transaction
|
from django.db import DatabaseError, connections, models, router, transaction
|
||||||
from django.db.backends.utils import typecast_timestamp
|
|
||||||
from django.utils import six, timezone
|
from django.utils import six, timezone
|
||||||
from django.utils.encoding import force_bytes
|
from django.utils.encoding import force_bytes
|
||||||
|
|
||||||
@ -46,43 +45,40 @@ class BaseDatabaseCache(BaseCache):
|
|||||||
class DatabaseCache(BaseDatabaseCache):
|
class DatabaseCache(BaseDatabaseCache):
|
||||||
|
|
||||||
# This class uses cursors provided by the database connection. This means
|
# This class uses cursors provided by the database connection. This means
|
||||||
# it reads expiration values as aware or naive datetimes depending on the
|
# 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
|
# value of USE_TZ and whether the database supports time zones. The ORM's
|
||||||
# of "now" respectively.
|
# conversion and adaptation infrastructure is then used to avoid comparing
|
||||||
|
# aware and naive datetimes accidentally.
|
||||||
# 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.
|
|
||||||
|
|
||||||
def get(self, key, default=None, version=None):
|
def get(self, key, default=None, version=None):
|
||||||
key = self.make_key(key, version=version)
|
key = self.make_key(key, version=version)
|
||||||
self.validate_key(key)
|
self.validate_key(key)
|
||||||
db = router.db_for_read(self.cache_model_class)
|
db = router.db_for_read(self.cache_model_class)
|
||||||
table = connections[db].ops.quote_name(self._table)
|
connection = connections[db]
|
||||||
|
table = connection.ops.quote_name(self._table)
|
||||||
|
|
||||||
with connections[db].cursor() as cursor:
|
with connection.cursor() as cursor:
|
||||||
cursor.execute("SELECT cache_key, value, expires FROM %s "
|
cursor.execute("SELECT cache_key, value, expires FROM %s "
|
||||||
"WHERE cache_key = %%s" % table, [key])
|
"WHERE cache_key = %%s" % table, [key])
|
||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
if row is None:
|
if row is None:
|
||||||
return default
|
return default
|
||||||
now = timezone.now()
|
|
||||||
expires = row[2]
|
expires = row[2]
|
||||||
if connections[db].features.needs_datetime_string_cast and not isinstance(expires, datetime):
|
expression = models.Expression(output_field=models.DateTimeField())
|
||||||
# Note: typecasting is needed by some 3rd party database backends.
|
for converter in (connection.ops.get_db_converters(expression) +
|
||||||
# All core backends work without typecasting, so be careful about
|
expression.get_db_converters(connection)):
|
||||||
# changes here - test suite will NOT pick regressions here.
|
expires = converter(expires, expression, connection, {})
|
||||||
expires = typecast_timestamp(str(expires))
|
|
||||||
if settings.USE_TZ:
|
if expires < timezone.now():
|
||||||
expires = expires.replace(tzinfo=timezone.utc)
|
|
||||||
if expires < now:
|
|
||||||
db = router.db_for_write(self.cache_model_class)
|
db = router.db_for_write(self.cache_model_class)
|
||||||
with connections[db].cursor() as cursor:
|
connection = connections[db]
|
||||||
|
with connection.cursor() as cursor:
|
||||||
cursor.execute("DELETE FROM %s "
|
cursor.execute("DELETE FROM %s "
|
||||||
"WHERE cache_key = %%s" % table, [key])
|
"WHERE cache_key = %%s" % table, [key])
|
||||||
return default
|
return default
|
||||||
value = connections[db].ops.process_clob(row[1])
|
|
||||||
|
value = connection.ops.process_clob(row[1])
|
||||||
return pickle.loads(base64.b64decode(force_bytes(value)))
|
return pickle.loads(base64.b64decode(force_bytes(value)))
|
||||||
|
|
||||||
def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
|
def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
|
||||||
@ -98,9 +94,10 @@ class DatabaseCache(BaseDatabaseCache):
|
|||||||
def _base_set(self, mode, key, value, timeout=DEFAULT_TIMEOUT):
|
def _base_set(self, mode, key, value, timeout=DEFAULT_TIMEOUT):
|
||||||
timeout = self.get_backend_timeout(timeout)
|
timeout = self.get_backend_timeout(timeout)
|
||||||
db = router.db_for_write(self.cache_model_class)
|
db = router.db_for_write(self.cache_model_class)
|
||||||
table = connections[db].ops.quote_name(self._table)
|
connection = connections[db]
|
||||||
|
table = connection.ops.quote_name(self._table)
|
||||||
|
|
||||||
with connections[db].cursor() as cursor:
|
with connection.cursor() as cursor:
|
||||||
cursor.execute("SELECT COUNT(*) FROM %s" % table)
|
cursor.execute("SELECT COUNT(*) FROM %s" % table)
|
||||||
num = cursor.fetchone()[0]
|
num = cursor.fetchone()[0]
|
||||||
now = timezone.now()
|
now = timezone.now()
|
||||||
@ -129,14 +126,15 @@ class DatabaseCache(BaseDatabaseCache):
|
|||||||
cursor.execute("SELECT cache_key, expires FROM %s "
|
cursor.execute("SELECT cache_key, expires FROM %s "
|
||||||
"WHERE cache_key = %%s" % table, [key])
|
"WHERE cache_key = %%s" % table, [key])
|
||||||
result = cursor.fetchone()
|
result = cursor.fetchone()
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
current_expires = result[1]
|
current_expires = result[1]
|
||||||
if (connections[db].features.needs_datetime_string_cast and not
|
expression = models.Expression(output_field=models.DateTimeField())
|
||||||
isinstance(current_expires, datetime)):
|
for converter in (connection.ops.get_db_converters(expression) +
|
||||||
current_expires = typecast_timestamp(str(current_expires))
|
expression.get_db_converters(connection)):
|
||||||
if settings.USE_TZ:
|
current_expires = converter(current_expires, expression, connection, {})
|
||||||
current_expires = current_expires.replace(tzinfo=timezone.utc)
|
|
||||||
exp = connections[db].ops.adapt_datetimefield_value(exp)
|
exp = connection.ops.adapt_datetimefield_value(exp)
|
||||||
if result and (mode == 'set' or (mode == 'add' and current_expires < now)):
|
if result and (mode == 'set' or (mode == 'add' and current_expires < now)):
|
||||||
cursor.execute("UPDATE %s SET value = %%s, expires = %%s "
|
cursor.execute("UPDATE %s SET value = %%s, expires = %%s "
|
||||||
"WHERE cache_key = %%s" % table,
|
"WHERE cache_key = %%s" % table,
|
||||||
@ -156,9 +154,10 @@ class DatabaseCache(BaseDatabaseCache):
|
|||||||
self.validate_key(key)
|
self.validate_key(key)
|
||||||
|
|
||||||
db = router.db_for_write(self.cache_model_class)
|
db = router.db_for_write(self.cache_model_class)
|
||||||
table = connections[db].ops.quote_name(self._table)
|
connection = connections[db]
|
||||||
|
table = connection.ops.quote_name(self._table)
|
||||||
|
|
||||||
with connections[db].cursor() as cursor:
|
with connection.cursor() as cursor:
|
||||||
cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % table, [key])
|
cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % table, [key])
|
||||||
|
|
||||||
def has_key(self, key, version=None):
|
def has_key(self, key, version=None):
|
||||||
@ -166,7 +165,8 @@ class DatabaseCache(BaseDatabaseCache):
|
|||||||
self.validate_key(key)
|
self.validate_key(key)
|
||||||
|
|
||||||
db = router.db_for_read(self.cache_model_class)
|
db = router.db_for_read(self.cache_model_class)
|
||||||
table = connections[db].ops.quote_name(self._table)
|
connection = connections[db]
|
||||||
|
table = connection.ops.quote_name(self._table)
|
||||||
|
|
||||||
if settings.USE_TZ:
|
if settings.USE_TZ:
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
@ -174,27 +174,26 @@ class DatabaseCache(BaseDatabaseCache):
|
|||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
now = now.replace(microsecond=0)
|
now = now.replace(microsecond=0)
|
||||||
|
|
||||||
with connections[db].cursor() as cursor:
|
with connection.cursor() as cursor:
|
||||||
cursor.execute("SELECT cache_key FROM %s "
|
cursor.execute("SELECT cache_key FROM %s "
|
||||||
"WHERE cache_key = %%s and expires > %%s" % table,
|
"WHERE cache_key = %%s and expires > %%s" % table,
|
||||||
[key, connections[db].ops.adapt_datetimefield_value(now)])
|
[key, connection.ops.adapt_datetimefield_value(now)])
|
||||||
return cursor.fetchone() is not None
|
return cursor.fetchone() is not None
|
||||||
|
|
||||||
def _cull(self, db, cursor, now):
|
def _cull(self, db, cursor, now):
|
||||||
if self._cull_frequency == 0:
|
if self._cull_frequency == 0:
|
||||||
self.clear()
|
self.clear()
|
||||||
else:
|
else:
|
||||||
# When USE_TZ is True, 'now' will be an aware datetime in UTC.
|
connection = connections[db]
|
||||||
now = now.replace(tzinfo=None)
|
table = connection.ops.quote_name(self._table)
|
||||||
table = connections[db].ops.quote_name(self._table)
|
|
||||||
cursor.execute("DELETE FROM %s WHERE expires < %%s" % table,
|
cursor.execute("DELETE FROM %s WHERE expires < %%s" % table,
|
||||||
[connections[db].ops.adapt_datetimefield_value(now)])
|
[connection.ops.adapt_datetimefield_value(now)])
|
||||||
cursor.execute("SELECT COUNT(*) FROM %s" % table)
|
cursor.execute("SELECT COUNT(*) FROM %s" % table)
|
||||||
num = cursor.fetchone()[0]
|
num = cursor.fetchone()[0]
|
||||||
if num > self._max_entries:
|
if num > self._max_entries:
|
||||||
cull_num = num // self._cull_frequency
|
cull_num = num // self._cull_frequency
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
connections[db].ops.cache_key_culling_sql() % table,
|
connection.ops.cache_key_culling_sql() % table,
|
||||||
[cull_num])
|
[cull_num])
|
||||||
cursor.execute("DELETE FROM %s "
|
cursor.execute("DELETE FROM %s "
|
||||||
"WHERE cache_key < %%s" % table,
|
"WHERE cache_key < %%s" % table,
|
||||||
@ -202,6 +201,7 @@ class DatabaseCache(BaseDatabaseCache):
|
|||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
db = router.db_for_write(self.cache_model_class)
|
db = router.db_for_write(self.cache_model_class)
|
||||||
table = connections[db].ops.quote_name(self._table)
|
connection = connections[db]
|
||||||
with connections[db].cursor() as cursor:
|
table = connection.ops.quote_name(self._table)
|
||||||
|
with connection.cursor() as cursor:
|
||||||
cursor.execute('DELETE FROM %s' % table)
|
cursor.execute('DELETE FROM %s' % table)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user