Skip to content

Commit

Permalink
Optimizes automatic invalidation.
Browse files Browse the repository at this point in the history
  • Loading branch information
BertrandBordage committed May 24, 2015
1 parent 7204803 commit a32f86f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
15 changes: 6 additions & 9 deletions cachalot/monkey_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
from django.db.models.signals import post_migrate
else:
from django.db.models.signals import post_syncdb as post_migrate
from django.db.models.sql.compiler import (
SQLCompiler, SQLInsertCompiler, SQLUpdateCompiler, SQLDeleteCompiler)
from django.db.models.sql.compiler import SQLCompiler
from django.db.transaction import Atomic, get_connection
from django.test import TransactionTestCase

Expand All @@ -26,10 +25,8 @@
from .settings import cachalot_settings
from .utils import (
_get_query_cache_key, _invalidate_tables,
_get_table_cache_keys, _get_tables_from_sql, RandomQueryException)


WRITE_COMPILERS = (SQLInsertCompiler, SQLUpdateCompiler, SQLDeleteCompiler)
_get_table_cache_keys, _get_tables_from_sql, RandomQueryException,
WRITE_COMPILERS)


PATCHED = False
Expand Down Expand Up @@ -102,9 +99,9 @@ def inner(compiler, *args, **kwargs):

def _patch_write_compiler(original):
@wraps(original)
def inner(compiler, *args, **kwargs):
_invalidate_tables(cachalot_caches.get_cache(), compiler)
return original(compiler, *args, **kwargs)
def inner(write_compiler, *args, **kwargs):
_invalidate_tables(cachalot_caches.get_cache(), write_compiler)
return original(write_compiler, *args, **kwargs)

inner.original = original
return inner
Expand Down
19 changes: 12 additions & 7 deletions cachalot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import django
from django.db import connections
from django.db.models.sql import Query
from django.db.models.sql.compiler import (
SQLInsertCompiler, SQLUpdateCompiler, SQLDeleteCompiler)
from django.db.models.sql.where import ExtraWhere, SubqueryConstraint
DJANGO_GTE_1_7 = django.VERSION[:2] >= (1, 7)
if DJANGO_GTE_1_7:
Expand All @@ -18,6 +20,9 @@
from .signals import post_invalidation


WRITE_COMPILERS = (SQLInsertCompiler, SQLUpdateCompiler, SQLDeleteCompiler)


class RandomQueryException(Exception):
pass

Expand Down Expand Up @@ -125,11 +130,11 @@ def _invalidate_table_cache_keys(cache, table_cache_keys):
cache.set_many(d, None)


def _invalidate_tables(cache, compiler):
db_alias = compiler.using
tables = _get_tables(compiler.query, db_alias)
table_cache_keys = [_get_table_cache_key(db_alias, t) for t in tables]
_invalidate_table_cache_keys(cache, table_cache_keys)
def _invalidate_tables(cache, write_compiler):
db_alias = write_compiler.using

table = write_compiler.query.get_meta().db_table
table_cache_key = _get_table_cache_key(db_alias, table)
_invalidate_table_cache_keys(cache, (table_cache_key,))

for table in tables:
post_invalidation.send(table, db_alias=db_alias)
post_invalidation.send(table, db_alias=db_alias)

0 comments on commit a32f86f

Please sign in to comment.