The decorator functools.lru_cache named so because of the unde | Python etc
The decorator functools.lru_cache named so because of the underlying cache replacement policy. When the cache size limit is reached Least Recently Used records removed first:
Python 3.9 introduced functools.cache which is the same as lru_cache(maxsize=None) but a little bit faster because it doesn't have all that LRU-related logic inside:
from functools import cache
@cache def fib_cache(n): if n <= 2: return 1 return fib(n-1) + fib(n-2)
fib_cache(30) # 832040
%timeit fib(30) # 63 ns ± 0.574 ns per loop
%timeit fib_cache(30) # 61.8 ns ± 0.409 ns per loop