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

Side by Side Diff: third_party/google-endpoints/cachetools/lfu.py

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 10 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
OLDNEW
(Empty)
1 import collections
2
3 from .cache import Cache
4
5
6 class LFUCache(Cache):
7 """Least Frequently Used (LFU) cache implementation."""
8
9 def __init__(self, maxsize, missing=None, getsizeof=None):
10 Cache.__init__(self, maxsize, missing, getsizeof)
11 self.__counter = collections.Counter()
12
13 def __getitem__(self, key, cache_getitem=Cache.__getitem__):
14 value = cache_getitem(self, key)
15 self.__counter[key] -= 1
16 return value
17
18 def __setitem__(self, key, value, cache_setitem=Cache.__setitem__):
19 cache_setitem(self, key, value)
20 self.__counter[key] -= 1
21
22 def __delitem__(self, key, cache_delitem=Cache.__delitem__):
23 cache_delitem(self, key)
24 del self.__counter[key]
25
26 def popitem(self):
27 """Remove and return the `(key, value)` pair least frequently used."""
28 try:
29 (key, _), = self.__counter.most_common(1)
30 except ValueError:
31 raise KeyError('%s is empty' % self.__class__.__name__)
32 else:
33 return (key, self.pop(key))
OLDNEW
« 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