Skip to main content

CacheBackend

Trait CacheBackend 

Source
pub trait CacheBackend: Send + Sync {
    // Required methods
    fn get<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Option<Bytes>>;
    fn set_with_ttl<'a>(
        &'a self,
        key: &'a str,
        value: Bytes,
        ttl: Duration,
    ) -> BoxFuture<'a, Result<()>>;
    fn remove<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<()>>;
    fn health_check(&self) -> BoxFuture<'_, bool>;
    fn name(&self) -> &'static str;

    // Provided method
    fn remove_pattern<'a>(
        &'a self,
        _pattern: &'a str,
    ) -> BoxFuture<'a, Result<()>> { ... }
}
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<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Option<Bytes>>

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<'a>( &'a self, key: &'a str, value: Bytes, ttl: Duration, ) -> BoxFuture<'a, Result<()>>

Set value in cache with time-to-live

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

fn remove<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<()>>

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(&self) -> BoxFuture<'_, bool>

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
Source

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

Get the name of this cache backend

Provided Methods§

Source

fn remove_pattern<'a>(&'a self, _pattern: &'a str) -> BoxFuture<'a, Result<()>>

Remove keys matching a pattern

§Arguments
  • pattern - Glob-style pattern (e.g. “user:*”)
§Returns
  • Ok(()) - Pattern processed
  • Err(e) - Operation failed

Implementors§

Source§

impl CacheBackend for DashMapCache

Implement CacheBackend trait for DashMapCache

Source§

impl CacheBackend for MokaCache

Implement CacheBackend trait for MokaCache

Source§

impl CacheBackend for RedisCache

Implement CacheBackend trait for RedisCache