1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
"""
Memory Backend
--------------
Provides a simple dictionary-based backend.
"""
from dogpile.cache.api import CacheBackend, NO_VALUE
try:
import cPickle as pickle
except ImportError:
import pickle
class MemoryBackend(CacheBackend):
"""A backend that uses a plain dictionary.
There is no size management, and values which
are placed into the dictionary will remain
until explicitly removed. Note that
Dogpile's expiration of items is based on
timestamps and does not remove them from
the cache.
E.g.::
from dogpile.cache import make_region
region = make_region().configure(
'dogpile.cache.memory'
)
To use a Python dictionary of your choosing,
it can be passed in with the ``cache_dict``
argument::
my_dictionary = {}
region = make_region().configure(
'dogpile.cache.memory',
arguments={
"cache_dict":my_dictionary
}
)
"""
is_pickle = False
def __init__(self, arguments):
self._cache = arguments.pop("cache_dict", {})
def get(self, key):
value = self._cache.get(key, NO_VALUE)
if value is not NO_VALUE :
if self.is_pickle :
value = pickle.loads(value)
return value
def get_multi(self, keys):
values = []
for key in keys :
values.append(self.get(key))
return values
def set(self, key, value):
if self.is_pickle :
value = pickle.dumps(value)
self._cache[key] = value
def set_multi(self, mapping):
for key,value in mapping.items():
self.set(key, value)
def delete(self, key):
self._cache.pop(key, None)
def delete_multi(self, keys):
for key in keys:
self._cache.pop(key, None)
class MemoryPickleBackend(MemoryBackend):
"""A backend that uses a plain dictionary, but serializes objects on `set`
and deserializes the objects on `get`. This is because objects cached in
the MemoryBackend are cached as the actual object, so changes to them will
persist without a `set`.
This backend takes a lightweight performance hit through pickling, in order
to achieve parity with other cache backends where the objects returned by
`get` are a discretely new object, and `set` must be called to persist
changes.
MemoryPickleBackend will try to serialize with cPickle, and will fall back
to pickle if it is not available.
"""
is_pickle = True
|