pub trait CacheBackend:
Send
+ Sync
+ Clone {
// Required methods
async fn get(&self, key: &str) -> Result<Option<Vec<u8>>>;
async fn set(
&self,
key: &str,
value: Vec<u8>,
ttl: Option<Duration>,
) -> Result<()>;
async fn delete(&self, key: &str) -> Result<()>;
// Provided methods
async fn exists(&self, key: &str) -> Result<bool> { ... }
async fn mget(&self, keys: &[&str]) -> Result<Vec<Option<Vec<u8>>>> { ... }
async fn mdelete(&self, keys: &[&str]) -> Result<()> { ... }
async fn health_check(&self) -> Result<bool> { ... }
async fn clear_all(&self) -> Result<()> { ... }
}Expand description
Trait for cache backend implementations.
Abstracts storage operations, allowing swappable backends. Implementations: InMemory (default), Redis, Memcached, RocksDB, Database, S3, etc.
IMPORTANT: All methods use &self instead of &mut self to allow concurrent access.
Backend implementations should use interior mutability (RwLock, Mutex, or external storage).
ASYNC: All methods are async and must be awaited.
Required Methods§
Provided Methods§
Sourceasync fn mget(&self, keys: &[&str]) -> Result<Vec<Option<Vec<u8>>>>
async fn mget(&self, keys: &[&str]) -> Result<Vec<Option<Vec<u8>>>>
Bulk get operation (optional optimization).
Default implementation calls get() for each key.
Override for batch efficiency (e.g., Redis MGET).
§Errors
Returns Err if backend error occurs
Sourceasync fn mdelete(&self, keys: &[&str]) -> Result<()>
async fn mdelete(&self, keys: &[&str]) -> Result<()>
Bulk delete operation (optional optimization).
Default implementation calls delete() for each key.
Override for batch efficiency (e.g., Redis DEL).
§Errors
Returns Err if backend error occurs
Sourceasync fn health_check(&self) -> Result<bool>
async fn health_check(&self) -> Result<bool>
Health check - verify backend is accessible.
Used for readiness probes, circuit breakers, etc.
§Errors
Returns Err if backend is not accessible
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.