Expand description
Multi-Tier Cache
A high-performance, production-ready multi-tier caching library for Rust featuring:
- L1 Cache: In-memory caching with Moka (sub-millisecond latency)
- L2 Cache: Distributed caching with Redis (persistent storage)
- Cache Stampede Protection:
DashMap+ Mutex request coalescing - Redis Streams: Built-in support for event streaming
- Automatic L2-to-L1 Promotion: Intelligent cache tier promotion
- Comprehensive Statistics: Hit rates, promotions, in-flight tracking
§Quick Start
use multi_tier_cache::{CacheSystem, CacheStrategy};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Initialize cache system
let cache = CacheSystem::new().await?;
// Store data with cache strategy
let data = serde_json::json!({"user": "alice", "score": 100});
cache.cache_manager()
.set_with_strategy("user:1", data, CacheStrategy::ShortTerm)
.await?;
// Retrieve data (L1 first, then L2 fallback)
if let Some(cached) = cache.cache_manager().get("user:1").await? {
tracing::info!("Cached data: {}", cached);
}
// Get statistics
let stats = cache.cache_manager().get_stats();
tracing::info!("Hit rate: {:.2}%", stats.hit_rate);
Ok(())
}§Features
- Multi-Tier Architecture: Combines fast in-memory (L1) with persistent distributed (L2) caching
- Cache Stampede Protection: Prevents duplicate computations during cache misses
- Redis Streams: Publish/subscribe with automatic trimming
- Zero-Config: Sensible defaults, works out of the box
- Production-Proven: Battle-tested at 16,829+ RPS with 5.2ms latency
§Architecture
Request → L1 Cache (Moka) → L2 Cache (Redis) → Compute/Fetch
↓ Hit (90%) ↓ Hit (75%) ↓ Miss (5%)
Return Promote to L1 Store in L1+L2Re-exports§
pub use backends::DashMapCache;pub use backends::L1Cache;pub use backends::L2Cache;pub use backends::MokaCache;pub use backends::MokaCacheConfig;pub use backends::RedisCache;pub use builder::CacheSystemBuilder;pub use cache_manager::CacheManager;pub use cache_manager::CacheManagerStats;pub use cache_manager::CacheStrategy;pub use cache_manager::CacheTier;pub use cache_manager::TierConfig;pub use cache_manager::TierStats;pub use invalidation::InvalidationConfig;pub use invalidation::InvalidationMessage;pub use invalidation::InvalidationPublisher;pub use invalidation::InvalidationStats;pub use invalidation::InvalidationSubscriber;pub use redis_streams::RedisStreams;pub use traits::CacheBackend;pub use traits::L2CacheBackend;pub use traits::StreamingBackend;
Modules§
- backends
- Cache Backend Implementations
- builder
- Cache System Builder
- cache_
manager - Cache Manager - Unified Cache Operations
- invalidation
- Cache invalidation and synchronization module
- redis_
streams - Redis Streams Integration
- traits
- Cache Backend Traits
Structs§
- Cache
System - Main entry point for the Multi-Tier Cache system