Total Complexity | 8 |
Total Lines | 31 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | # coding: utf-8 |
||
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 |