Tracking Recency with a Map and a List

A recency cache can combine a map for locating entries with an ordering structure for moving and evicting them.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a small cache
  2. Preserve both views
  3. Keep lookup and recency order synchronized
  4. Update existing keys without duplicating their order entries

Trace a small cache

With capacity two, insert A and B, read A, and insert C. Least-recently-used eviction removes B because reading A made A newer. A lookup map finds the entry, while neighbor links can detach and move it when located.

Preserve both views

After eviction, lookup by B should miss and the ordered sequence should have only A and C. Dictionary caches explain the role of the key in the retrieved result.[1]

Keep lookup and recency order synchronized

A cache with capacity two stores A and B in that use order. Looking up A moves A to the most recent end. Inserting C should then evict B. The lookup map locates the entry and the linked list records its position in recency order. See doubly linked list updates.

Least recent → most recent
[A,B] before reading A
[B,A] after reading A
Insert C, evict B → [A,C]
Map contains exactly A and C

Update existing keys without duplicating their order entries

Replacing A's value should reuse or remove its former list node according to the cache contract. A stale node can later trigger eviction of an active value. Test repeated inserts of one key, capacity one and a lookup that misses.

References

  1. Jeff Erickson, Algorithms ↩