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

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

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: third_party/google-endpoints/dogpile/cache/backends/memory.py
diff --git a/third_party/google-endpoints/dogpile/cache/backends/memory.py b/third_party/google-endpoints/dogpile/cache/backends/memory.py
new file mode 100644
index 0000000000000000000000000000000000000000..e2083f7f052c7ada123edec8d90a98b98f283487
--- /dev/null
+++ b/third_party/google-endpoints/dogpile/cache/backends/memory.py
@@ -0,0 +1,124 @@
+"""
+Memory Backends
+---------------
+
+Provides simple dictionary-based backends.
+
+The two backends are :class:`.MemoryBackend` and :class:`.MemoryPickleBackend`;
+the latter applies a serialization step to cached values while the former
+places the value as given into the dictionary.
+
+"""
+
+from ..api import CacheBackend, NO_VALUE
+from ...util.compat 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
+ }
+ )
+
+
+ """
+ pickle_values = 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 and self.pickle_values:
+ value = pickle.loads(value)
+ return value
+
+ def get_multi(self, keys):
+ ret = [
+ self._cache.get(key, NO_VALUE)
+ for key in keys]
+ if self.pickle_values:
+ ret = [
+ pickle.loads(value)
+ if value is not NO_VALUE else value
+ for value in ret
+ ]
+ return ret
+
+ def set(self, key, value):
+ if self.pickle_values:
+ value = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
+ self._cache[key] = value
+
+ def set_multi(self, mapping):
+ pickle_values = self.pickle_values
+ for key, value in mapping.items():
+ if pickle_values:
+ value = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
+ self._cache[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
+ :meth:`.MemoryBackend.set` and deserializes :meth:`.MemoryBackend.get`.
+
+ E.g.::
+
+ from dogpile.cache import make_region
+
+ region = make_region().configure(
+ 'dogpile.cache.memory_pickle'
+ )
+
+ The usage of pickle to serialize cached values allows an object
+ as placed in the cache to be a copy of the original given object, so
+ that any subsequent changes to the given object aren't reflected
+ in the cached value, thus making the backend behave the same way
+ as other backends which make use of serialization.
+
+ The serialization is performed via pickle, and incurs the same
+ performance hit in doing so as that of other backends; in this way
+ the :class:`.MemoryPickleBackend` performance is somewhere in between
+ that of the pure :class:`.MemoryBackend` and the remote server oriented
+ backends such as that of Memcached or Redis.
+
+ Pickle behavior here is the same as that of the Redis backend, using
+ either ``cPickle`` or ``pickle`` and specifying ``HIGHEST_PROTOCOL``
+ upon serialize.
+
+ .. versionadded:: 0.5.3
+
+ """
+ pickle_values = True

Powered by Google App Engine
This is Rietveld 408576698