How to memoize a function in Python with lru_cache
Use functools.lru_cache to memoize a recursive Fibonacci function, caching results for a fixed number of calls to avoid repeated computation.
Python code
12 linesfrom functools import lru_cache
@lru_cache(maxsize=128)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
if __name__ == "__main__":
for i in range(10):
print(f"fib({i}) = {fibonacci(i)}")
print(f"Cache info: {fibonacci.cache_info()}")
Output
fib(0) = 0
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
fib(6) = 8
fib(7) = 13
fib(8) = 21
fib(9) = 34
Cache info: CacheInfo(hits=8, misses=10, maxsize=128, currsize=10)
How it works
The @lru_cache decorator wraps fibonacci and stores the result of each call keyed by its arguments. When the same argument recurs, the cached result is returned instantly, avoiding re-computation of the entire subtree. maxsize=128 limits the cache to 128 entries, evicting the least recently used items when full. The cache_info() method reveals how many hits and misses occurred, providing transparency for tuning cache sizes. Because recursion now hits cached values, the runtime drops from exponential to linear.
Common mistakes
- Applying lru_cache to functions with unhashable arguments like lists or dicts
- Setting maxsize too small, causing frequent evictions and reduced benefit
- Forgetting that lru_cache also caches exceptions if they occur
- Not using @lru_cache on a function that mutates external state, producing stale results
Variations
- Use `@lru_cache(maxsize=None)` for an unlimited cache (though this may take memory).
- Replace with `@cache` from functools (Python 3.9+) which is equivalent to an unbounded cache.
Real-world use cases
- Memoizing database query helper functions that are called repeatedly with the same parameters.
- Caching network API responses keyed by request URL within a short-lived service process.
- Storing results of expensive mathematical calculations in data pipelines to avoid recomputation.
Sponsored
More from Caching & Redis
- Cache Asides in Python with a Read-Through Loader easy
- Cache Data in Redis with Python easy
- Cache Penetration Null Object Mock in Python medium
- Cache Stampede Prevention with SingleFlight in Python medium
- Cache Warming with Python: Preload Hot Keys easy
- Coalescing duplicate in-flight requests: one shared result for concurrent callers hard
Keep learning
Related tutorials and quizzes for this topic.