Python › functools & Functional Programming Tools
What is the difference between functools.cache and functools.lru_cache?
BeginnerAnswer
functools.cache (added in Python 3.9) is a simpler, unbounded version of lru_cache -- equivalent to lru_cache(maxsize=None). Use cache when you want unlimited caching with simpler syntax, and lru_cache when you need to bound memory usage with maxsize or need typed caching.
from functools import cache
@cache
def factorial(n):
return 1 if n <= 1 else n * factorial(n-1)
print(factorial(10))Real-world example A configuration loader caches parsed config file contents with @cache since the file rarely changes during a program's run.