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 keyset_with_ttl: Store a value with a time-to-liveremove: Delete a value by keyhealth_check: Verify cache backend is operational
§Thread Safety
Implementations must be Send + Sync to support concurrent access across async tasks.
§Performance Considerations
getoperations should be optimized for low latency (target: <1ms for L1, <5ms for L2)set_with_ttloperations 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§
Sourcefn 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 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,
Sourcefn 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 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,
Sourcefn 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 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,
Sourcefn health_check<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: '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,
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 operationalfalse- Cache is unhealthy or unreachable
Provided Methods§
Implementors§
impl CacheBackend for DashMapCache
Implement CacheBackend trait for DashMapCache
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.
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.