Fixed-capacity cache: full → evict the least recently used. Does get change the order too? Click put/get, watch recency shift + hit rate. An interactive LRU…
Why it matters
LRU sits in browser and CDN caches, database caches, memoisation and the operating system’s page cache. One rule, "what was used recently will likely be used again", lets a small cache answer most requests. The key: get changes the order too.
The key idea
A Map whose insertion order is the order of use
Common mistakes
Getting a key that hits, and the next eviction order changes too A get refreshes the order of use: a hit moves the key to the newest end. That is what makes LRU work, not a strange side effect.
A home-made LRU gets slow once the cache capacity is made large Moving things in an array is O(n). Use a Map (ordered) or a hash map + doubly linked list for O(1) get and put.
LRU and FIFO look the same, but evict differently FIFO evicts what came IN first (access ignored); LRU evicts what was ACCESSED longest ago. LRU cares about use, FIFO only about arrival.
Try it in this playground
Get a hit Ask for a seed already on the bench.
Fill the bench until one is evicted The one sent back is the one used longest ago.
Refresh an old box A hit on a box that is not the newest moves it to the newest end.