I have this simple cache decorator demo here
@functools.cache
def cached_fib(n):
assert n > 0
if n <= 2:
return 1
return cached_fib(n - 1) + cached_fib(n - 2)
t1 = time.perf_counter()
cached_fib(400)
t2 = time.perf_counter()
print(f"cached_fib: {t2 - t1}") # 0.0004117000003134308
I want to access the actual cache dictionary object inside this cached_fib, but when I try to access through cached_fib.cache, it gives me an error saying that AttributeError: 'functools._lru_cache_wrapper' object has no attribute 'cache' though the cache attribute is used in python and c version alike.
Thank you!
The internals of the cache are encapsulated for thread safety and to allow the underlying implementation details to change.
The three public attributes are, some statistics in
cache_info:Details about the init mode in
cache_parametersAnd
cache_clearwhich purges the cache.Additionally, the "uncached" function version is publicly available via
__wrapped__, but this is not something specific tofunctools.cache:If you want a cache implementation where the underlying dict is exposed to the user, I recommend the third-party cachetools library.