CacheKit Docs

High-performance cache policies and supporting data structures.

View the Project on GitHub OxidizeLabs/cachekit

LRU operational spec

Spec maturity: reference, tla

Executable oracle: tests/abstract_models/exact/lru.rs (LruOccupancyModel); independent reference: reference/lru.rs (NaiveLruModel). Optional TLA+: Lru.tla — run scripts/run-lru-tlc.sh; see formal/lru/tlc.md.

Least Recently Used cache replacement: evict the key whose most recent access is oldest when space is needed. This spec is independent of LruCore / FastLru internals; implementations must refine it.

State

Two equivalent formulations:

  1. Deque (MRU-first): order: Seq<K> listing resident keys; front = MRU, back = LRU.
  2. Timestamps: access: Map<K, ℕ> last-access time per resident key; monotonic clock incremented once per op that records access.

This document uses deque notation; the reference model uses timestamps.

Init

Observables

Observable Definition
resident Keys in order
peek_victim Back of order (LRU), or none if empty
recency_rank(k) Index of k in order (0 = MRU); undefined if absent
hit Per op: MustHit / MustMiss from membership

Timestamp recency rank: sort resident keys by (access[k] desc, k asc); rank = index of k.

Operations

Insert(k)

  1. If k ∈ resident: promote to MRU (move to front / set access[k] = ++clock).
  2. Else if |resident| ≥ capacity: evict LRU (pop back / min timestamp), record evicted_on_insert.
  3. Insert k at MRU (push front / assign new timestamp).

Get(k) / GetMut(k)

Peek(k)

Touch(k)

Remove(k)

EvictOne

Tie-breaks

Harness Op mapping

Op Cache API Side effects
Insert(k) insert(k, v) Promote if resident; else insert/evict
Get(k) get(k) Promote on hit
Peek(k) peek(k) No promotion
GetMut(k) No-op in adapter (LRU tests)
Touch(k) touch(k) Promote on hit
Remove(k) remove(k) Remove
EvictOne evict_one() Evict LRU

Align with trait hierarchy: Peek must not change recency_rank.

References