cachekit
High-performance cache policies and supporting data structures for Rust systems with optional metrics and benchmarks.
Why CacheKit
- Pluggable eviction policies with predictable performance characteristics.
- Unified builder API plus direct access for policy-specific operations.
- Optional metrics and benchmarks to validate trade-offs.
Table of Contents
- Overview
- Main Features
- Feature Flags
- Installation
- Quick Start
- Available Policies
- Policy Selection Guide
- Direct Policy Access
- Documentation
- Next Steps
Overview
CacheKit is a Rust library that provides:
- High-performance cache replacement policies (e.g., FIFO, LRU, LRU-K, Clock, NRU, S3-FIFO, SLRU, 2Q, and more).
- Supporting data structures and policy primitives for building caches.
- Optional metrics and benchmark harnesses.
- A modular API suitable for embedding in systems where control over caching behavior is critical.
This crate is designed for systems programming, microservices, and performance-critical applications.
Features
- Policy implementations optimized for performance and predictability.
- Optional integration with metrics collectors (e.g., Prometheus/metrics crates).
- Benchmarks to compare policy performance under real-world workloads.
Installation
Add cachekit as a dependency in your Cargo.toml:
[]
= "0.3.0"
From source:
[]
= { = "https://github.com/OxidizeLabs/cachekit" }
Quick Start
Using the Builder (Recommended)
The CacheBuilder provides a unified API for creating caches with any eviction policy:
use ;
Feature Flags
General
| Feature | Enables |
|---|---|
metrics |
Hit/miss metrics and snapshots |
concurrency |
Concurrent wrappers (requires parking_lot) |
Per-Policy (Eviction Policies)
Each eviction policy is gated behind a feature flag. Use default-features = false and enable only the policies you need for smaller builds.
| Feature | Policy | Description |
|---|---|---|
policy-fifo |
FIFO | First In, First Out |
policy-lru |
LRU | Least Recently Used (Arc-wrapped, concurrent wrapper available) |
policy-fast-lru |
Fast LRU | Optimized single-threaded LRU (~7–10× faster than LRU) |
policy-lru-k |
LRU-K | Scan-resistant with K-th access |
policy-lfu |
LFU | Least Frequently Used (bucket-based) |
policy-heap-lfu |
Heap LFU | LFU with heap-based eviction |
policy-two-q |
2Q | Two-Queue |
policy-s3-fifo |
S3-FIFO | Scan-resistant three-queue FIFO |
policy-arc |
ARC | Adaptive Replacement Cache |
policy-lifo |
LIFO | Last In, First Out |
policy-mfu |
MFU | Most Frequently Used |
policy-mru |
MRU | Most Recently Used |
policy-random |
Random | Random eviction |
policy-slru |
SLRU | Segmented LRU |
policy-clock |
Clock | Second-chance clock |
policy-clock-pro |
Clock-PRO | Scan-resistant clock |
policy-nru |
NRU | Not Recently Used |
Convenience: policy-all enables every policy above.
# Minimal build: only LRU and S3-FIFO
= { = "0.3", = false, = ["policy-lru", "policy-s3-fifo"] }
Available Policies
All policies are available through the unified builder API. Enable the corresponding feature flag for each policy (e.g. policy-lru for CachePolicy::Lru). See Feature Flags above.
use ;
// FIFO - First In, First Out
let fifo = new.;
// LRU - Least Recently Used
let lru = new.;
// LRU-K - Scan-resistant LRU (K=2 is common)
let lru_k = new.;
// LFU - Least Frequently Used (bucket-based, O(1))
let lfu = new.;
// HeapLFU - Least Frequently Used (heap-based, O(log n))
let heap_lfu = new.;
// 2Q - Two-Queue with configurable probation fraction
let two_q = new.;
// S3-FIFO - Scan-resistant FIFO with small + ghost ratios
let s3_fifo = new.;
// LIFO - Last In, First Out (stack-like eviction)
let lifo = new.;
// MFU - Most Frequently Used (evicts hot items)
let mfu = new.;
// MRU - Most Recently Used (evicts recently accessed)
let mru = new.;
// Random - Uniform random eviction
let random = new.;
// SLRU - Segmented LRU with probationary/protected segments
let slru = new.;
// Clock - Approximate LRU with reference bits (lower overhead)
let clock = new.;
// Clock-PRO - Scan-resistant Clock variant
let clock_pro = new.;
// NRU - Not Recently Used (simple reference bit tracking)
let nru = new.;
Policy Selection Guide
| Policy | Best For | Eviction Basis |
|---|---|---|
| FIFO | Simple, predictable workloads | Insertion order |
| LRU | Temporal locality | Recency |
| LRU-K | Scan-resistant workloads | K-th access time |
| LFU | Stable access patterns | Frequency (O(1)) |
| HeapLFU | Large caches, frequent evictions | Frequency (O(log n)) |
| 2Q | Mixed workloads | Two-queue promotion |
| S3-FIFO | Scan-heavy workloads | FIFO + ghost history |
| LIFO | Stack-like caching | Reverse insertion order |
| MFU | Inverse frequency patterns | Highest frequency |
| MRU | Anti-recency patterns | Most recent access |
| Random | Baseline/uniform distribution | Random selection |
| SLRU | Scan resistance | Segmented LRU |
| Clock | Low-overhead LRU approximation | Reference bits + hand |
| ClockPro | Scan-resistant Clock variant | Clock + ghost history |
| NRU | Simple coarse tracking | Reference bits (binary) |
See Choosing a policy for benchmark-driven guidance.
Direct Policy Access
For advanced use cases requiring policy-specific operations, use the underlying implementations directly:
use Arc;
use LruCore;
use ;