MCPcopy Index your code
hub / github.com/python/cpython / lru_cache

Function lru_cache

Lib/functools.py:555–602  ·  view source on GitHub ↗

Least-recently-used cache decorator. If *maxsize* is set to None, the LRU features are disabled and the cache can grow without bound. If *typed* is True, arguments of different types will be cached separately. For example, f(decimal.Decimal("3.0")) and f(3.0) will be treated as

(maxsize=128, typed=False)

Source from the content-addressed store, hash-verified

553 return key
554
555def lru_cache(maxsize=128, typed=False):
556 """Least-recently-used cache decorator.
557
558 If *maxsize* is set to None, the LRU features are disabled and the cache
559 can grow without bound.
560
561 If *typed* is True, arguments of different types will be cached separately.
562 For example, f(decimal.Decimal("3.0")) and f(3.0) will be treated as
563 distinct calls with distinct results. Some types such as str and int may
564 be cached separately even when typed is false.
565
566 Arguments to the cached function must be hashable.
567
568 View the cache statistics named tuple (hits, misses, maxsize, currsize)
569 with f.cache_info(). Clear the cache and statistics with f.cache_clear().
570 Access the underlying function with f.__wrapped__.
571
572 See: https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)
573
574 """
575
576 # Users should only access the lru_cache through its public API:
577 # cache_info, cache_clear, and f.__wrapped__
578 # The internals of the lru_cache are encapsulated for thread safety and
579 # to allow the implementation to change (including a possible C version).
580
581 if isinstance(maxsize, int):
582 # Negative maxsize is treated as 0
583 if maxsize < 0:
584 maxsize = 0
585
586 elif callable(maxsize) and isinstance(typed, bool):
587 # The user_function was passed in directly via the maxsize argument
588 user_function, maxsize = maxsize, 128
589 wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
590 wrapper.cache_parameters = lambda : {'maxsize': maxsize, 'typed': typed}
591 return update_wrapper(wrapper, user_function)
592
593 elif maxsize is not None:
594 raise TypeError(
595 'Expected first argument to be an integer, a callable, or None')
596
597 def decorating_function(user_function):
598 wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
599 wrapper.cache_parameters = lambda : {'maxsize': maxsize, 'typed': typed}
600 return update_wrapper(wrapper, user_function)
601
602 return decorating_function
603
604def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
605 if not callable(user_function):

Callers 1

cacheFunction · 0.85

Calls 2

_lru_cache_wrapperFunction · 0.85
update_wrapperFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…