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.

Easy Python 3.9+ Aug 9, 2026 Caching & Redis 13 views 0 copies

Python code

12 lines
Python 3.9+
from 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

stdout
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

  1. Use `@lru_cache(maxsize=None)` for an unlimited cache (though this may take memory).
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Caching & Redis

Related tutorials and quizzes for this topic.