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

Unified Diff: client/third_party/cachetools/lru.py

Issue 2953253003: Replace custom blob gRPC API with ByteStream (Closed)
Patch Set: Import ndb directly to test code Created 3 years, 6 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 | « client/third_party/cachetools/lfu.py ('k') | client/third_party/cachetools/rr.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: client/third_party/cachetools/lru.py
diff --git a/client/third_party/cachetools/lru.py b/client/third_party/cachetools/lru.py
new file mode 100644
index 0000000000000000000000000000000000000000..b945797c1791a9c5c21f8e00b6d4ffd832673108
--- /dev/null
+++ b/client/third_party/cachetools/lru.py
@@ -0,0 +1,48 @@
+from __future__ import absolute_import
+
+import collections
+
+from .cache import Cache
+
+
+class LRUCache(Cache):
+ """Least Recently Used (LRU) cache implementation."""
+
+ def __init__(self, maxsize, missing=None, getsizeof=None):
+ Cache.__init__(self, maxsize, missing, getsizeof)
+ self.__order = collections.OrderedDict()
+
+ def __getitem__(self, key, cache_getitem=Cache.__getitem__):
+ value = cache_getitem(self, key)
+ self.__update(key)
+ return value
+
+ def __setitem__(self, key, value, cache_setitem=Cache.__setitem__):
+ cache_setitem(self, key, value)
+ self.__update(key)
+
+ def __delitem__(self, key, cache_delitem=Cache.__delitem__):
+ cache_delitem(self, key)
+ del self.__order[key]
+
+ def popitem(self):
+ """Remove and return the `(key, value)` pair least recently used."""
+ try:
+ key = next(iter(self.__order))
+ except StopIteration:
+ raise KeyError('%s is empty' % self.__class__.__name__)
+ else:
+ return (key, self.pop(key))
+
+ if hasattr(collections.OrderedDict, 'move_to_end'):
+ def __update(self, key):
+ try:
+ self.__order.move_to_end(key)
+ except KeyError:
+ self.__order[key] = None
+ else:
+ def __update(self, key):
+ try:
+ self.__order[key] = self.__order.pop(key)
+ except KeyError:
+ self.__order[key] = None
« no previous file with comments | « client/third_party/cachetools/lfu.py ('k') | client/third_party/cachetools/rr.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698