@@ -228,9 +228,8 @@ def lru_cache(maxsize=128, typed=False):
228228 PREV , NEXT , KEY , RESULT = 0 , 1 , 2 , 3 # names for the link fields
229229
230230 def decorating_function (user_function ):
231-
232231 cache = {}
233- hits = misses = currsize = 0
232+ hits = misses = 0
234233 full = False
235234 cache_get = cache .get # bound method to lookup a key or return None
236235 lock = Lock () # because linkedlist updates aren't threadsafe
@@ -250,7 +249,7 @@ def wrapper(*args, **kwds):
250249
251250 def wrapper (* args , ** kwds ):
252251 # simple caching without ordering or size limit
253- nonlocal hits , misses , currsize
252+ nonlocal hits , misses
254253 key = make_key (args , kwds , typed )
255254 result = cache_get (key , sentinel )
256255 if result is not sentinel :
@@ -259,14 +258,13 @@ def wrapper(*args, **kwds):
259258 result = user_function (* args , ** kwds )
260259 cache [key ] = result
261260 misses += 1
262- currsize += 1
263261 return result
264262
265263 else :
266264
267265 def wrapper (* args , ** kwds ):
268266 # size limited caching that tracks accesses by recency
269- nonlocal root , hits , misses , currsize , full
267+ nonlocal root , hits , misses , full
270268 key = make_key (args , kwds , typed )
271269 with lock :
272270 link = cache_get (key )
@@ -303,23 +301,22 @@ def wrapper(*args, **kwds):
303301 last = root [PREV ]
304302 link = [last , root , key , result ]
305303 cache [key ] = last [NEXT ] = root [PREV ] = link
306- currsize += 1
307- full = (currsize == maxsize )
304+ full = (len (cache ) == maxsize )
308305 misses += 1
309306 return result
310307
311308 def cache_info ():
312309 """Report cache statistics"""
313310 with lock :
314- return _CacheInfo (hits , misses , maxsize , currsize )
311+ return _CacheInfo (hits , misses , maxsize , len ( cache ) )
315312
316313 def cache_clear ():
317314 """Clear the cache and cache statistics"""
318- nonlocal hits , misses , currsize , full
315+ nonlocal hits , misses , full
319316 with lock :
320317 cache .clear ()
321318 root [:] = [root , root , None , None ]
322- hits = misses = currsize = 0
319+ hits = misses = 0
323320 full = False
324321
325322 wrapper .cache_info = cache_info
0 commit comments