Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: third_party/google-endpoints/dogpile/cache/backends/memory.py

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 """
2 Memory Backends
3 ---------------
4
5 Provides simple dictionary-based backends.
6
7 The two backends are :class:`.MemoryBackend` and :class:`.MemoryPickleBackend`;
8 the latter applies a serialization step to cached values while the former
9 places the value as given into the dictionary.
10
11 """
12
13 from ..api import CacheBackend, NO_VALUE
14 from ...util.compat import pickle
15
16
17 class MemoryBackend(CacheBackend):
18 """A backend that uses a plain dictionary.
19
20 There is no size management, and values which
21 are placed into the dictionary will remain
22 until explicitly removed. Note that
23 Dogpile's expiration of items is based on
24 timestamps and does not remove them from
25 the cache.
26
27 E.g.::
28
29 from dogpile.cache import make_region
30
31 region = make_region().configure(
32 'dogpile.cache.memory'
33 )
34
35
36 To use a Python dictionary of your choosing,
37 it can be passed in with the ``cache_dict``
38 argument::
39
40 my_dictionary = {}
41 region = make_region().configure(
42 'dogpile.cache.memory',
43 arguments={
44 "cache_dict":my_dictionary
45 }
46 )
47
48
49 """
50 pickle_values = False
51
52 def __init__(self, arguments):
53 self._cache = arguments.pop("cache_dict", {})
54
55 def get(self, key):
56 value = self._cache.get(key, NO_VALUE)
57 if value is not NO_VALUE and self.pickle_values:
58 value = pickle.loads(value)
59 return value
60
61 def get_multi(self, keys):
62 ret = [
63 self._cache.get(key, NO_VALUE)
64 for key in keys]
65 if self.pickle_values:
66 ret = [
67 pickle.loads(value)
68 if value is not NO_VALUE else value
69 for value in ret
70 ]
71 return ret
72
73 def set(self, key, value):
74 if self.pickle_values:
75 value = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
76 self._cache[key] = value
77
78 def set_multi(self, mapping):
79 pickle_values = self.pickle_values
80 for key, value in mapping.items():
81 if pickle_values:
82 value = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
83 self._cache[key] = value
84
85 def delete(self, key):
86 self._cache.pop(key, None)
87
88 def delete_multi(self, keys):
89 for key in keys:
90 self._cache.pop(key, None)
91
92
93 class MemoryPickleBackend(MemoryBackend):
94 """A backend that uses a plain dictionary, but serializes objects on
95 :meth:`.MemoryBackend.set` and deserializes :meth:`.MemoryBackend.get`.
96
97 E.g.::
98
99 from dogpile.cache import make_region
100
101 region = make_region().configure(
102 'dogpile.cache.memory_pickle'
103 )
104
105 The usage of pickle to serialize cached values allows an object
106 as placed in the cache to be a copy of the original given object, so
107 that any subsequent changes to the given object aren't reflected
108 in the cached value, thus making the backend behave the same way
109 as other backends which make use of serialization.
110
111 The serialization is performed via pickle, and incurs the same
112 performance hit in doing so as that of other backends; in this way
113 the :class:`.MemoryPickleBackend` performance is somewhere in between
114 that of the pure :class:`.MemoryBackend` and the remote server oriented
115 backends such as that of Memcached or Redis.
116
117 Pickle behavior here is the same as that of the Redis backend, using
118 either ``cPickle`` or ``pickle`` and specifying ``HIGHEST_PROTOCOL``
119 upon serialize.
120
121 .. versionadded:: 0.5.3
122
123 """
124 pickle_values = True
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698