CacheKit Docs

High-performance cache policies and supporting data structures.

View the Project on GitHub OxidizeLabs/cachekit

Second-Chance / Clock

Feature: policy-clock

Goal

Approximate LRU with lower overhead by avoiding strict recency ordering and linked list manipulation.

Core Idea

Maintain entries in a circular buffer (“clock”). Each entry has a reference bit.

Core Data Structures

Implementation uses ClockRing:

Operations

get(key)

insert(key, value)

evict()

contains(key)

remove(key)

Additional Operations (via as_ring())

Complexity & Overhead

Operation Time Notes
get O(1) Hash lookup + bit set
insert O(1)* *Amortized; eviction may sweep
contains O(1) Hash lookup only
remove O(1) Hash lookup + clear slot

Trade-offs vs True LRU

Aspect Clock True LRU
Access cost O(1) bit set O(1) list move
Memory layout Contiguous (cache-friendly) Scattered nodes
Eviction Approximate LRU Exact LRU
Overhead/entry ~1 byte (ref bit) ~16 bytes (2 pointers)

Thread Safety

Implementation Notes

References