Mastering the LRU Cache
Mastering the LRU Cache
Learn how an LRU (Least Recently Used) cache works to optimize performance by storing frequently accessed data and automatically evicting the oldest entries whe
In depth
An LRU (Least Recently Used) cache is a powerful optimization technique that stores the results of expensive function calls, preventing redundant computations. It's crucial for improving application performance by reducing latency and CPU load when dealing with frequently requested data or computationally intensive operations.
The Problem: Redundant Calculations
Without a cache, every time a function is called with the same arguments, it re-executes its entire logic. For computationally expensive functions, this leads to significant delays and wasted resources, even if the result for those inputs has been calculated before.
Basic Memoization with a Dictionary
At its core, a cache acts as a memoization table. A simple dictionary can store function inputs as keys and their corresponding outputs as values. Before performing a calculation, the function checks if the result for the current input already exists in this dictionary. If it does, the stored result is returned immediately, bypassing the expensive computation.
The Challenge: Finite Memory
While effective, a simple dictionary cache has a critical limitation: memory is finite. If the cache continuously stores new results without removing old ones, it will eventually consume excessive memory, leading to performance degradation or even application crashes.
Introducing LRU Eviction
To manage memory efficiently, caches employ eviction policies. The Least Recently Used (LRU) policy is one of the most common and effective: when the cache reaches its maximum size and a new item needs to be added, the item that has not been accessed for the longest time is removed (evicted) to make space.
How LRU Eviction Works
An LRU cache maintains an order of items based on their access time. When an item is accessed (either retrieved or added), it is marked as the most recently used. When the cache is full and a new item arrives, the item at the "least recently used" end of the order is evicted. This ensures that frequently accessed data remains in the cache.
function get_or_compute(key, compute_func):
if key in cache:
move key to front (most recently used)
return cache[key]
else:
if cache is full:
evict least recently used item
result = compute_func(key)
add key, result to cache
move key to front (most recently used)
return resultPython's `@lru_cache` Decorator
Python provides a built-in `functools.lru_cache` decorator that simplifies implementing an LRU cache. By simply adding `@lru_cache(maxsize=N)` above a function definition, Python automatically handles the caching logic, including storage, retrieval, and LRU-based eviction, up to a specified `maxsize`.
Key Takeaways
- LRU caches store results of expensive function calls to avoid re-computation.
- They use an eviction policy to remove the least recently used items when the cache is full.
- This balances performance gains with efficient memory usage.
- Python's `@lru_cache` decorator offers a simple, powerful way to apply this optimization.
Got a different question? SeaThru generates a fresh video for any topic where systems talk or data structures move.
Ask your own question →