2014-09-26 14:53:44 +00:00
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
from collections import Iterable
|
2014-09-27 23:44:43 +00:00
|
|
|
|
import re
|
|
|
|
|
|
|
2014-09-29 17:16:53 +00:00
|
|
|
|
from django.core.cache import cache as django_cache
|
2014-09-29 17:54:08 +00:00
|
|
|
|
from django.db import connection
|
2014-09-26 14:53:44 +00:00
|
|
|
|
from django.db.models.query import EmptyResultSet
|
|
|
|
|
|
from django.db.models.sql.compiler import (
|
|
|
|
|
|
SQLCompiler, SQLAggregateCompiler, SQLDateCompiler, SQLDateTimeCompiler,
|
|
|
|
|
|
SQLInsertCompiler, SQLUpdateCompiler, SQLDeleteCompiler)
|
2014-09-27 23:44:43 +00:00
|
|
|
|
from django.db.models.sql.where import ExtraWhere
|
2014-09-29 17:16:53 +00:00
|
|
|
|
from django.db.transaction import Atomic
|
2014-09-26 14:53:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
COMPILERS = (SQLCompiler,
|
|
|
|
|
|
SQLAggregateCompiler, SQLDateCompiler, SQLDateTimeCompiler,
|
|
|
|
|
|
SQLInsertCompiler, SQLUpdateCompiler, SQLDeleteCompiler)
|
|
|
|
|
|
WRITE_COMPILERS = (SQLInsertCompiler, SQLUpdateCompiler, SQLDeleteCompiler)
|
|
|
|
|
|
READ_COMPILERS = [c for c in COMPILERS if c not in WRITE_COMPILERS]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PATCHED = False
|
2014-09-29 17:16:53 +00:00
|
|
|
|
MISS_VALUE = '[[Missing cache key]]'
|
2014-09-26 14:53:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
2014-09-27 23:44:43 +00:00
|
|
|
|
def _get_tables(query):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Returns a ``set`` of all database table names used by ``query``.
|
|
|
|
|
|
"""
|
2014-09-28 17:36:49 +00:00
|
|
|
|
tables = set(query.tables)
|
2014-09-27 23:44:43 +00:00
|
|
|
|
tables.add(query.model._meta.db_table)
|
|
|
|
|
|
return tables
|
|
|
|
|
|
|
2014-09-26 14:53:44 +00:00
|
|
|
|
|
2014-09-27 23:44:43 +00:00
|
|
|
|
def _get_tables_cache_keys(query):
|
|
|
|
|
|
return ['%s_queries' % t for t in _get_tables(query)]
|
2014-09-26 14:53:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
2014-09-29 17:16:53 +00:00
|
|
|
|
def _update_tables_queries(cache, query, cache_key):
|
2014-09-27 23:44:43 +00:00
|
|
|
|
tables_cache_keys = _get_tables_cache_keys(query)
|
2014-09-26 14:53:44 +00:00
|
|
|
|
tables_queries = cache.get_many(tables_cache_keys)
|
|
|
|
|
|
for k in tables_cache_keys:
|
|
|
|
|
|
queries = tables_queries.get(k, [])
|
|
|
|
|
|
queries.append(cache_key)
|
|
|
|
|
|
tables_queries[k] = queries
|
|
|
|
|
|
cache.set_many(tables_queries)
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-29 17:16:53 +00:00
|
|
|
|
def _invalidate_tables(cache, query):
|
2014-09-27 23:44:43 +00:00
|
|
|
|
tables_cache_keys = _get_tables_cache_keys(query)
|
2014-09-26 14:53:44 +00:00
|
|
|
|
tables_queries = cache.get_many(tables_cache_keys)
|
|
|
|
|
|
queries = []
|
|
|
|
|
|
for k in tables_cache_keys:
|
|
|
|
|
|
queries.extend(tables_queries.get(k, []))
|
|
|
|
|
|
cache.delete_many(queries)
|
|
|
|
|
|
cache.delete_many(tables_cache_keys)
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-27 23:44:43 +00:00
|
|
|
|
COLUMN_RE = re.compile(r'^"(?P<table>[\w_]+)"\."(?P<column>[\w_]+)"$')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _has_extra_select_or_where(query):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Returns True if ``query`` contains a ``QuerySet.extra`` with ``select``
|
|
|
|
|
|
or ``where`` arguments.
|
|
|
|
|
|
|
|
|
|
|
|
We also have to check for ``prefetch_related``, as it internally uses a
|
|
|
|
|
|
``QuerySet.extra(select={'_prefetch_related_val_…': '"table"."column"'})``.
|
|
|
|
|
|
|
|
|
|
|
|
For more details on how prefetch_related uses ``QuerySet.extra``, see:
|
|
|
|
|
|
https://github.com/django/django/blob/1.6.7/django/db/models/fields/related.py#L553-L577
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
# Checks if there’s an extra where
|
|
|
|
|
|
if any(isinstance(child, ExtraWhere) for child in query.where.children):
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# Checks if there’s an extra select
|
|
|
|
|
|
if query.extra_select and query.extra_select_mask is None:
|
|
|
|
|
|
tables = _get_tables(query)
|
|
|
|
|
|
# Checks if extra subqueries are something else than one or several
|
|
|
|
|
|
# ``prefetch_related``.
|
|
|
|
|
|
for subquery, params in query.extra_select.values():
|
|
|
|
|
|
match = COLUMN_RE.match(subquery)
|
|
|
|
|
|
return match is None or match.group('table') not in tables
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-29 17:16:53 +00:00
|
|
|
|
TRANSACTION_CACHES = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AtomicCache(dict):
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
super(AtomicCache, self).__init__()
|
|
|
|
|
|
self.parent_cache = (TRANSACTION_CACHES[-1] if TRANSACTION_CACHES
|
|
|
|
|
|
else django_cache)
|
|
|
|
|
|
self.to_be_deleted = set()
|
|
|
|
|
|
|
|
|
|
|
|
def get(self, k, default=None):
|
|
|
|
|
|
if k in self.to_be_deleted:
|
|
|
|
|
|
return default
|
|
|
|
|
|
if k in self:
|
|
|
|
|
|
return self[k]
|
|
|
|
|
|
return self.parent_cache.get(k, default)
|
|
|
|
|
|
|
|
|
|
|
|
def set(self, k, v):
|
|
|
|
|
|
if k in self.to_be_deleted:
|
|
|
|
|
|
self.to_be_deleted.remove(k)
|
|
|
|
|
|
self[k] = v
|
|
|
|
|
|
|
|
|
|
|
|
def delete(self, k):
|
|
|
|
|
|
self.to_be_deleted.add(k)
|
|
|
|
|
|
|
|
|
|
|
|
def get_many(self, keys):
|
|
|
|
|
|
data = {}
|
|
|
|
|
|
for k in keys:
|
|
|
|
|
|
v = self.get(k, MISS_VALUE)
|
|
|
|
|
|
if v != MISS_VALUE:
|
|
|
|
|
|
data[k] = v
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def set_many(self, data):
|
|
|
|
|
|
self.to_be_deleted.difference_update(set(data))
|
|
|
|
|
|
self.update(data)
|
|
|
|
|
|
|
|
|
|
|
|
def delete_many(self, keys):
|
|
|
|
|
|
self.to_be_deleted.update(keys)
|
|
|
|
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
|
|
self.parent_cache.set_many(self)
|
|
|
|
|
|
self.parent_cache.delete_many(self.to_be_deleted)
|
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
|
return '<AtomicCache (cache=%s, to_be_deleted=%s)>' % (
|
|
|
|
|
|
super(AtomicCache, self).__repr__(),
|
|
|
|
|
|
self.to_be_deleted)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_cache():
|
|
|
|
|
|
if TRANSACTION_CACHES:
|
|
|
|
|
|
return TRANSACTION_CACHES[-1]
|
|
|
|
|
|
return django_cache
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _patch_orm_read():
|
|
|
|
|
|
def patch_execute_sql(original):
|
2014-09-26 14:53:44 +00:00
|
|
|
|
def inner(compiler, *args, **kwargs):
|
|
|
|
|
|
if isinstance(compiler, WRITE_COMPILERS):
|
2014-09-29 17:16:53 +00:00
|
|
|
|
return original(compiler, *args, **kwargs)
|
2014-09-26 14:53:44 +00:00
|
|
|
|
|
2014-09-27 23:44:43 +00:00
|
|
|
|
query = compiler.query
|
|
|
|
|
|
|
|
|
|
|
|
if _has_extra_select_or_where(query):
|
2014-09-29 17:16:53 +00:00
|
|
|
|
return original(compiler, *args, **kwargs)
|
2014-09-27 23:44:43 +00:00
|
|
|
|
|
2014-09-26 14:53:44 +00:00
|
|
|
|
try:
|
|
|
|
|
|
cache_key = compiler.as_sql()
|
|
|
|
|
|
except EmptyResultSet:
|
2014-09-29 17:16:53 +00:00
|
|
|
|
return original(compiler, *args, **kwargs)
|
2014-09-26 14:53:44 +00:00
|
|
|
|
|
2014-09-29 17:16:53 +00:00
|
|
|
|
cache = get_cache()
|
2014-09-26 14:53:44 +00:00
|
|
|
|
result = cache.get(cache_key, MISS_VALUE)
|
|
|
|
|
|
|
|
|
|
|
|
if result == MISS_VALUE:
|
2014-09-29 17:16:53 +00:00
|
|
|
|
result = original(compiler, *args, **kwargs)
|
2014-09-26 14:53:44 +00:00
|
|
|
|
if isinstance(result, Iterable) \
|
|
|
|
|
|
and not isinstance(result, (tuple, list)):
|
|
|
|
|
|
result = list(result)
|
|
|
|
|
|
|
2014-09-29 17:16:53 +00:00
|
|
|
|
_update_tables_queries(cache, query, cache_key)
|
2014-09-26 14:53:44 +00:00
|
|
|
|
|
|
|
|
|
|
cache.set(cache_key, result)
|
|
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
2014-09-29 17:16:53 +00:00
|
|
|
|
inner.original = original
|
2014-09-26 14:53:44 +00:00
|
|
|
|
return inner
|
|
|
|
|
|
|
|
|
|
|
|
for compiler in READ_COMPILERS:
|
|
|
|
|
|
compiler.execute_sql = patch_execute_sql(compiler.execute_sql)
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-29 17:16:53 +00:00
|
|
|
|
def _patch_orm_write():
|
|
|
|
|
|
def patch_execute_sql(original):
|
2014-09-26 14:53:44 +00:00
|
|
|
|
def inner(compiler, *args, **kwargs):
|
2014-09-29 17:16:53 +00:00
|
|
|
|
_invalidate_tables(get_cache(), compiler.query)
|
|
|
|
|
|
return original(compiler, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
inner.original = original
|
2014-09-26 14:53:44 +00:00
|
|
|
|
return inner
|
|
|
|
|
|
|
|
|
|
|
|
for compiler in WRITE_COMPILERS:
|
|
|
|
|
|
compiler.execute_sql = patch_execute_sql(compiler.execute_sql)
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-29 17:16:53 +00:00
|
|
|
|
def _patch_atomic():
|
|
|
|
|
|
def patch_enter(original):
|
|
|
|
|
|
def inner(self):
|
|
|
|
|
|
TRANSACTION_CACHES.append(AtomicCache())
|
|
|
|
|
|
original(self)
|
|
|
|
|
|
|
|
|
|
|
|
inner.original = original
|
|
|
|
|
|
return inner
|
|
|
|
|
|
|
|
|
|
|
|
def patch_exit(original):
|
|
|
|
|
|
def inner(self, exc_type, exc_value, traceback):
|
|
|
|
|
|
atomic_cache = TRANSACTION_CACHES.pop()
|
2014-09-29 17:54:08 +00:00
|
|
|
|
if exc_type is None and not connection.needs_rollback:
|
2014-09-29 17:16:53 +00:00
|
|
|
|
atomic_cache.commit()
|
|
|
|
|
|
|
|
|
|
|
|
original(self, exc_type, exc_value, traceback)
|
|
|
|
|
|
|
|
|
|
|
|
inner.original = original
|
|
|
|
|
|
return inner
|
|
|
|
|
|
|
|
|
|
|
|
Atomic.__enter__ = patch_enter(Atomic.__enter__)
|
|
|
|
|
|
Atomic.__exit__ = patch_exit(Atomic.__exit__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _unpatch_orm_read():
|
|
|
|
|
|
for compiler in READ_COMPILERS:
|
|
|
|
|
|
compiler.execute_sql = compiler.execute_sql.original
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _unpatch_orm_write():
|
|
|
|
|
|
for compiler in WRITE_COMPILERS:
|
|
|
|
|
|
compiler.execute_sql = compiler.execute_sql.original
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _unpatch_atomic():
|
|
|
|
|
|
Atomic.__enter__ = Atomic.__enter__.original
|
|
|
|
|
|
Atomic.__exit__ = Atomic.__exit__.original
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def patch():
|
2014-09-26 14:53:44 +00:00
|
|
|
|
global PATCHED
|
2014-09-29 17:16:53 +00:00
|
|
|
|
_patch_orm_write()
|
|
|
|
|
|
_patch_orm_read()
|
|
|
|
|
|
_patch_atomic()
|
2014-09-26 14:53:44 +00:00
|
|
|
|
PATCHED = True
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-29 17:16:53 +00:00
|
|
|
|
def unpatch():
|
|
|
|
|
|
global PATCHED
|
|
|
|
|
|
_unpatch_orm_read()
|
|
|
|
|
|
_unpatch_orm_write()
|
|
|
|
|
|
_unpatch_atomic()
|
|
|
|
|
|
PATCHED = False
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-26 14:53:44 +00:00
|
|
|
|
def is_patched():
|
|
|
|
|
|
return PATCHED
|