| Total Complexity | 7 |
| Total Lines | 26 |
| Duplicated Lines | 0 % |
| 1 | # coding: utf-8 |
||
| 6 | class AtomicCache(dict): |
||
| 7 | def __init__(self, parent_cache, db_alias): |
||
| 8 | super(AtomicCache, self).__init__() |
||
| 9 | self.parent_cache = parent_cache |
||
| 10 | self.db_alias = db_alias |
||
| 11 | self.to_be_invalidated = set() |
||
| 12 | |||
| 13 | def set(self, k, v, timeout): |
||
| 14 | self[k] = v |
||
| 15 | |||
| 16 | def get_many(self, keys): |
||
| 17 | data = {k: self[k] for k in keys if k in self} |
||
| 18 | missing_keys = set(keys) |
||
| 19 | missing_keys.difference_update(data) |
||
| 20 | data.update(self.parent_cache.get_many(missing_keys)) |
||
| 21 | return data |
||
| 22 | |||
| 23 | def set_many(self, data, timeout): |
||
| 24 | self.update(data) |
||
| 25 | |||
| 26 | def commit(self): |
||
| 27 | self.parent_cache.set_many(self, None) |
||
| 28 | # The previous `set_many` is not enough. The parent cache needs to be |
||
| 29 | # invalidated in case another transaction occurred in the meantime. |
||
| 30 | _invalidate_tables(self.parent_cache, self.db_alias, |
||
| 31 | self.to_be_invalidated) |
||
| 32 | |||
| 37 |