LRU (Least Recently Used) Cache — eviction policy that removes least-recently-accessed items on overflow. Classic implementation: HashMap + doubly linked list for O(1) get/put. Redis default: `maxmemory-policy allkeys-lru`. Alternatives: LFU (Least Frequently Used), FIFO, random. LRU is universally good for web caching (recent content is hot).
Below: details, example, related terms, FAQ.
Free online tool — HTTP header checker: instant results, no signup.
// Python @lru_cache decorator
from functools import lru_cache
@lru_cache(maxsize=128)
def expensive(n): return n * n
// Redis LRU policy
maxmemory 1gb
maxmemory-policy allkeys-lruThe LRU (Least Recently Used) Cache is an effective eviction policy utilized in various caching mechanisms. The primary purpose of this policy is to optimize the use of limited memory resources by removing the least recently accessed items when the cache reaches its maximum capacity.
When implementing an LRU Cache, the algorithm maintains a record of the order in which items are accessed. This is typically achieved through a combination of a HashMap and a doubly linked list. The HashMap stores the keys and a reference to the corresponding nodes in the linked list, while the linked list maintains the order of access. When an item is accessed, it is moved to the front of the list. If the cache reaches its limit, the item at the end of the list (the least recently used) is removed.
LRU caching is particularly beneficial for applications that exhibit temporal locality, where recently accessed data is likely to be accessed again soon. This makes LRU cache an ideal choice for scenarios such as web applications, database queries, and file system caches.
In summary, the LRU Cache eviction policy is a strategic method for managing memory efficiently by prioritizing access patterns. Implementing this policy can lead to improved performance and resource utilization in systems that require rapid access to frequently used data.
Redis, a popular in-memory data structure store, supports LRU caching through its configuration settings. To implement an LRU Cache in Redis, you can adjust the maxmemory-policy setting to allkeys-lru or volatile-lru, depending on your needs. Here’s how you can do it:
CONFIG SET maxmemory 100mb CONFIG SET maxmemory-policy allkeys-lru SET key1 value1 SET key2 value2 GET key1 SET key3 value3 SET key4 value4 SET key5 value5 GET key2 GET key1 By configuring Redis to use an LRU eviction policy, you can effectively manage memory and ensure that the most relevant data remains accessible, optimizing performance in high-demand scenarios.
While LRU (Least Recently Used) is a widely adopted caching strategy, it is essential to compare it with other caching strategies to understand its advantages and limitations. Below are some common alternatives:
In conclusion, while LRU is a robust caching strategy for many applications, understanding the context and characteristics of your data access patterns is crucial in choosing the right caching mechanism. Each strategy has its trade-offs, and selecting the most suitable one can lead to significant performance improvements in your application.
LRU — recency matters. LFU — frequency matters. For web: LRU usually wins (recency correlates with future access). LFU better for static catalog queries.
Redis uses approximate LRU for efficiency. Sample 5 keys, evict oldest. Accurate enough for most use cases.
Different. TTL — absolute expiry (delete after N sec). LRU — eviction on memory pressure. Often combined: TTL on data + LRU for overflow.
Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.