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

Unified Diff: third_party/google-endpoints/cachetools/rr.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
« no previous file with comments | « third_party/google-endpoints/cachetools/lru.py ('k') | third_party/google-endpoints/cachetools/ttl.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/google-endpoints/cachetools/rr.py
diff --git a/third_party/google-endpoints/cachetools/rr.py b/third_party/google-endpoints/cachetools/rr.py
new file mode 100644
index 0000000000000000000000000000000000000000..c82919e1dc333c21b17af9f6a2fdfd4936813825
--- /dev/null
+++ b/third_party/google-endpoints/cachetools/rr.py
@@ -0,0 +1,26 @@
+import random
+
+from .cache import Cache
+
+
+class RRCache(Cache):
+ """Random Replacement (RR) cache implementation."""
+
+ def __init__(self, maxsize, choice=random.choice, missing=None,
+ getsizeof=None):
+ Cache.__init__(self, maxsize, missing, getsizeof)
+ self.__choice = choice
+
+ @property
+ def choice(self):
+ """The `choice` function used by the cache."""
+ return self.__choice
+
+ def popitem(self):
+ """Remove and return a random `(key, value)` pair."""
+ try:
+ key = self.__choice(list(self))
+ except IndexError:
+ raise KeyError('%s is empty' % self.__class__.__name__)
+ else:
+ return (key, self.pop(key))
« no previous file with comments | « third_party/google-endpoints/cachetools/lru.py ('k') | third_party/google-endpoints/cachetools/ttl.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698