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

Side by Side 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, 5 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
« no previous file with comments | « client/third_party/cachetools/lfu.py ('k') | client/third_party/cachetools/rr.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 from __future__ import absolute_import
2
3 import collections
4
5 from .cache import Cache
6
7
8 class LRUCache(Cache):
9 """Least Recently Used (LRU) cache implementation."""
10
11 def __init__(self, maxsize, missing=None, getsizeof=None):
12 Cache.__init__(self, maxsize, missing, getsizeof)
13 self.__order = collections.OrderedDict()
14
15 def __getitem__(self, key, cache_getitem=Cache.__getitem__):
16 value = cache_getitem(self, key)
17 self.__update(key)
18 return value
19
20 def __setitem__(self, key, value, cache_setitem=Cache.__setitem__):
21 cache_setitem(self, key, value)
22 self.__update(key)
23
24 def __delitem__(self, key, cache_delitem=Cache.__delitem__):
25 cache_delitem(self, key)
26 del self.__order[key]
27
28 def popitem(self):
29 """Remove and return the `(key, value)` pair least recently used."""
30 try:
31 key = next(iter(self.__order))
32 except StopIteration:
33 raise KeyError('%s is empty' % self.__class__.__name__)
34 else:
35 return (key, self.pop(key))
36
37 if hasattr(collections.OrderedDict, 'move_to_end'):
38 def __update(self, key):
39 try:
40 self.__order.move_to_end(key)
41 except KeyError:
42 self.__order[key] = None
43 else:
44 def __update(self, key):
45 try:
46 self.__order[key] = self.__order.pop(key)
47 except KeyError:
48 self.__order[key] = None
OLDNEW
« 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