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

Unified Diff: third_party/google-endpoints/cachetools/lfu.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/keys.py ('k') | third_party/google-endpoints/cachetools/lru.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/google-endpoints/cachetools/lfu.py
diff --git a/third_party/google-endpoints/cachetools/lfu.py b/third_party/google-endpoints/cachetools/lfu.py
new file mode 100644
index 0000000000000000000000000000000000000000..160f537d43190105d694489383c88f74631aa62c
--- /dev/null
+++ b/third_party/google-endpoints/cachetools/lfu.py
@@ -0,0 +1,33 @@
+import collections
+
+from .cache import Cache
+
+
+class LFUCache(Cache):
+ """Least Frequently Used (LFU) cache implementation."""
+
+ def __init__(self, maxsize, missing=None, getsizeof=None):
+ Cache.__init__(self, maxsize, missing, getsizeof)
+ self.__counter = collections.Counter()
+
+ def __getitem__(self, key, cache_getitem=Cache.__getitem__):
+ value = cache_getitem(self, key)
+ self.__counter[key] -= 1
+ return value
+
+ def __setitem__(self, key, value, cache_setitem=Cache.__setitem__):
+ cache_setitem(self, key, value)
+ self.__counter[key] -= 1
+
+ def __delitem__(self, key, cache_delitem=Cache.__delitem__):
+ cache_delitem(self, key)
+ del self.__counter[key]
+
+ def popitem(self):
+ """Remove and return the `(key, value)` pair least frequently used."""
+ try:
+ (key, _), = self.__counter.most_common(1)
+ except ValueError:
+ raise KeyError('%s is empty' % self.__class__.__name__)
+ else:
+ return (key, self.pop(key))
« no previous file with comments | « third_party/google-endpoints/cachetools/keys.py ('k') | third_party/google-endpoints/cachetools/lru.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698