LRU Cache
LRU Cache
Overviewβ
Least Recently Used (LRU) Cache is a caching mechanism that evicts the least recently used item when the cache reaches its capacity. It combines a HashMap for O(1) key lookup with a Doubly Linked List for O(1) order maintenance.
Design Patternβ
LRU Cache uses two data structures in combination:
- HashMap: Maps keys to linked list nodes for O(1) access
- Doubly Linked List: Maintains usage order, with most recently used at head
LRU Cache Structure:
HashMap: Doubly Linked List:
βββββββββββββββ HEAD <-> [MRU] <-> ... <-> [LRU] <-> TAIL