Advertisement
Advanced Python, functools & Functional Programming Tools

Why is caution needed when applying @lru_cache directly to instance methods?

Short answer

lru_cache stores a reference to every argument, including self, as part of the cache key.

Save Python mock interview
Advertisement
AModel answer
lru_cache stores a reference to every argument, including self, as part of the cache key. Applied to an instance method, this keeps instances alive as long as the cache holds entries, potentially causing memory leaks, and cache entries are shared across all instances by argument value rather than being per-instance.
from functools import lru_cache

class Service:
    @lru_cache(maxsize=None)
    def compute(self, x):  # caches (self, x) pairs -- keeps self alive
        return x * 2
Real-world example
A long-running server process notices memory growth traced to a cached instance method holding references to short-lived request objects via self.
Likely follow-ups
  • How can you cache per-instance without leaking with lru_cache?
  • What's an alternative using cached_property?
Related topics