There is no specification for life time, only for size, the @functools.lru_cache
works as a dictionary, so as long as your application and function with the decorator are there the feature will exist, so it is recommended to use the maxsize=
with a size not too large (default is 128), to avoid uncontrolled use, if set maxsize=None
LRU feature will be disabled and cache can grow without limits, which can cause serious problems.
Note that if using typed=true
, the arguments of the function of different types will be cached separately, example: foobar(3)
and foobar(3.0)
will be treated as separate calls with distinct results, which will require more resources, so it is good to assess what is needed.
There is also the method .clear_cache
, with it at any time you can invalidate the cache of a function, example:
@lru_cache
def foobar():
return ..algoaqui..
foobar() # retorna o resultado
foobar() # retorna o mesmo resultado, possivelmente em cache
# descarta o cache existente para "foobar()"
foobar.clear_cache()
In the LRU algorithm used by this decorator, will discard the least recently used items first.
If you need a cache that lives even after the application restarts, consider Redis: https://redis.io
– vinibrsl