pub trait L2CacheBackend: CacheBackend {
// Required method
fn get_with_ttl<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Option<(Value, Option<Duration>)>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
}Expand description
Extended trait for L2 cache backends with TTL introspection
This trait extends CacheBackend with the ability to retrieve both a value
and its remaining TTL. This is essential for implementing efficient L2-to-L1
promotion with accurate TTL propagation.
§Use Cases
- L2-to-L1 promotion with same TTL
- TTL-based cache warming strategies
- Monitoring and analytics
§Example
use multi_tier_cache::{CacheBackend, L2CacheBackend, async_trait};
use std::time::Duration;
use anyhow::Result;
struct MyDistributedCache;
#[async_trait]
impl CacheBackend for MyDistributedCache {
async fn get(&self, _key: &str) -> Option<serde_json::Value> { None }
async fn set_with_ttl(&self, _k: &str, _v: serde_json::Value, _t: Duration) -> Result<()> { Ok(()) }
async fn remove(&self, _k: &str) -> Result<()> { Ok(()) }
async fn health_check(&self) -> bool { true }
}
#[async_trait]
impl L2CacheBackend for MyDistributedCache {
async fn get_with_ttl(&self, key: &str) -> Option<(serde_json::Value, Option<Duration>)> {
// Retrieve value and calculate remaining TTL
None
}
}Required Methods§
Sourcefn get_with_ttl<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Option<(Value, Option<Duration>)>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get_with_ttl<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Option<(Value, Option<Duration>)>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Get value with its remaining TTL from L2 cache
This method retrieves both the value and its remaining time-to-live. This is used by the cache manager to promote entries from L2 to L1 with the correct TTL.
§Arguments
key- The cache key to retrieve
§Returns
Some((value, Some(ttl)))- Value found with remaining TTLSome((value, None))- Value found but no expiration set (never expires)None- Key not found or expired
§TTL Semantics
- TTL represents the remaining time until expiration
NoneTTL means the key has no expiration- Implementations should use backend-specific TTL commands (e.g., Redis TTL)
Implementors§
impl L2CacheBackend for RedisCache
Implement L2CacheBackend trait for RedisCache
This extends CacheBackend with TTL introspection capabilities needed for L2->L1 promotion.