Interview frequency: …
Short answer
It stores function results by their arguments up to a limit (maxsize), and suits pure functions whose arguments are hashable and whose result does not depend on changing external state.
Explanation
A cache is not suitable for results that depend on time, the current user, or a database that changes without an invalidation mechanism. Use cache_info() to monitor the hit rate and cache_clear() for invalidation. On methods the cache keeps a reference to self, which prevents objects from being freed; use functools.cached_property or a class-level cache instead. Beware of returning mutable objects, because all callers will share the same object.
Code example
from functools import lru_cache
@lru_cache(maxsize=1024)
def shipping_cost(country: str, weight_kg: int) -> float:
return expensive_rate_calculation(country, weight_kg)
shipping_cost("EG", 2)
print(shipping_cost.cache_info())
# CacheInfo(hits=0, misses=1, maxsize=1024, currsize=1)Common mistakes
- Putting lru_cache on a method, so every instance is kept in memory.
- Caching a result that depends on the current user, so one user sees another user's data.