Design a data structure to implement Least Recently Used (LRU)
LRU : Discards the least recently used items first.
Operations
- Insert(key, value)
- find(key)
Logic
Algorithm should keep track of which was used when, If the cache limit is reached and we want to insert one more we need to evict least recently used item from cache and insert the new item
Implementation
Data structures used:
- Doubly linked list : maximum size is equal to cache size, The most recently used will be near front end and least recently pages will be near rear end.
- Hash : with key as key and value as pointer to doubly linked list node