CacheKit Docs

High-performance cache policies and supporting data structures.

View the Project on GitHub OxidizeLabs/cachekit

LIFO (Last In, First Out)

Feature: policy-lifo

Goal

Evict the most recently inserted entry.

Core Idea

LIFO uses a stack structure:

Think of it like a stack of plates - you remove the one you just added.

Core Data Structures

In cachekit, src/policy/lifo.rs uses:

Operations

get(key)

insert(key, value)

Eviction Logic

Complexity & Overhead

When to Use

LIFO is a niche policy - only use when you have specific evidence that newest items are least valuable:

Use LIFO when:

Avoid LIFO when:

Examples

When LIFO Makes Sense

Undo Buffer Pattern:
  - User does Action A (cached)
  - User does Action B (cached)
  - User does Action C (cached)
  - User hits Undo → Action C discarded (most recent)

  LIFO is perfect: most recent action discarded first

When LIFO Is Bad

Typical Access Pattern:
  - Insert page A
  - Insert page B
  - Insert page C
  - Access page C again ← but it was just evicted!

  LIFO evicted the item we just added and immediately needed
  LRU/FIFO would be much better

Comparison with Other Policies

Policy Evicts Good For Common?
FIFO Oldest insert Predictable order Common
LIFO Newest insert Temporary/undo buffers Very rare
LRU Least recent Temporal locality Very common
Random Random Baseline Rare

LIFO is the opposite of FIFO and much less intuitive for most use cases.

Implementation in cachekit

Stack-based with Vec:

Use Case Examples

  1. Undo/Redo Management
    • Recent operations at top of stack
    • Undo pops most recent
    • LIFO natural fit
  2. Temporary Scratch Space
    • Newest allocations are temporary
    • Can be discarded if needed
    • LIFO prevents evicting stable data
  3. Batch Processing
    • Process items in batches
    • Newest batch items are speculative
    • LIFO protects earlier batch results

Safety / Invariants (Rust)

References