GC issues when using WeakValueDictionary for caches

According to the official Python documentation for the weakref module, "the main use for weak links is to implement caches or mappings to large objects ...". So, I used WeakValueDictionary to implement a caching mechanism for a long-term function. However, as it turned out, the values ​​in the cache never remained there until they were actually used again, but they had to be recounted almost every time. Since there were no strong references between accesses to values ​​stored in the WeakValueDictionary, GC got rid of them (although there were no memory problems).

Now, how should I use weak reference material to implement the cache? If I explicitly store links somewhere so that HC does not delete my weak links, at first it would not make sense to use WeakValueDictionary. There probably should be some option for the GC that reports this: delete everything that has no links at all, and all with weak links only when the memory is exhausted (or some threshold is exceeded). Is there something like that? Or is there a better strategy for such a cache?

+5
source share
2 answers

weakref . weakref.WeakValueDictionary collections.deque, maxlen, , . :

import weakref, collections
def createLRUCache(factory, maxlen=64):
    weak = weakref.WeakValueDictionary()
    strong = collections.deque(maxlen=maxlen)

    notFound = object()
    def fetch(key):
        value = weak.get(key, notFound)
        if value is notFound:
            weak[key] = value = factory(key)
        strong.append(value)
        return value
    return fetch

deque maxlen, . , python, WeakValueDictionary . , maxlen LRU.

class Silly(object):
    def __init__(self, v):
        self.v = v

def fib(i):
    if i > 1:
        return Silly(_fibCache(i-1).v + _fibCache(i-2).v)
    elif i: return Silly(1)
    else: return Silly(0)
_fibCache = createLRUCache(fib)
+3

, , , CPython 2.7 3.0.

createLRUCache():

createLRUCache (factory, maxlen = 64) . "maxlen" - , . , , .

, GC WeakValueDictionary , GC:

0, .

+1

All Articles