pythonbeginner

functools.cache and lru_cache

Memoize expensive function calls with functools.cache and lru_cache for automatic result caching.

python
from functools import cache, lru_cache

# Unlimited cache (Python 3.9+)
@cache
def fibonacci(n: int) -> int:
    if n < 2:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)

# LRU cache with max size
@lru_cache(maxsize=256)
def fetch_config(key: str) -> dict:
    # Simulated expensive lookup
    configs = {
        "db": {"host": "localhost", "port": 5432},
        "cache": {"host": "localhost", "port": 6379},
    }
    return configs.get(key, {})

# Cache info
print(fibonacci(100))
print(fibonacci.cache_info())

# Clear cache
fetch_config.cache_clear()

Use Cases

  • Recursive algorithms
  • API response caching
  • Expensive computations

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.