According to the official Python documentation for the weakref module the “primary use for weak references is to implement caches or mappings holding large objects,…”. So, I used a WeakValueDictionary to implement a caching mechanism for a long running function. However, as it turned out, values in the cache never stayed there until they would actually be used again, but needed to be recomputed almost every time. Since there were no strong references between accesses to the values stored in the WeakValueDictionary, the GC got rid of them (even though there was absolutely no problem with memory).
Now, how am I then supposed to use the weak reference stuff to implement a cache? If I keep strong references somewhere explicitly to keep the GC from deleting my weak references, there would be no point using a WeakValueDictionary in the first place. There should probably be some option to the GC that tells it: delete everything that has no references at all and everything with weak references only when memory is running out (or some threshold is exceeded). Is there something like that? Or is there a better strategy for this kind of cache?
I’ll attempt to answer your inquiry with an example of how to use the
weakrefmodule to implement caching. We’ll keep our cache’s weak references in aweakref.WeakValueDictionary, and the strong references in acollections.dequebecause it has amaxlenproperty that controls how many objects it holds on to. Implemented in function closure style:The
dequeobject will only keep the lastmaxlenentries, simply dropping references to the old entries once it reaches capacity. When the old entries are dropped and garbage collected by python, theWeakValueDictionarywill remove those keys from the map. Hence, the combination of the two objects helps us keep onlymaxlenentries in our LRU cache.