skp-cache
Advanced, modular caching library for Rust with dependency graph invalidation, stampede protection, and framework integrations.
✨ Features
- Multi-tier caching — L1 Memory + L2 Redis with automatic promotion
- Dependency graph invalidation — Cascade invalidation when parent entries change
- Stampede protection — Request coalescing (singleflight) prevents thundering herd
- Stale-while-revalidate — Serve stale data while refreshing in background
- Pluggable serialization — JSON (default), MessagePack, Bincode
- Metrics integration — First-class observability via
CacheMetricstrait - Framework support — Native Axum middleware + extractors
- TTL jitter — Prevents synchronized expiration storms
📦 Installation
[]
= "0.1"
# Optional features
= { = "0.1", = ["redis", "axum", "msgpack"] }
Available Features
| Feature | Description |
|---|---|
memory |
In-memory backend (default) |
redis |
Redis backend with connection pooling |
multitier |
L1 + L2 multi-tier caching |
json |
JSON serialization (default) |
msgpack |
MessagePack serialization |
bincode |
Bincode serialization |
compression |
Zstd compression support |
metrics |
Metrics crate integration |
🚀 Quick Start
use *;
async
🔗 Dependency Graph Invalidation
Link cache entries so invalidating a parent cascades to dependents:
// User depends on tenant
cache.set.await?;
// Posts depend on user
cache.set.await?;
// Invalidating tenant cascades to user and posts!
let count = cache.invalidate.await?;
// count == 3 (tenant + user + posts)
🛡️ Stampede Protection
Concurrent requests for the same missing key trigger only ONE computation:
// 1000 concurrent calls = 1 database query
let user = cache.get_or_compute.await?;
⏱️ Stale-While-Revalidate
Serve slightly stale data instantly while refreshing in background:
cache.set.await?;
🌐 Axum Integration
use ;
let app = new
.with_state
.route
.layer;
async
📊 Metrics
Integrate with any metrics system:
use ;
let cache = with_serializer_and_metrics;
// Emits: skp_cache_hits_total, skp_cache_misses_total, etc.
📁 Examples
Run the examples to see features in action:
# Dependency graph invalidation
# Request coalescing
# Stale-while-revalidate
# Axum integration
📈 Benchmarks
Performance benchmarks for serialization, compression, and cache operations.
Quick Summary
| Component | Recommendation | Performance |
|---|---|---|
| Serializer | Bincode | 2-3x faster than JSON |
| Compression | Zstd L1 | 86% size reduction, ~3-11 µs overhead |
| Cache Hit | Memory backend | ~700 ns (1.4M ops/s) |
Serialized Size Comparison
| Format | Size | Reduction |
|---|---|---|
| JSON | 15,172 B | - |
| MsgPack | 13,564 B | 10.6% |
| Bincode | 13,592 B | 10.4% |
| JSON + Zstd | 2,069 B | 86.4% |
See BENCHMARK.md for detailed analysis and recommendations.
# Run all benchmarks
🏗️ Architecture
┌─────────────────────────────────────────────────────┐
│ Application │
│ (Axum Middleware / Standalone / Actix) │
└───────────────────────┬─────────────────────────────┘
│
┌───────────────────────▼─────────────────────────────┐
│ CacheManager │
│ ┌─────────┐ ┌───────────┐ ┌──────────────────┐ │
│ │Coalescer│ │ Serializer│ │ Metrics Collector│ │
│ └─────────┘ └───────────┘ └──────────────────┘ │
└───────────────────────┬─────────────────────────────┘
│
┌───────────────────────▼─────────────────────────────┐
│ Backends │
│ ┌─────────┐ ┌─────────┐ ┌───────────────────┐ │
│ │ Memory │ │ Redis │ │ MultiTier │ │
│ │(DashMap)│ │(bb8+Lua)│ │ (L1 Mem + L2 Red) │ │
│ └─────────┘ └─────────┘ └───────────────────┘ │
└─────────────────────────────────────────────────────┘
📜 License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.