django-cachalot/cachalot/monkey_patch.py

198 lines
5.9 KiB
Python
Raw Normal View History

2014-09-26 14:53:44 +00:00
# coding: utf-8
from __future__ import unicode_literals
from collections import Iterable
from functools import wraps
from time import time
from django import VERSION as django_version
2014-10-20 21:43:10 +00:00
from django.conf import settings
2014-11-04 00:38:51 +00:00
if django_version >= (1, 7):
from django.db.backends.utils import CursorWrapper
else:
from django.db.backends.util import CursorWrapper
2014-09-26 14:53:44 +00:00
from django.db.models.query import EmptyResultSet
if django_version >= (1, 7):
from django.db.models.signals import post_migrate
else:
from django.db.models.signals import post_syncdb as post_migrate
2014-09-26 14:53:44 +00:00
from django.db.models.sql.compiler import (
2014-10-29 17:19:00 +00:00
SQLCompiler, SQLInsertCompiler, SQLUpdateCompiler, SQLDeleteCompiler)
2014-10-20 21:43:10 +00:00
from django.db.transaction import Atomic, get_connection
2014-10-21 19:47:50 +00:00
from django.test import TransactionTestCase
from .api import invalidate_all, invalidate_tables
from .cache import cachalot_caches
from .settings import cachalot_settings
from .utils import (
_get_query_cache_key, _invalidate_tables,
_get_table_cache_keys, _get_tables_from_sql, RandomQueryException)
2014-09-26 14:53:44 +00:00
WRITE_COMPILERS = (SQLInsertCompiler, SQLUpdateCompiler, SQLDeleteCompiler)
PATCHED = False
2014-11-04 00:17:35 +00:00
def is_patched():
return PATCHED
2014-09-26 14:53:44 +00:00
2014-11-04 00:17:35 +00:00
def _unset_raw_connection(original):
def inner(compiler, *args, **kwargs):
compiler.connection.raw = False
out = original(compiler, *args, **kwargs)
compiler.connection.raw = True
return out
return inner
2014-09-26 14:53:44 +00:00
def _get_result_or_execute_query(execute_query_func, cache_key,
table_cache_keys):
cache = cachalot_caches.get_cache()
data = cache.get_many(table_cache_keys + [cache_key])
new_table_cache_keys = set(table_cache_keys)
new_table_cache_keys.difference_update(data)
if new_table_cache_keys:
2014-12-13 18:13:22 +00:00
now = time()
d = {}
for k in new_table_cache_keys:
d[k] = now
cache.set_many(d, None)
elif cache_key in data:
2014-12-14 07:35:52 +00:00
timestamp, result = data.pop(cache_key)
table_times = data.values()
if table_times and timestamp > max(table_times):
return result
result = execute_query_func()
if isinstance(result, Iterable) \
and not isinstance(result, (tuple, list)):
result = list(result)
cache.set(cache_key, (time(), result), None)
return result
2014-11-04 00:17:35 +00:00
def _patch_compiler(original):
@wraps(original)
@_unset_raw_connection
def inner(compiler, *args, **kwargs):
execute_query_func = lambda: original(compiler, *args, **kwargs)
2014-11-04 00:17:35 +00:00
if not cachalot_settings.CACHALOT_ENABLED \
or isinstance(compiler, WRITE_COMPILERS):
return execute_query_func()
2014-11-04 00:17:35 +00:00
try:
cache_key = _get_query_cache_key(compiler)
table_cache_keys = _get_table_cache_keys(compiler)
except (EmptyResultSet, RandomQueryException):
return execute_query_func()
2014-11-04 00:17:35 +00:00
return _get_result_or_execute_query(
execute_query_func, cache_key, table_cache_keys)
2014-11-04 00:17:35 +00:00
inner.original = original
return inner
def _patch_write_compiler(original):
@wraps(original)
def inner(compiler, *args, **kwargs):
_invalidate_tables(cachalot_caches.get_cache(), compiler)
return original(compiler, *args, **kwargs)
inner.original = original
return inner
def _patch_orm():
SQLCompiler.execute_sql = _patch_compiler(SQLCompiler.execute_sql)
for compiler in WRITE_COMPILERS:
compiler.execute_sql = _patch_write_compiler(compiler.execute_sql)
def _patch_cursor():
def _patch_cursor_execute(original):
@wraps(original)
2014-12-07 01:44:50 +00:00
def inner(cursor, sql, *args, **kwargs):
out = original(cursor, sql, *args, **kwargs)
2014-11-04 00:17:35 +00:00
if getattr(cursor.db, 'raw', True) \
and cachalot_settings.CACHALOT_INVALIDATE_RAW:
2014-12-07 01:44:50 +00:00
sql = sql.lower()
if 'update' in sql or 'insert' in sql or 'delete' in sql:
tables = _get_tables_from_sql(cursor.db, sql)
2014-11-04 00:17:35 +00:00
invalidate_tables(tables, db_alias=cursor.db.alias)
return out
2014-09-29 17:16:53 +00:00
inner.original = original
2014-09-26 14:53:44 +00:00
return inner
2014-11-04 00:17:35 +00:00
CursorWrapper.execute = _patch_cursor_execute(CursorWrapper.execute)
2014-12-07 01:44:50 +00:00
CursorWrapper.executemany = _patch_cursor_execute(CursorWrapper.executemany)
2014-09-26 14:53:44 +00:00
2014-09-29 17:16:53 +00:00
def _patch_atomic():
def patch_enter(original):
@wraps(original)
2014-09-29 17:16:53 +00:00
def inner(self):
cachalot_caches.enter_atomic()
2014-09-29 17:16:53 +00:00
original(self)
inner.original = original
return inner
def patch_exit(original):
@wraps(original)
2014-09-29 17:16:53 +00:00
def inner(self, exc_type, exc_value, traceback):
2014-10-20 21:43:10 +00:00
needs_rollback = get_connection(self.using).needs_rollback
original(self, exc_type, exc_value, traceback)
cachalot_caches.exit_atomic(exc_type is None
and not needs_rollback)
2014-09-29 17:16:53 +00:00
inner.original = original
return inner
Atomic.__enter__ = patch_enter(Atomic.__enter__)
Atomic.__exit__ = patch_exit(Atomic.__exit__)
2014-10-21 19:47:50 +00:00
def _patch_tests():
def patch_transaction_test_case(original):
@wraps(original)
def inner(*args, **kwargs):
out = original(*args, **kwargs)
invalidate_all()
2014-10-21 19:47:50 +00:00
return out
inner.original = original
return inner
TransactionTestCase._fixture_setup = patch_transaction_test_case(
TransactionTestCase._fixture_setup)
def _invalidate_on_migration(sender, **kwargs):
db_alias = kwargs['using'] if django_version >= (1, 7) else kwargs['db']
invalidate_all(db_alias=db_alias)
2014-09-29 17:16:53 +00:00
def patch():
2014-09-26 14:53:44 +00:00
global PATCHED
post_migrate.connect(_invalidate_on_migration)
if 'south' in settings.INSTALLED_APPS:
from south.signals import post_migrate as south_post_migrate
south_post_migrate.connect(_invalidate_on_migration)
2014-11-04 00:17:35 +00:00
_patch_cursor()
2014-10-21 19:47:50 +00:00
_patch_tests()
_patch_atomic()
2014-11-04 00:17:35 +00:00
_patch_orm()
2014-09-26 14:53:44 +00:00
PATCHED = True