The lru_cache() decorator function is used to cache recently used values to improve the performance of a function. It works by storing the results of function calls, so that if the same arguments are used for a subsequent call, the cached result can be used instead of recomputing the result. This can significantly speed up the function, as it does not need to recalculate the results for the same arguments.

The LRU (Least Recently Used) algorithm is used to decide which values will be cached, and which will be discarded. The algorithm works by keeping a list of recently used values, and discarding the least recently used value when a new value is added to the cache if the maximum size has been reached..

@lru_cache(maxsize=128, typed=False)
def function_to_cache():
   #satements
maxsize An optional integer specifying the maximum number of recently used results that should be kept in the cache. It defaults to 128.
typed Specifies whether to cache separately the results when arguments are of different types, but are semantically equivalent.  For example if set to True, f(5) and f(5.0) will be cached separately, where f() is the decorated function.
import timeit

from functools import lru_cache

@lru_cache
def fibonacci(n): 
    if n <= 0: 
       return 0 
    elif n == 1: 
       return 1
    return fibonacci(n-1) + fibonacci(n-2) 

#test time taken for first and subsequent calls
print(timeit.timeit(lambda: [fibonacci(i) for i in range(100)], number = 1))
print(timeit.timeit(lambda: [fibonacci(i) for i in range(100)], number = 1))
print(timeit.timeit(lambda: [fibonacci(i) for i in range(100)], number = 1))

0.00010459999612066895
3.819999983534217e-05
3.9399994420818985e-05

With maxsize set

When we set the maxsize arguments, the function ensures that  the number of stored values does not exceed the specified size. When the maxsize is reached  the least used value in the cache queue are discarded to make room for the new value.

import timeit

from functools import lru_cache

@lru_cache(maxsize = 5)
def fibonacci(n): 
    if n <= 0: 
       return 0 
    elif n == 1: 
       return 1
    return fibonacci(n-1) + fibonacci(n-2) 

#test time taken for first and subsequent calls
print(timeit.timeit(lambda: [fibonacci(i) for i in range(100)], number = 1))
print(timeit.timeit(lambda: [fibonacci(i) for i in range(100)], number = 1))
print(timeit.timeit(lambda: [fibonacci(i) for i in range(100)], number = 1))

0.00020759999461006373
8.870000601746142e-05
0.00010399999155197293