Eviction Strategies
When cache is full (maxSize reached), which entry should be removed?
LRU (Least Recently Used)
Remove the entry that was accessed longest ago.
Access order: A → B → C → A → D
Cache size: 3
State after D:
A: lastAccess=4 (recent)
C: lastAccess=3 (recent)
D: lastAccess=5 (recent)
B: lastAccess=2 (oldest) ← EVICTBest for: General purpose, most web applications.
typescript
const node = new CacheNode("node-1", {
maxSize: 1000,
evictionPolicy: "lru", // default
});LFU (Least Frequently Used)
Remove the entry with fewest accesses.
Access counts:
A: accessed 10 times
B: accessed 2 times ← EVICT
C: accessed 8 timesBest for: Workloads with clear "hot" and "cold" keys.
typescript
const node = new CacheNode("node-1", {
maxSize: 1000,
evictionPolicy: "lfu",
});FIFO (First In, First Out)
Remove the oldest entry regardless of access.
Insertion order: A → B → C → D
Cache size: 3
After D:
A: inserted 1st ← EVICT
B: inserted 2nd
C: inserted 3rd
D: inserted 4thBest for: Simple workloads, streaming data.
typescript
const node = new CacheNode("node-1", {
maxSize: 1000,
evictionPolicy: "fifo",
});Comparison
| Strategy | Speed | Memory | Use Case |
|---|---|---|---|
| LRU | Fast | Good | General purpose |
| LFU | Medium | Best | Hot key patterns |
| FIFO | Fastest | Fair | Simple workloads |
TTL (Time To Live)
In addition to eviction, entries can expire automatically:
typescript
// Auto-expire after 60 seconds
node.set("session:abc", data, 60000);
// Background sweep every 30 seconds removes expired entries