AtomicCache.commit()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
c 2
b 0
f 0
dl 0
loc 11
rs 9.4285
1
# coding: utf-8
2
3
from __future__ import unicode_literals
4
5
from .settings import cachalot_settings
6
7
8
class AtomicCache(dict):
9
    def __init__(self, parent_cache, db_alias):
10
        super(AtomicCache, self).__init__()
11
        self.parent_cache = parent_cache
12
        self.db_alias = db_alias
13
        self.to_be_invalidated = set()
14
15
    def set(self, k, v, timeout):
16
        self[k] = v
17
18
    def get_many(self, keys):
19
        data = {k: self[k] for k in keys if k in self}
20
        missing_keys = set(keys)
21
        missing_keys.difference_update(data)
22
        data.update(self.parent_cache.get_many(missing_keys))
23
        return data
24
25
    def set_many(self, data, timeout):
26
        self.update(data)
27
28
    def commit(self):
29
        # We import this here to avoid a circular import issue.
30
        from .utils import _invalidate_tables
31
32
        if self:
33
            self.parent_cache.set_many(
34
                self, cachalot_settings.CACHALOT_TIMEOUT)
35
        # The previous `set_many` is not enough.  The parent cache needs to be
36
        # invalidated in case another transaction occurred in the meantime.
37
        _invalidate_tables(self.parent_cache, self.db_alias,
38
                           self.to_be_invalidated)
39