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

Side by Side Diff: client/third_party/cachetools/lfu.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/keys.py ('k') | client/third_party/cachetools/lru.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 LFUCache(Cache):
9 """Least Frequently Used (LFU) cache implementation."""
10
11 def __init__(self, maxsize, missing=None, getsizeof=None):
12 Cache.__init__(self, maxsize, missing, getsizeof)
13 self.__counter = collections.Counter()
14
15 def __getitem__(self, key, cache_getitem=Cache.__getitem__):
16 value = cache_getitem(self, key)
17 self.__counter[key] -= 1
18 return value
19
20 def __setitem__(self, key, value, cache_setitem=Cache.__setitem__):
21 cache_setitem(self, key, value)
22 self.__counter[key] -= 1
23
24 def __delitem__(self, key, cache_delitem=Cache.__delitem__):
25 cache_delitem(self, key)
26 del self.__counter[key]
27
28 def popitem(self):
29 """Remove and return the `(key, value)` pair least frequently used."""
30 try:
31 (key, _), = self.__counter.most_common(1)
32 except ValueError:
33 raise KeyError('%s is empty' % self.__class__.__name__)
34 else:
35 return (key, self.pop(key))
OLDNEW
« no previous file with comments | « client/third_party/cachetools/keys.py ('k') | client/third_party/cachetools/lru.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698