42 – Bounded cache#
If you have a deterministic function with no side-effects that gets called very often, consider caching its results. If said function lives in a long-running application (e.g., a web server), make sure you don’t run out of memory by ensuring the cache has a maximum sizes.
You can do both of these things with the decorator functools.lru_cache, which accepts the cache size as an argument.
For example, @lru_cache(1024) in the snippet below creates a cache that saves up to 1024 different call results.
from functools import lru_cache
@lru_cache(1024)
def function_to_cache(*args): ...
# Some function calls...
print(function_to_cache.cache_info().currsize) # 35
print(function_to_cache.cache_info())
# CacheInfo(hits=12, misses=35, maxsize=1024, currsize=35)
You can access cache information by using the method .cache_info that is added to the function that gets a cache.