Sub topic 1
What a cache is and why it needs an eviction policy
Prerequisites
This chapter assumes you know basic Python syntax (classes, methods, dictionaries) and Big-O notation. You should also know what a hash map offers: average O(1) lookup, insert, and delete by key. Later chapters also use object references as pointers, but this chapter does not.
What a cache is
A cache is a small, fast store that keeps copies of data that is expensive to get. The original source might be a database, a network call, or a slow computation. When the program needs the data again, it checks the cache first.
- A cache hit means the key is in the cache. The program uses the stored value and skips the slow work.
- A cache miss means the key is absent. The program does the slow work and usually stores the result.
The pattern looks like this in Python:
import time
cache = {}
def slow_square(n):
time.sleep(0.5) # stands in for a database or network call
return n * n
def cached_square(n):
if n in cache:
return cache[n] # hit
result = slow_square(n) # miss
cache[n] = result
return result
print(cached_square(12)) # slow, miss
print(cached_square(12)) # fast, hitThe first call takes about half a second. The second returns at once.
Why memory limits force a choice
The dictionary above never removes anything. If the program sees millions of distinct keys, the cache grows until memory runs out. A cache is only useful if it stays small, so it needs a fixed capacity: the maximum number of entries it may hold.
Once the cache is full and a new key arrives, something has to go. Deciding what to remove is the job of an eviction policy. Eviction means removing an entry to make room.
Common eviction policies
Different policies make different guesses about which entry is least valuable.
| Policy | Evicts | Guess it makes |
|---|---|---|
| FIFO (first in, first out) | The oldest inserted entry | Age matters more than use |
| LFU (least frequently used) | The entry with the fewest accesses | Popular entries stay popular |
| LRU (least recently used) | The entry untouched for the longest time | Recently used entries will be used again soon |
| Random | Any entry | No guess, very cheap |
LRU is a common default because many workloads show temporal locality: data used a moment ago is likely to be used again soon. This tutorial builds an LRU cache.
What the finished cache must offer
The cache you will build has a small interface with two operations:
get(key)returns the stored value, orNoneif the key is absent. A hit also counts as a use of that key.put(key, value)stores or updates a value. If the cache is full and the key is new, it evicts the least recently used entry first.
Both operations should run in O(1) time. The next chapters explain why that goal is hard and which data structures meet it.
Common mistakes
- Using a plain dictionary as a cache with no size limit. It works in tests and fails in production when memory fills.
- Choosing a policy without looking at the access pattern. A one-time scan over many keys can flush useful entries from an LRU cache.
- Caching data that changes often, which returns stale values.
Exercise
A cache has capacity 3 and holds keys A, B, C. Key A was used most recently, then C, then B. Key D arrives. Which key does an LRU policy evict, and which key does a FIFO policy evict if the insertion order was A, B, C?
Show answer
LRU evicts B, because it was used least recently. FIFO evicts A, because it was inserted first.