MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / _lru_cache_wrapper

Function _lru_cache_wrapper

tools/python-3.11.9-amd64/Lib/functools.py:525–639  ·  view source on GitHub ↗
(user_function, maxsize, typed, _CacheInfo)

Source from the content-addressed store, hash-verified

523 return decorating_function
524
525def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
526 # Constants shared by all lru cache instances:
527 sentinel = object() # unique object used to signal cache misses
528 make_key = _make_key # build a key from the function arguments
529 PREV, NEXT, KEY, RESULT = 0, 1, 2, 3 # names for the link fields
530
531 cache = {}
532 hits = misses = 0
533 full = False
534 cache_get = cache.get # bound method to lookup a key or return None
535 cache_len = cache.__len__ # get cache size without calling len()
536 lock = RLock() # because linkedlist updates aren't threadsafe
537 root = [] # root of the circular doubly linked list
538 root[:] = [root, root, None, None] # initialize by pointing to self
539
540 if maxsize == 0:
541
542 def wrapper(*args, **kwds):
543 # No caching -- just a statistics update
544 nonlocal misses
545 misses += 1
546 result = user_function(*args, **kwds)
547 return result
548
549 elif maxsize is None:
550
551 def wrapper(*args, **kwds):
552 # Simple caching without ordering or size limit
553 nonlocal hits, misses
554 key = make_key(args, kwds, typed)
555 result = cache_get(key, sentinel)
556 if result is not sentinel:
557 hits += 1
558 return result
559 misses += 1
560 result = user_function(*args, **kwds)
561 cache[key] = result
562 return result
563
564 else:
565
566 def wrapper(*args, **kwds):
567 # Size limited caching that tracks accesses by recency
568 nonlocal root, hits, misses, full
569 key = make_key(args, kwds, typed)
570 with lock:
571 link = cache_get(key)
572 if link is not None:
573 # Move the link to the front of the circular queue
574 link_prev, link_next, _key, result = link
575 link_prev[NEXT] = link_next
576 link_next[PREV] = link_prev
577 last = root[PREV]
578 last[NEXT] = root[PREV] = link
579 link[PREV] = last
580 link[NEXT] = root
581 hits += 1
582 return result

Callers 2

lru_cacheFunction · 0.85
decorating_functionFunction · 0.85

Calls 1

RLockFunction · 0.70

Tested by

no test coverage detected