Kaydet (Commit) 32e2ff5d authored tarafından Raymond Hettinger's avatar Raymond Hettinger

merge

...@@ -291,19 +291,23 @@ def lru_cache(maxsize=128, typed=False): ...@@ -291,19 +291,23 @@ def lru_cache(maxsize=128, typed=False):
# computed result and update the count of misses. # computed result and update the count of misses.
pass pass
elif full: elif full:
# use root to store the new key and result # use the old root to store the new key and result
root[KEY] = key oldroot = root
root[RESULT] = result oldroot[KEY] = key
cache[key] = root oldroot[RESULT] = result
# empty the oldest link and make it the new root # empty the oldest link and make it the new root
root = root[NEXT] root = oldroot[NEXT]
del cache[root[KEY]] oldkey = root[KEY]
oldvalue = root[RESULT]
root[KEY] = root[RESULT] = None root[KEY] = root[RESULT] = None
# now update the cache dictionary for the new links
del cache[oldkey]
cache[key] = oldroot
else: else:
# put result in a new link at the front of the queue # put result in a new link at the front of the queue
last = root[PREV] last = root[PREV]
link = [last, root, key, result] link = [last, root, key, result]
cache[key] = last[NEXT] = root[PREV] = link last[NEXT] = root[PREV] = cache[key] = link
full = (len(cache) == maxsize) full = (len(cache) == maxsize)
misses += 1 misses += 1
return result return result
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment