Episode 13
Only Two Jars on the Shelf
A repeated request affects the cached copy that gets evicted using least recently used.




















Episode transcript
Only two shelf spaces
Robot is standing next to this giant supply cabinet and this little shelf with only 2 spots on it. Duck is watching from the monitor.
CACHE CAPACITY: 2 COPIES. ORIGINALS STAY IN CUPBOARD
Duck: Can our fav colors be near us?
A = AMBER. REQUEST A: MISS
Robot: Amber is not on the shelf.
The source is still in the cupboard.
LEAST → MOST RECENT: [A]
Duck: One cupboard trip.
Robot: Room for one more.
Fill the nearby shelf
B = BLUE. REQUEST B: MISS
Duck: Blue, please.
The original blue is still in the cupboard.
LEAST → MOST RECENT: [A,B]
Robot: Another trip to the cupboard.
LAST USE: A REQUEST1, B REQUEST2
Duck: Blue was used most recently.
Duck asks for AMBER yet again. He points to the existing shelf copy.
REQUEST A AGAIN
Duck: Amber again.
A hit changes recency
Robot is directly accessing the shelf amber copy. The cupboard is still closed. This is a cache hit.
A: HIT. NO CUPBOARD FETCH
Robot: It is already here.
Since A is re-used the order of least to most recent becomes B then A. Nothing is fetched again.
LEAST → MOST RECENT: [B,A]
Duck: Amber is newest for us in order.
LAST USE: B REQUEST2, A REQUEST3
Robot: It's not about the time of arrival
C = CORAL. REQUEST C: MISS
Duck: Now coral.
Make room for coral
CACHE FULL: [B,A]. HOLD C OUTSIDE
Robot: I need to make space.
LEAST → MOST RECENT: [B,A]
The original blue supply is still in the cupboard.
EVICT B COPY. ORIGINAL B REMAINS
Duck: Blue. It's the one that hasn't been used most recently.
Copies and originals
So now we have A and C in the cache. A is least recent, C is most recent.
INSERT C. LEAST → MOST RECENT: [A,C]
Robot: Amber and coral fit.
ORIGINALS A,B,C REMAIN AT SOURCE
Duck: Blue is still in the cupboard.
Robot sees 3 cupboard retrievals and 1 direct shelf use.
FIRST FOUR REQUESTS: A,B,A,C → 3 MISSES,1 HIT
Robot: Three misses. One hit.
Duck: Keep that little shelf near my next masterpiece
A least recently used cache
A cache has some number of copies, which may be reused.
If you get a hit, you already have the copy you want.
If you get a miss, you fetch it from the source.
We'll just use the least recently used strategy for eviction. So, each time something is used, we mark it as more recent (even if we get a hit). If the cache is full, and we need a new copy, we'll get rid of the least recently used copy.
The original source is not destroyed when we get rid of the cached copy.
Recency is different from total number of uses.
And a real cache would have to have some rules on how to handle changing values in the source.
Request an evicted item again
Answer
Then you request B. It misses. That's because you just lost it due to eviction. Now A is least recent. So you swap A for B. This leaves you with C B.
FIFTH REQUEST B: MISS. EVICT A COPY. LEAST → MOST: [C,B]. STOP AFTER REQUEST5.
Notice that the original A,B,C values are still available at the source.
Sources
Python documentation, OrderedDict examples and recipes: LRU caches (opens in a new tab)