Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
lru_cache memoizes a function by its arguments and evicts the least recently used entries beyond maxsize; arguments must be hashable, results are shared between callers, and cache on methods can keep instances alive.
Decorating a pure function with @lru_cache(maxsize=1024) stores results keyed by the call arguments, so repeated calls return instantly, which is ideal for expensive deterministic computations. maxsize=None or functools.cache makes the cache unbounded, which can grow without limit. Arguments must be hashable, so lists or dicts cannot be passed. The cached result object is returned to every caller, so mutating it corrupts later results. Using lru_cache on instance methods includes self in the key and keeps those instances alive. There is no time-based expiry, so data that changes, such as database or API results, needs a TTL cache or an external cache like Redis.