问题
Is it possible to define a callback for functools.lru_cache
when an item is evicted? In the callback the cached value should also be present.
If not, maybe someone knows a light-weight dict-like cache that supports eviction and callbacks?
回答1:
I will post the solution I used for future reference. I used a package called cachetools (https://github.com/tkem/cachetools). You can install by simply $ pip install cachetools
.
It also has decorators similar to the Python 3 functools.lru_cache
(https://docs.python.org/3/library/functools.html).
The different caches all derive from cachetools.cache.Cache
which calls the popitem()
function from MutableMapping
when evicting an item. This function returns the key and the value of the "popped" item.
To inject an eviction callback one simply has to derive from the wanted cache and override the popitem()
function. For example:
class LRUCache2(LRUCache):
def __init__(self, maxsize, missing=None, getsizeof=None, evict=None):
LRUCache.__init__(self, maxsize, missing, getsizeof)
self.__evict = evict
def popitem(self):
key, val = LRUCache.popitem(self)
evict = self.__evict
if evict:
evict(key, val)
return key, val
来源:https://stackoverflow.com/questions/29470958/python-functools-lru-cache-eviction-callback-or-equivalent