The functools.lfru_cache decorator (and its unbounded counterpart, @cache) provides a powerful mechanism for memoization—an optimization technique that stores the results of expensive function calls and returns the cached result when the same inputs occur again. This is particularly effective for functions that are deterministic and computationally intensive, such as those involving recursion, complex calculations, or I/O operations that can be buffered.
How LRU Cache Works Internally The “LRU” in lru_cache stands for “Least Recently Used,” which describes its cache eviction policy. The decorator creates a dictionary that maps the function’s arguments to its return value. However, to prevent unbounded memory growth, the cache has a maximum size. When the cache is full and a new result needs to be stored, the least recently accessed entry (the one that hasn’t been used for the longest time) is discarded to make space. This is implemented efficiently using a doubly-linked list to track access order and a dictionary for fast lookups. The @cache decorator, introduced in Python 3.9, offers the same functionality but without a size limit, making it simpler to use when memory usage is not a concern, though this can be dangerous for functions with many possible inputs.