Expand description
§mytheclipse-cache
A unified multi-layer cache abstraction that keeps your application from being locked to any single cache provider.
- L1 (in-process) caches:
memory::MemoryCache(zero-dependency, default) or [moka_cache::MokaL1] (high-performance, TTL/max-capacity). - L2 (distributed) caches: [
redis::RedisCache] backed by Redis/Valkey. - Multi-layer composition:
multilayer::MultiLayerCachelayers an L1 over an L2 behind oneCacheface; reads fall through to L2 and backfill L1. - Cache-aside / auto-refresh:
cache_aside::CacheAsidereads through to a data source on a miss and caches the result.
The core Cache trait is byte-oriented; typed convenience (JSON) is
layered on top via memory::typed::TypedCache.
§Example
Multi-layer + cache-aside composition (default features):
use mytheclipse_cache::{Cache, MemoryCache, MultiLayerCache, CacheAside};
let l1 = MemoryCache::new();
let l2 = MemoryCache::new(); // in a real app: a RedisCache
let cache = MultiLayerCache::new(l1, l2);
cache.set("user:1", b"payload".to_vec(), None).await.unwrap();
assert_eq!(cache.get("user:1").await.unwrap(), Some(b"payload".to_vec()));
// Cache-aside: fill misses from a source of truth.
let aside = CacheAside::new(
MemoryCache::new(),
|key| async move { Some(format!("data-for-{key}").into_bytes()) },
);
let _v = aside.get("orders:42").await.unwrap();Re-exports§
pub use traits::Cache;pub use traits::CacheError;pub use memory::MemoryCache;pub use cache_aside::CacheAside;pub use multilayer::MultiLayerCache;
Modules§
- auto_
refresh - Auto-refresh cache wrapper that proactively refreshes stale entries in the background, eliminating thundering-herd on cache miss.
- cache_
aside - Cache-aside with read-through (feature
cache-aside). - memory
- A simple, dependency-free in-process cache (L1,
l1-memory). - metrics
- Cache instrumentation metrics (hit/miss/eviction counters).
- multilayer
- Multi-layer (L1/L2) caching behind a single
Cacheface. - traits
- The core
CacheandKeyEncodertraits.