Eliminate unnecessary variable.
This commit is contained in:
parent
5df9f82547
commit
832eddeafb
@ -228,9 +228,8 @@ def lru_cache(maxsize=128, typed=False):
|
|||||||
PREV, NEXT, KEY, RESULT = 0, 1, 2, 3 # names for the link fields
|
PREV, NEXT, KEY, RESULT = 0, 1, 2, 3 # names for the link fields
|
||||||
|
|
||||||
def decorating_function(user_function):
|
def decorating_function(user_function):
|
||||||
|
|
||||||
cache = {}
|
cache = {}
|
||||||
hits = misses = currsize = 0
|
hits = misses = 0
|
||||||
full = False
|
full = False
|
||||||
cache_get = cache.get # bound method to lookup a key or return None
|
cache_get = cache.get # bound method to lookup a key or return None
|
||||||
lock = Lock() # because linkedlist updates aren't threadsafe
|
lock = Lock() # because linkedlist updates aren't threadsafe
|
||||||
@ -250,7 +249,7 @@ def lru_cache(maxsize=128, typed=False):
|
|||||||
|
|
||||||
def wrapper(*args, **kwds):
|
def wrapper(*args, **kwds):
|
||||||
# simple caching without ordering or size limit
|
# simple caching without ordering or size limit
|
||||||
nonlocal hits, misses, currsize
|
nonlocal hits, misses
|
||||||
key = make_key(args, kwds, typed)
|
key = make_key(args, kwds, typed)
|
||||||
result = cache_get(key, sentinel)
|
result = cache_get(key, sentinel)
|
||||||
if result is not sentinel:
|
if result is not sentinel:
|
||||||
@ -259,14 +258,13 @@ def lru_cache(maxsize=128, typed=False):
|
|||||||
result = user_function(*args, **kwds)
|
result = user_function(*args, **kwds)
|
||||||
cache[key] = result
|
cache[key] = result
|
||||||
misses += 1
|
misses += 1
|
||||||
currsize += 1
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
def wrapper(*args, **kwds):
|
def wrapper(*args, **kwds):
|
||||||
# size limited caching that tracks accesses by recency
|
# size limited caching that tracks accesses by recency
|
||||||
nonlocal root, hits, misses, currsize, full
|
nonlocal root, hits, misses, full
|
||||||
key = make_key(args, kwds, typed)
|
key = make_key(args, kwds, typed)
|
||||||
with lock:
|
with lock:
|
||||||
link = cache_get(key)
|
link = cache_get(key)
|
||||||
@ -303,23 +301,22 @@ def lru_cache(maxsize=128, typed=False):
|
|||||||
last = root[PREV]
|
last = root[PREV]
|
||||||
link = [last, root, key, result]
|
link = [last, root, key, result]
|
||||||
cache[key] = last[NEXT] = root[PREV] = link
|
cache[key] = last[NEXT] = root[PREV] = link
|
||||||
currsize += 1
|
full = (len(cache) == maxsize)
|
||||||
full = (currsize == maxsize)
|
|
||||||
misses += 1
|
misses += 1
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def cache_info():
|
def cache_info():
|
||||||
"""Report cache statistics"""
|
"""Report cache statistics"""
|
||||||
with lock:
|
with lock:
|
||||||
return _CacheInfo(hits, misses, maxsize, currsize)
|
return _CacheInfo(hits, misses, maxsize, len(cache))
|
||||||
|
|
||||||
def cache_clear():
|
def cache_clear():
|
||||||
"""Clear the cache and cache statistics"""
|
"""Clear the cache and cache statistics"""
|
||||||
nonlocal hits, misses, currsize, full
|
nonlocal hits, misses, full
|
||||||
with lock:
|
with lock:
|
||||||
cache.clear()
|
cache.clear()
|
||||||
root[:] = [root, root, None, None]
|
root[:] = [root, root, None, None]
|
||||||
hits = misses = currsize = 0
|
hits = misses = 0
|
||||||
full = False
|
full = False
|
||||||
|
|
||||||
wrapper.cache_info = cache_info
|
wrapper.cache_info = cache_info
|
||||||
|
Loading…
x
Reference in New Issue
Block a user