Completed
Push — master ( 8124bf...d2c927 )
by Bertrand
01:15
created

AtomicCache   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 26
rs 10
c 2
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_many() 0 6 3
A set_many() 0 2 1
A __init__() 0 5 1
A commit() 0 6 1
A set() 0 2 1
1
# coding: utf-8
2
3
from __future__ import unicode_literals
4
5
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
33
34
# We import this after AtomicCache to avoid a circular import issue and
35
# avoid importing this locally, which degrades performance.
36
from .utils import _invalidate_tables
37