Treehouse LRU Shelf
The fairy treehouse has only three hooks for magic ingredients, but the forest has hundreds. Build a shelf that always keeps the ingredients fairies are using most within reach.
π§ Before we start
Guild Union Find kept groups tidy. Today's LRU cache keeps a small, fast shelf of things you used lately. LRU means Least Recently Used: when the shelf is full, the thing nobody has touched for the longest time leaves first.
Build the shelf strategy
Tap the four cards in the order the treehouse follows every time a fairy asks for an ingredient.
Hit or miss?
A hit means the ingredient is already on the shelf. A miss means the treehouse must fetch it from the forest. Judge each request.
Refresh or evict
Read the request, then tap the hook that should change. A hit refreshes that item. A miss on a full shelf evicts the back item.
Why recency wins
Moss is requested again and again. Replay the same requests with two different rules, compare the hits, then lock in the better rule.
π What you learned
- A cache is a small, fast store that keeps copies of things you may need again.
- A hit means the item was already stored. A miss means the slow trip to fetch it.
- An LRU cache moves an item to the front every time it is used.
- When the cache is full, it evicts the item at the back: the one used longest ago.
- Removing whoever arrived first can throw away a busy item. Tracking recency keeps popular items close.