pub struct CacheSystem {
pub cache_manager: Arc<CacheManager>,
pub l1_cache: Option<Arc<L1Cache>>,
pub l2_cache: Option<Arc<L2Cache>>,
}Expand description
Main entry point for the Multi-Tier Cache system
Provides unified access to L1 (Moka) and L2 (Redis) caches with automatic failover, promotion, and stampede protection.
§Example
use multi_tier_cache::CacheSystem;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cache = CacheSystem::new().await?;
// Use cache_manager for all operations
let manager = cache.cache_manager();
Ok(())
}§Note on l1_cache and l2_cache Fields
When using multi-tier mode or custom backends, l1_cache and l2_cache
may be None. Always use cache_manager() for cache operations.
Fields§
§cache_manager: Arc<CacheManager>Unified cache manager (primary interface)
l1_cache: Option<Arc<L1Cache>>L1 Cache (in-memory, Moka) - None when using custom backends
l2_cache: Option<Arc<L2Cache>>L2 Cache (distributed, Redis) - None when using custom backends
Implementations§
Source§impl CacheSystem
impl CacheSystem
Sourcepub async fn new() -> Result<Self>
pub async fn new() -> Result<Self>
Create new cache system with default configuration
§Configuration
Redis connection is configured via REDIS_URL environment variable.
Default: redis://127.0.0.1:6379
§Example
use multi_tier_cache::CacheSystem;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Set environment variable (optional)
std::env::set_var("REDIS_URL", "redis://localhost:6379");
let cache = CacheSystem::new().await?;
Ok(())
}Sourcepub async fn with_redis_url(redis_url: &str) -> Result<Self>
pub async fn with_redis_url(redis_url: &str) -> Result<Self>
Create cache system with custom Redis URL
§Arguments
redis_url- Redis connection string (e.g., “redis://localhost:6379”)
§Example
use multi_tier_cache::CacheSystem;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cache = CacheSystem::with_redis_url("redis://custom:6379").await?;
Ok(())
}Sourcepub async fn health_check(&self) -> bool
pub async fn health_check(&self) -> bool
Perform health check on all cache tiers
Returns true if at least L1 is operational.
L2 failure is tolerated (graceful degradation).
§Example
use multi_tier_cache::CacheSystem;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cache = CacheSystem::new().await?;
if cache.health_check().await {
println!("Cache system healthy");
}
Ok(())
}Sourcepub fn cache_manager(&self) -> &Arc<CacheManager>
pub fn cache_manager(&self) -> &Arc<CacheManager>
Get reference to cache manager (primary interface)
Use this for all cache operations: get, set, streams, etc.
Trait Implementations§
Source§impl Clone for CacheSystem
impl Clone for CacheSystem
Source§fn clone(&self) -> CacheSystem
fn clone(&self) -> CacheSystem
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more