Compare `functools.lru_cache` and `functools.cache`. When should you prefer the former over the latter in a production web server?
Python interview question for Advanced practice.
Answer
lrucache(maxsize=N): Implements a Least Recently Used cache with a fixed size. When the cache reaches N items, the oldest (least recently accessed) entry is evicted. cache: An unbounded cache. It stores every result forever as long as the process is running. Production Recommendation: In a long-running web server, you should almost always prefer lrucache with a specific maxsize. An unbounded cache can cause a Memory Leak if the input domain is large (e.g., caching database results for millions of unique user IDs). Over time, the Python process will consume all available RAM and crash (OOM). Use cache only for functions with a very small, finite set of possible inputs (e.g., config settings).
Explanation
functools.cache was introduced in Python 3.9 as a simpler alias for lrucache(maxsize=None).