Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. "Database cache backend."
  2. import base64
  3. import pickle
  4. from datetime import datetime
  5. from django.conf import settings
  6. from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache
  7. from django.db import DatabaseError, connections, models, router, transaction
  8. from django.utils import timezone
  9. from django.utils.inspect import func_supports_parameter
  10. class Options:
  11. """A class that will quack like a Django model _meta class.
  12. This allows cache operations to be controlled by the router
  13. """
  14. def __init__(self, table):
  15. self.db_table = table
  16. self.app_label = 'django_cache'
  17. self.model_name = 'cacheentry'
  18. self.verbose_name = 'cache entry'
  19. self.verbose_name_plural = 'cache entries'
  20. self.object_name = 'CacheEntry'
  21. self.abstract = False
  22. self.managed = True
  23. self.proxy = False
  24. self.swapped = False
  25. class BaseDatabaseCache(BaseCache):
  26. def __init__(self, table, params):
  27. super().__init__(params)
  28. self._table = table
  29. class CacheEntry:
  30. _meta = Options(table)
  31. self.cache_model_class = CacheEntry
  32. class DatabaseCache(BaseDatabaseCache):
  33. # This class uses cursors provided by the database connection. This means
  34. # it reads expiration values as aware or naive datetimes, depending on the
  35. # value of USE_TZ and whether the database supports time zones. The ORM's
  36. # conversion and adaptation infrastructure is then used to avoid comparing
  37. # aware and naive datetimes accidentally.
  38. def get(self, key, default=None, version=None):
  39. key = self.make_key(key, version=version)
  40. self.validate_key(key)
  41. db = router.db_for_read(self.cache_model_class)
  42. connection = connections[db]
  43. quote_name = connection.ops.quote_name
  44. table = quote_name(self._table)
  45. with connection.cursor() as cursor:
  46. cursor.execute(
  47. 'SELECT %s, %s, %s FROM %s WHERE %s = %%s' % (
  48. quote_name('cache_key'),
  49. quote_name('value'),
  50. quote_name('expires'),
  51. table,
  52. quote_name('cache_key'),
  53. ),
  54. [key]
  55. )
  56. row = cursor.fetchone()
  57. if row is None:
  58. return default
  59. expires = row[2]
  60. expression = models.Expression(output_field=models.DateTimeField())
  61. for converter in (connection.ops.get_db_converters(expression) +
  62. expression.get_db_converters(connection)):
  63. if func_supports_parameter(converter, 'context'): # RemovedInDjango30Warning
  64. expires = converter(expires, expression, connection, {})
  65. else:
  66. expires = converter(expires, expression, connection)
  67. if expires < timezone.now():
  68. db = router.db_for_write(self.cache_model_class)
  69. connection = connections[db]
  70. with connection.cursor() as cursor:
  71. cursor.execute(
  72. 'DELETE FROM %s WHERE %s = %%s' % (
  73. table,
  74. quote_name('cache_key'),
  75. ),
  76. [key]
  77. )
  78. return default
  79. value = connection.ops.process_clob(row[1])
  80. return pickle.loads(base64.b64decode(value.encode()))
  81. def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
  82. key = self.make_key(key, version=version)
  83. self.validate_key(key)
  84. self._base_set('set', key, value, timeout)
  85. def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
  86. key = self.make_key(key, version=version)
  87. self.validate_key(key)
  88. return self._base_set('add', key, value, timeout)
  89. def touch(self, key, timeout=DEFAULT_TIMEOUT, version=None):
  90. key = self.make_key(key, version=version)
  91. self.validate_key(key)
  92. return self._base_set('touch', key, None, timeout)
  93. def _base_set(self, mode, key, value, timeout=DEFAULT_TIMEOUT):
  94. timeout = self.get_backend_timeout(timeout)
  95. db = router.db_for_write(self.cache_model_class)
  96. connection = connections[db]
  97. quote_name = connection.ops.quote_name
  98. table = quote_name(self._table)
  99. with connection.cursor() as cursor:
  100. cursor.execute("SELECT COUNT(*) FROM %s" % table)
  101. num = cursor.fetchone()[0]
  102. now = timezone.now()
  103. now = now.replace(microsecond=0)
  104. if timeout is None:
  105. exp = datetime.max
  106. elif settings.USE_TZ:
  107. exp = datetime.utcfromtimestamp(timeout)
  108. else:
  109. exp = datetime.fromtimestamp(timeout)
  110. exp = exp.replace(microsecond=0)
  111. if num > self._max_entries:
  112. self._cull(db, cursor, now)
  113. pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
  114. # The DB column is expecting a string, so make sure the value is a
  115. # string, not bytes. Refs #19274.
  116. b64encoded = base64.b64encode(pickled).decode('latin1')
  117. try:
  118. # Note: typecasting for datetimes is needed by some 3rd party
  119. # database backends. All core backends work without typecasting,
  120. # so be careful about changes here - test suite will NOT pick
  121. # regressions.
  122. with transaction.atomic(using=db):
  123. cursor.execute(
  124. 'SELECT %s, %s FROM %s WHERE %s = %%s' % (
  125. quote_name('cache_key'),
  126. quote_name('expires'),
  127. table,
  128. quote_name('cache_key'),
  129. ),
  130. [key]
  131. )
  132. result = cursor.fetchone()
  133. if result:
  134. current_expires = result[1]
  135. expression = models.Expression(output_field=models.DateTimeField())
  136. for converter in (connection.ops.get_db_converters(expression) +
  137. expression.get_db_converters(connection)):
  138. if func_supports_parameter(converter, 'context'): # RemovedInDjango30Warning
  139. current_expires = converter(current_expires, expression, connection, {})
  140. else:
  141. current_expires = converter(current_expires, expression, connection)
  142. exp = connection.ops.adapt_datetimefield_value(exp)
  143. if result and mode == 'touch':
  144. cursor.execute(
  145. 'UPDATE %s SET %s = %%s WHERE %s = %%s' % (
  146. table,
  147. quote_name('expires'),
  148. quote_name('cache_key')
  149. ),
  150. [exp, key]
  151. )
  152. elif result and (mode == 'set' or (mode == 'add' and current_expires < now)):
  153. cursor.execute(
  154. 'UPDATE %s SET %s = %%s, %s = %%s WHERE %s = %%s' % (
  155. table,
  156. quote_name('value'),
  157. quote_name('expires'),
  158. quote_name('cache_key'),
  159. ),
  160. [b64encoded, exp, key]
  161. )
  162. elif mode != 'touch':
  163. cursor.execute(
  164. 'INSERT INTO %s (%s, %s, %s) VALUES (%%s, %%s, %%s)' % (
  165. table,
  166. quote_name('cache_key'),
  167. quote_name('value'),
  168. quote_name('expires'),
  169. ),
  170. [key, b64encoded, exp]
  171. )
  172. else:
  173. return False # touch failed.
  174. except DatabaseError:
  175. # To be threadsafe, updates/inserts are allowed to fail silently
  176. return False
  177. else:
  178. return True
  179. def delete(self, key, version=None):
  180. key = self.make_key(key, version=version)
  181. self.validate_key(key)
  182. db = router.db_for_write(self.cache_model_class)
  183. connection = connections[db]
  184. table = connection.ops.quote_name(self._table)
  185. with connection.cursor() as cursor:
  186. cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % table, [key])
  187. def has_key(self, key, version=None):
  188. key = self.make_key(key, version=version)
  189. self.validate_key(key)
  190. db = router.db_for_read(self.cache_model_class)
  191. connection = connections[db]
  192. quote_name = connection.ops.quote_name
  193. if settings.USE_TZ:
  194. now = datetime.utcnow()
  195. else:
  196. now = datetime.now()
  197. now = now.replace(microsecond=0)
  198. with connection.cursor() as cursor:
  199. cursor.execute(
  200. 'SELECT %s FROM %s WHERE %s = %%s and expires > %%s' % (
  201. quote_name('cache_key'),
  202. quote_name(self._table),
  203. quote_name('cache_key'),
  204. ),
  205. [key, connection.ops.adapt_datetimefield_value(now)]
  206. )
  207. return cursor.fetchone() is not None
  208. def _cull(self, db, cursor, now):
  209. if self._cull_frequency == 0:
  210. self.clear()
  211. else:
  212. connection = connections[db]
  213. table = connection.ops.quote_name(self._table)
  214. cursor.execute("DELETE FROM %s WHERE expires < %%s" % table,
  215. [connection.ops.adapt_datetimefield_value(now)])
  216. cursor.execute("SELECT COUNT(*) FROM %s" % table)
  217. num = cursor.fetchone()[0]
  218. if num > self._max_entries:
  219. cull_num = num // self._cull_frequency
  220. cursor.execute(
  221. connection.ops.cache_key_culling_sql() % table,
  222. [cull_num])
  223. cursor.execute("DELETE FROM %s "
  224. "WHERE cache_key < %%s" % table,
  225. [cursor.fetchone()[0]])
  226. def clear(self):
  227. db = router.db_for_write(self.cache_model_class)
  228. connection = connections[db]
  229. table = connection.ops.quote_name(self._table)
  230. with connection.cursor() as cursor:
  231. cursor.execute('DELETE FROM %s' % table)