CacheBackend

Trait CacheBackend 

Source
pub trait CacheBackend: Send + Sync {
    // Required methods
    fn get<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Option<Value>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn set_with_ttl<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
        value: Value,
        ttl: Duration,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn remove<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn health_check<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided method
    fn name(&self) -> &'static str { ... }
}
Expand description

Core cache backend trait for both L1 and L2 caches

This trait defines the essential operations that any cache backend must support. Implement this trait to create custom L1 (in-memory) or L2 (distributed) cache backends.

§Required Operations

  • get: Retrieve a value by key
  • set_with_ttl: Store a value with a time-to-live
  • remove: Delete a value by key
  • health_check: Verify cache backend is operational

§Thread Safety

Implementations must be Send + Sync to support concurrent access across async tasks.

§Performance Considerations

  • get operations should be optimized for low latency (target: <1ms for L1, <5ms for L2)
  • set_with_ttl operations can be slightly slower but should still be fast
  • Consider connection pooling for distributed backends

§Example

See module-level documentation for a complete example.

Required Methods§

Source

fn get<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Option<Value>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get value from cache by key

§Arguments
  • key - The cache key to retrieve
§Returns
  • Some(value) - Value found in cache
  • None - Key not found or expired
Source

fn set_with_ttl<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, value: Value, ttl: Duration, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Set value in cache with time-to-live

§Arguments
  • key - The cache key
  • value - The value to store (must be JSON-serializable)
  • ttl - Time-to-live duration
§Returns
  • Ok(()) - Value successfully cached
  • Err(e) - Cache operation failed
Source

fn remove<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Remove value from cache

§Arguments
  • key - The cache key to remove
§Returns
  • Ok(()) - Value removed (or didn’t exist)
  • Err(e) - Cache operation failed
Source

fn health_check<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Check if cache backend is healthy

This method should verify that the cache backend is operational. For distributed caches, this typically involves a ping or connectivity check.

§Returns
  • true - Cache is healthy and operational
  • false - Cache is unhealthy or unreachable

Provided Methods§

Source

fn name(&self) -> &'static str

Get the name of this cache backend

This is used for logging and debugging purposes.

§Returns

A string identifying this cache backend (e.g., “Moka”, “Redis”, “Memcached”)

Implementors§

Source§

impl CacheBackend for DashMapCache

Implement CacheBackend trait for DashMapCache

Source§

impl CacheBackend for MokaCache

Implement CacheBackend trait for MokaCache

This allows MokaCache to be used as a pluggable backend in the multi-tier cache system.

Source§

impl CacheBackend for RedisCache

Implement CacheBackend trait for RedisCache

This allows RedisCache to be used as a pluggable backend in the multi-tier cache system.