Completed
Push — master ( b812f7...ce9ec0 )
by Bertrand
01:05
created

_patch_compiler()   C

Complexity

Conditions 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
c 0
b 0
f 0
dl 0
loc 23
rs 5.5
1
# coding: utf-8
2
3
from __future__ import unicode_literals
4
from collections import Iterable
5
from functools import wraps
6
from time import time
7
8
from django.db.backends.utils import CursorWrapper
9
from django.db.models.query import EmptyResultSet
10
from django.db.models.signals import post_migrate
11
from django.db.models.sql.compiler import (
12
    SQLCompiler, SQLInsertCompiler, SQLUpdateCompiler, SQLDeleteCompiler,
13
)
14
from django.db.transaction import Atomic, get_connection
15
from django.utils.six import binary_type
16
17
from .api import invalidate
18
from .cache import cachalot_caches
19
from .settings import cachalot_settings, ITERABLES
20
from .utils import (
21
    _get_table_cache_keys, _get_tables_from_sql,
22
    UncachableQuery, is_cachable, filter_cachable,
23
)
24
25
26
WRITE_COMPILERS = (SQLInsertCompiler, SQLUpdateCompiler, SQLDeleteCompiler)
27
28
29
def _unset_raw_connection(original):
30
    def inner(compiler, *args, **kwargs):
31
        compiler.connection.raw = False
32
        out = original(compiler, *args, **kwargs)
33
        compiler.connection.raw = True
34
        return out
35
    return inner
36
37
38
def _get_result_or_execute_query(execute_query_func, cache,
39
                                 cache_key, table_cache_keys):
40
    data = cache.get_many(table_cache_keys + [cache_key])
41
42
    new_table_cache_keys = set(table_cache_keys)
43
    new_table_cache_keys.difference_update(data)
44
45
    if new_table_cache_keys:
46
        now = time()
47
        cache.set_many({k: now for k in new_table_cache_keys},
48
                       cachalot_settings.CACHALOT_TIMEOUT)
49
    elif cache_key in data:
50
        timestamp, result = data.pop(cache_key)
51
        table_times = data.values()
52
        if table_times and timestamp > max(table_times):
53
            return result
54
55
    result = execute_query_func()
56
    if result.__class__ not in ITERABLES and isinstance(result, Iterable):
57
        result = list(result)
58
59
    cache.set(cache_key, (time(), result), cachalot_settings.CACHALOT_TIMEOUT)
60
61
    return result
62
63
64
def _patch_compiler(original):
65
    @wraps(original)
66
    @_unset_raw_connection
67
    def inner(compiler, *args, **kwargs):
68
        execute_query_func = lambda: original(compiler, *args, **kwargs)
69
        db_alias = compiler.using
70
        if not cachalot_settings.CACHALOT_ENABLED \
71
                or db_alias not in cachalot_settings.CACHALOT_DATABASES \
72
                or isinstance(compiler, WRITE_COMPILERS):
73
            return execute_query_func()
74
75
        try:
76
            cache_key = cachalot_settings.CACHALOT_QUERY_KEYGEN(compiler)
77
            table_cache_keys = _get_table_cache_keys(compiler)
78
        except (EmptyResultSet, UncachableQuery):
79
            return execute_query_func()
80
81
        return _get_result_or_execute_query(
82
            execute_query_func,
83
            cachalot_caches.get_cache(db_alias=db_alias),
84
            cache_key, table_cache_keys)
85
86
    return inner
87
88
89
def _patch_write_compiler(original):
90
    @wraps(original)
91
    @_unset_raw_connection
92
    def inner(write_compiler, *args, **kwargs):
93
        db_alias = write_compiler.using
94
        table = write_compiler.query.get_meta().db_table
95
        if is_cachable(table):
96
            invalidate(table, db_alias=db_alias,
97
                       cache_alias=cachalot_settings.CACHALOT_CACHE)
98
        return original(write_compiler, *args, **kwargs)
99
100
    return inner
101
102
103
def _patch_orm():
104
    SQLCompiler.execute_sql = _patch_compiler(SQLCompiler.execute_sql)
105
    for compiler in WRITE_COMPILERS:
106
        compiler.execute_sql = _patch_write_compiler(compiler.execute_sql)
107
108
109
def _patch_cursor():
110
    def _patch_cursor_execute(original):
111
        @wraps(original)
112
        def inner(cursor, sql, *args, **kwargs):
113
            out = original(cursor, sql, *args, **kwargs)
114
            connection = cursor.db
115
            if getattr(connection, 'raw', True) \
116
                    and cachalot_settings.CACHALOT_INVALIDATE_RAW:
117
                if isinstance(sql, binary_type):
118
                    sql = sql.decode('utf-8')
119
                sql = sql.lower()
120
                if 'update' in sql or 'insert' in sql or 'delete' in sql \
121
                        or 'alter' in sql or 'create' in sql or 'drop' in sql:
122
                    tables = filter_cachable(
123
                        _get_tables_from_sql(connection, sql))
124
                    if tables:
125
                        invalidate(*tables, db_alias=connection.alias,
126
                                   cache_alias=cachalot_settings.CACHALOT_CACHE)
127
            return out
128
129
        return inner
130
131
    CursorWrapper.execute = _patch_cursor_execute(CursorWrapper.execute)
132
    CursorWrapper.executemany = _patch_cursor_execute(CursorWrapper.executemany)
133
134
135
def _patch_atomic():
136
    def patch_enter(original):
137
        @wraps(original)
138
        def inner(self):
139
            cachalot_caches.enter_atomic(self.using)
140
            original(self)
141
142
        return inner
143
144
    def patch_exit(original):
145
        @wraps(original)
146
        def inner(self, exc_type, exc_value, traceback):
147
            needs_rollback = get_connection(self.using).needs_rollback
148
            original(self, exc_type, exc_value, traceback)
149
            cachalot_caches.exit_atomic(
150
                self.using, exc_type is None and not needs_rollback)
151
152
        return inner
153
154
    Atomic.__enter__ = patch_enter(Atomic.__enter__)
155
    Atomic.__exit__ = patch_exit(Atomic.__exit__)
156
157
158
def _invalidate_on_migration(sender, **kwargs):
159
    invalidate(*sender.get_models(), db_alias=kwargs['using'],
160
               cache_alias=cachalot_settings.CACHALOT_CACHE)
161
162
163
def patch():
164
    post_migrate.connect(_invalidate_on_migration)
165
166
    _patch_cursor()
167
    _patch_atomic()
168
    _patch_orm()
169