Для проверки вы можете использовать cache_info()
метод упакованной функции:
from functools import lru_cache
@lru_cache()
def my_function(foo):
return foo * 2
def cache(foo, use_cache=False):
if use_cache is False:
return my_function.__wrapped__(foo)
return my_function(foo)
print(cache(10, use_cache=True)) # cache miss
print(cache(10, use_cache=True)) # cache hit
print(cache(10, use_cache=False)) # bypass
print(cache(20, use_cache=False)) # bypass
print(my_function.cache_info()) # cache size=1, hit=1, miss=1
Печать:
20
20
20
40
CacheInfo(hits=1, misses=1, maxsize=128, currsize=1)