This page documents the various cache implementation data structures available in the repository. Caches are optimized temporary storage mechanisms that improve performance by keeping frequently or recently accessed data readily available. This documentation covers the different cache eviction policies implemented in the codebase and explains their internal workings, usage patterns, and performance characteristics.
The repository implements three primary cache strategies, each with different eviction policies:
Additionally, related probabilistic and distributed data structures are documented:
Sources: src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java26 src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java42 src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java19 src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java23
When a cache reaches its capacity, it must remove (evict) existing entries to make room for new ones. The strategy used to decide which entries to remove is called the eviction policy.
The following table compares the different eviction strategies implemented in the repository:
| Cache Type | Eviction Policy | Best Use Cases | Implementation |
|---|---|---|---|
| LFU Cache | Removes the least frequently accessed items first | When access frequency matters more than recency | Uses a frequency counter and a doubly-linked list |
| LRU Cache | Removes the least recently accessed items first | General purpose caching with temporal locality | Uses a doubly-linked list to track access order |
| MRU Cache | Removes the most recently accessed items first | When older items are more likely to be accessed again | Similar to LRU but with inverse eviction logic |
Sources: src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java7-11 src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java7-12 src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java7-11
The LFU Cache evicts the least frequently used items when it reaches capacity. Each item has a frequency counter that increases when the item is accessed. When the cache is full and a new item needs to be added, the item with the lowest frequency count is evicted src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java101-103
Sources: src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java33-52 src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java54-57
The LFUCache class provides O(1) time complexity for both get and put operations by using a HashMap for quick access and a doubly linked list for maintaining frequency order src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java13-15
this.head.key (the least frequently used item) src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java108-124Sources: src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java89-184
The LRU Cache evicts the least recently used items when it reaches capacity. Each time an item is accessed, it's moved to the "most recently used" position (the tail) src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java25-26
Sources: src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java42-48 src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java186-234
The LRUCache uses a HashMap for O(1) lookups and a doubly-linked list for tracking access order src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java23-24
moveNodeToLast(entry) and returns the value src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java107-114data.size() == cap), it calls evict() to remove the head entry before adding the new one src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java148-167head node, which represents the least recently used item src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java77-86Sources: src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java77-184
The MRU Cache evicts the most recently accessed items first. This strategy is useful in scenarios where the most recently used data is the least likely to be needed again in the immediate future src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java9-11
The MRUCache structure is identical to the LRUCache but modifies the evict() logic.
MRUCache, this method removes the tail node, which represents the most recently used item src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java76-87Sources: src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java76-87 src/main/java/com/thealgorithms/datastructures/caches/MRUCache.java112-130
The LWWElementSet is a state-based Conflict-free Replicated Data Type (CRDT) designed for distributed environments. It uses timestamps to resolve conflicts between concurrent additions and removals src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java8-13
addSet with Instant.now() src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java44-46removeSet with Instant.now() src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java54-56addSet and either not in the removeSet or its add timestamp is strictly after the remove timestamp src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java68-73resolveConflict src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java83-90Sources: src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java23-104
| Operation | LFU Cache | LRU Cache | MRU Cache | LWW Set |
|---|---|---|---|---|
| Lookup (get) | O(1) src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java13 | O(1) src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java11 | O(1) | O(1) |
| Insertion (put) | O(1) src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java13 | O(1) src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java11 | O(1) | O(1) |
| Eviction | O(n) (list search)* | O(1) (head) | O(1) (tail) | N/A |
* Note: While LFUCache claims O(1), the current implementation of addNodeWithUpdatedFrequency iterates through the list to find the correct frequency position, which can be O(n) in the worst case src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java132-159
Sources: src/main/java/com/thealgorithms/datastructures/caches/LFUCache.java13-15 src/main/java/com/thealgorithms/datastructures/caches/LRUCache.java11-12 src/main/java/com/thealgorithms/datastructures/crdt/LWWElementSet.java68-73
Refresh this wiki