Crate multi_tier_cache

Crate multi_tier_cache 

Source
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? {
        println!("Cached data: {}", cached);
    }

    // Get statistics
    let stats = cache.cache_manager().get_stats();
    println!("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+L2

Re-exports§

pub use l1_cache::L1Cache;
pub use l2_cache::L2Cache;
pub use cache_manager::CacheManager;
pub use cache_manager::CacheStrategy;
pub use cache_manager::CacheManagerStats;

Modules§

cache_manager
Cache Manager - Unified Cache Operations
l1_cache
L1 Cache - Moka In-Memory Cache
l2_cache
L2 Cache - Redis Cache

Structs§

CacheSystem
Main entry point for the Multi-Tier Cache system