Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/dogpile/cache/backends/memory.py : 59%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2Memory Backends
3---------------
5Provides simple dictionary-based backends.
7The two backends are :class:`.MemoryBackend` and :class:`.MemoryPickleBackend`;
8the latter applies a serialization step to cached values while the former
9places the value as given into the dictionary.
11"""
13from ..api import CacheBackend
14from ..api import NO_VALUE
15from ...util.compat import pickle
18class MemoryBackend(CacheBackend):
19 """A backend that uses a plain dictionary.
21 There is no size management, and values which
22 are placed into the dictionary will remain
23 until explicitly removed. Note that
24 Dogpile's expiration of items is based on
25 timestamps and does not remove them from
26 the cache.
28 E.g.::
30 from dogpile.cache import make_region
32 region = make_region().configure(
33 'dogpile.cache.memory'
34 )
37 To use a Python dictionary of your choosing,
38 it can be passed in with the ``cache_dict``
39 argument::
41 my_dictionary = {}
42 region = make_region().configure(
43 'dogpile.cache.memory',
44 arguments={
45 "cache_dict":my_dictionary
46 }
47 )
50 """
52 pickle_values = False
54 def __init__(self, arguments):
55 self._cache = arguments.pop("cache_dict", {})
57 def get(self, key):
58 value = self._cache.get(key, NO_VALUE)
59 if value is not NO_VALUE and self.pickle_values:
60 value = pickle.loads(value)
61 return value
63 def get_multi(self, keys):
64 ret = [self._cache.get(key, NO_VALUE) for key in keys]
65 if self.pickle_values:
66 ret = [
67 pickle.loads(value) if value is not NO_VALUE else value
68 for value in ret
69 ]
70 return ret
72 def set(self, key, value):
73 if self.pickle_values:
74 value = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
75 self._cache[key] = value
77 def set_multi(self, mapping):
78 pickle_values = self.pickle_values
79 for key, value in mapping.items():
80 if pickle_values:
81 value = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
82 self._cache[key] = value
84 def delete(self, key):
85 self._cache.pop(key, None)
87 def delete_multi(self, keys):
88 for key in keys:
89 self._cache.pop(key, None)
92class MemoryPickleBackend(MemoryBackend):
93 """A backend that uses a plain dictionary, but serializes objects on
94 :meth:`.MemoryBackend.set` and deserializes :meth:`.MemoryBackend.get`.
96 E.g.::
98 from dogpile.cache import make_region
100 region = make_region().configure(
101 'dogpile.cache.memory_pickle'
102 )
104 The usage of pickle to serialize cached values allows an object
105 as placed in the cache to be a copy of the original given object, so
106 that any subsequent changes to the given object aren't reflected
107 in the cached value, thus making the backend behave the same way
108 as other backends which make use of serialization.
110 The serialization is performed via pickle, and incurs the same
111 performance hit in doing so as that of other backends; in this way
112 the :class:`.MemoryPickleBackend` performance is somewhere in between
113 that of the pure :class:`.MemoryBackend` and the remote server oriented
114 backends such as that of Memcached or Redis.
116 Pickle behavior here is the same as that of the Redis backend, using
117 either ``cPickle`` or ``pickle`` and specifying ``HIGHEST_PROTOCOL``
118 upon serialize.
120 .. versionadded:: 0.5.3
122 """
124 pickle_values = True