Skip to main content

L2CacheBackend

Trait L2CacheBackend 

Source
pub trait L2CacheBackend: CacheBackend {
    // Required method
    fn get_with_ttl<'a>(
        &'a self,
        key: &'a str,
    ) -> BoxFuture<'a, Option<(Bytes, Option<Duration>)>>;
}
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

```rust,ignore
use multi_tier_cache::error::{CacheError, CacheResult};
use bytes::Bytes;
use std::time::Duration;
use futures_util::future::BoxFuture;
use multi_tier_cache::{CacheBackend, L2CacheBackend};

struct MyDistributedCache;

impl CacheBackend for MyDistributedCache {
    fn get<'a>(&'a self, _key: &'a str) -> BoxFuture<'a, Option<Bytes>> { Box::pin(async move { None }) }
    fn set_with_ttl<'a>(&'a self, _k: &'a str, _v: Bytes, _t: Duration) -> BoxFuture<'a, CacheResult<()>> { Box::pin(async move { Ok(()) }) }
    fn remove<'a>(&'a self, _k: &'a str) -> BoxFuture<'a, CacheResult<()>> { Box::pin(async move { Ok(()) }) }
    fn health_check(&self) -> BoxFuture<'_, bool> { Box::pin(async move { true }) }
    fn name(&self) -> &'static str { "MyDistCache" }
}

impl L2CacheBackend for MyDistributedCache {
    fn get_with_ttl<'a>(&'a self, _key: &'a str) -> BoxFuture<'a, Option<(Bytes, Option<Duration>)>> {
        Box::pin(async move { None })
    }
}

Required Methods§

Source

fn get_with_ttl<'a>( &'a self, key: &'a str, ) -> BoxFuture<'a, Option<(Bytes, Option<Duration>)>>

Get value with its remaining TTL from L2 cache

Implementors§