You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.0 KiB

  1. import collections
  2. from .cache import Cache
  3. class LFUCache(Cache):
  4. """Least Frequently Used (LFU) cache implementation."""
  5. def __init__(self, maxsize, getsizeof=None):
  6. Cache.__init__(self, maxsize, getsizeof)
  7. self.__counter = collections.Counter()
  8. def __getitem__(self, key, cache_getitem=Cache.__getitem__):
  9. value = cache_getitem(self, key)
  10. self.__counter[key] -= 1
  11. return value
  12. def __setitem__(self, key, value, cache_setitem=Cache.__setitem__):
  13. cache_setitem(self, key, value)
  14. self.__counter[key] -= 1
  15. def __delitem__(self, key, cache_delitem=Cache.__delitem__):
  16. cache_delitem(self, key)
  17. del self.__counter[key]
  18. def popitem(self):
  19. """Remove and return the `(key, value)` pair least frequently used."""
  20. try:
  21. (key, _), = self.__counter.most_common(1)
  22. except ValueError:
  23. msg = '%s is empty' % self.__class__.__name__
  24. raise KeyError(msg) from None
  25. else:
  26. return (key, self.pop(key))