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
Problems with the GC when using a WeakValueDictionary for caches