Expand description
Cache Backend Traits
This module defines the trait abstractions that allow users to implement custom cache backends for both L1 (in-memory) and L2 (distributed) caches.
§Architecture
CacheBackend: Core trait for all cache implementationsL2CacheBackend: Extended trait for L2 caches with TTL introspectionStreamingBackend: Optional trait for event streaming capabilities
§Example: Custom L1 Backend
ⓘ
use multi_tier_cache::{CacheBackend, async_trait};
use std::time::Duration;
use anyhow::Result;
struct MyCustomCache {
// Your implementation
}
#[async_trait]
impl CacheBackend for MyCustomCache {
async fn get(&self, key: &str) -> Option<serde_json::Value> {
// Your implementation
}
async fn set_with_ttl(&self, key: &str, value: serde_json::Value, ttl: Duration) -> Result<()> {
// Your implementation
}
async fn remove(&self, key: &str) -> Result<()> {
// Your implementation
}
async fn health_check(&self) -> bool {
// Your implementation
}
}Traits§
- Cache
Backend - Core cache backend trait for both L1 and L2 caches
- L2Cache
Backend - Extended trait for L2 cache backends with TTL introspection
- Streaming
Backend - Optional trait for cache backends that support event streaming