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(3.0) and f(3) will be treated as distinct calls wit
(maxsize=128, typed=False)
| 477 | return _HashedSeq(key) |
| 478 | |
| 479 | def lru_cache(maxsize=128, typed=False): |
| 480 | """Least-recently-used cache decorator. |
| 481 | |
| 482 | If *maxsize* is set to None, the LRU features are disabled and the cache |
| 483 | can grow without bound. |
| 484 | |
| 485 | If *typed* is True, arguments of different types will be cached separately. |
| 486 | For example, f(3.0) and f(3) will be treated as distinct calls with |
| 487 | distinct results. |
| 488 | |
| 489 | Arguments to the cached function must be hashable. |
| 490 | |
| 491 | View the cache statistics named tuple (hits, misses, maxsize, currsize) |
| 492 | with f.cache_info(). Clear the cache and statistics with f.cache_clear(). |
| 493 | Access the underlying function with f.__wrapped__. |
| 494 | |
| 495 | See: https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU) |
| 496 | |
| 497 | """ |
| 498 | |
| 499 | # Users should only access the lru_cache through its public API: |
| 500 | # cache_info, cache_clear, and f.__wrapped__ |
| 501 | # The internals of the lru_cache are encapsulated for thread safety and |
| 502 | # to allow the implementation to change (including a possible C version). |
| 503 | |
| 504 | if isinstance(maxsize, int): |
| 505 | # Negative maxsize is treated as 0 |
| 506 | if maxsize < 0: |
| 507 | maxsize = 0 |
| 508 | elif callable(maxsize) and isinstance(typed, bool): |
| 509 | # The user_function was passed in directly via the maxsize argument |
| 510 | user_function, maxsize = maxsize, 128 |
| 511 | wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo) |
| 512 | wrapper.cache_parameters = lambda : {'maxsize': maxsize, 'typed': typed} |
| 513 | return update_wrapper(wrapper, user_function) |
| 514 | elif maxsize is not None: |
| 515 | raise TypeError( |
| 516 | 'Expected first argument to be an integer, a callable, or None') |
| 517 | |
| 518 | def decorating_function(user_function): |
| 519 | wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo) |
| 520 | wrapper.cache_parameters = lambda : {'maxsize': maxsize, 'typed': typed} |
| 521 | return update_wrapper(wrapper, user_function) |
| 522 | |
| 523 | return decorating_function |
| 524 | |
| 525 | def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo): |
| 526 | # Constants shared by all lru cache instances: |
no test coverage detected