pub struct RedisCache { /* private fields */ }Expand description
Redis distributed cache with ConnectionManager for automatic reconnection
This is the default L2 (warm tier) cache backend, providing:
- Distributed caching across multiple instances
- Persistence to disk
- Automatic reconnection via ConnectionManager
- TTL introspection for cache promotion
- Pattern-based key scanning
Implementations§
Source§impl RedisCache
impl RedisCache
Sourcepub async fn new() -> Result<Self>
pub async fn new() -> Result<Self>
Create new Redis cache with ConnectionManager for automatic reconnection
Sourcepub async fn get(&self, key: &str) -> Option<Value>
pub async fn get(&self, key: &str) -> Option<Value>
Get value from Redis cache using persistent ConnectionManager
Sourcepub async fn get_with_ttl(&self, key: &str) -> Option<(Value, Option<Duration>)>
pub async fn get_with_ttl(&self, key: &str) -> Option<(Value, Option<Duration>)>
Get value with its remaining TTL from Redis cache
Returns tuple of (value, ttl) if key exists TTL is in seconds, None if key doesn’t exist or has no expiration
Sourcepub async fn set_with_ttl(
&self,
key: &str,
value: Value,
ttl: Duration,
) -> Result<()>
pub async fn set_with_ttl( &self, key: &str, value: Value, ttl: Duration, ) -> Result<()>
Set value with custom TTL using persistent ConnectionManager
Sourcepub async fn remove(&self, key: &str) -> Result<()>
pub async fn remove(&self, key: &str) -> Result<()>
Remove value from cache using persistent ConnectionManager
Sourcepub async fn scan_keys(&self, pattern: &str) -> Result<Vec<String>>
pub async fn scan_keys(&self, pattern: &str) -> Result<Vec<String>>
Scan keys matching a pattern (glob-style: *, ?, [])
Uses Redis SCAN command (non-blocking, cursor-based iteration) This is safe for production use, unlike KEYS command.
§Arguments
pattern- Glob-style pattern (e.g., “user:”, “product:123:”)
§Returns
Vector of matching key names
§Examples
// Find all user cache keys
let keys = cache.scan_keys("user:*").await?;
// Find specific user's cache keys
let keys = cache.scan_keys("user:123:*").await?;Sourcepub async fn remove_bulk(&self, keys: &[String]) -> Result<usize>
pub async fn remove_bulk(&self, keys: &[String]) -> Result<usize>
Remove multiple keys at once (bulk delete)
More efficient than calling remove() multiple times
Sourcepub async fn health_check(&self) -> bool
pub async fn health_check(&self) -> bool
Health check
Trait Implementations§
Source§impl CacheBackend for RedisCache
Implement CacheBackend trait for RedisCache
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.
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,
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,
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,
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,
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,
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,
Source§impl L2CacheBackend for RedisCache
Implement L2CacheBackend trait for RedisCache
impl L2CacheBackend for RedisCache
Implement L2CacheBackend trait for RedisCache
This extends CacheBackend with TTL introspection capabilities needed for L2->L1 promotion.