Cache expensive function results automatically with the built-in cache decorator.
Code Snippet
from functools import cache
import time
@cache
def expensive_computation(n: int) -> int:
"""Simulate expensive calculation."""
time.sleep(0.1) # Pretend this is slow
return sum(i * i for i in range(n))
# First call: takes 0.1 seconds
result1 = expensive_computation(1000)
# Second call: instant (cached)
result2 = expensive_computation(1000)
# Check cache stats
print(expensive_computation.cache_info())
# CacheInfo(hits=1, misses=1, maxsize=None, currsize=1)
Why This Helps
- Zero-effort memoization for pure functions
- Built-in cache statistics for monitoring
- Thread-safe implementation
How to Test
- Compare execution times for repeated calls
- Monitor cache_info() hit rates
When to Use
Recursive algorithms, API response caching, any pure function called repeatedly with same args.
Performance/Security Notes
Use @lru_cache(maxsize=N) to limit memory. Arguments must be hashable.
References
Try this tip in your next project and share your results in the comments!
Discover more from Byte Architect
Subscribe to get the latest posts sent to your email.