pub trait BatchCache {
type Key: Send + Sync + Clone + Hash + Eq;
type Value: Send + Sync + Clone;
// Required methods
fn get<'life0, 'async_trait>(
&'life0 self,
req: Self::Key,
) -> Pin<Box<dyn Future<Output = AppResult<Option<Self::Value>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn get_batch<'life0, 'life1, 'async_trait>(
&'life0 self,
req: &'life1 [Self::Key],
) -> Pin<Box<dyn Future<Output = AppResult<HashMap<Self::Key, Self::Value>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn delete<'life0, 'async_trait>(
&'life0 self,
req: Self::Key,
) -> Pin<Box<dyn Future<Output = AppResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn delete_batch<'life0, 'life1, 'async_trait>(
&'life0 self,
req: &'life1 [Self::Key],
) -> Pin<Box<dyn Future<Output = AppResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
// Provided method
fn refresh<'life0, 'life1, 'async_trait>(
&'life0 self,
req: &'life1 HashMap<Self::Key, Self::Value>,
) -> Pin<Box<dyn Future<Output = AppResult<()>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
}Expand description
批量缓存操作 Trait
提供单个和批量缓存操作的统一接口
§关联类型
Key: 缓存键类型,需要实现Send + Sync + Clone + Hash + EqValue: 缓存值类型,需要实现Send + Sync + Clone和序列化(当使用 Redis 默认实现时)RedisClient: (可选)Redis 客户端类型,用于提供基于 Redis 的默认实现
Required Associated Types§
Required Methods§
Sourcefn get<'life0, 'async_trait>(
&'life0 self,
req: Self::Key,
) -> Pin<Box<dyn Future<Output = AppResult<Option<Self::Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get<'life0, 'async_trait>(
&'life0 self,
req: Self::Key,
) -> Pin<Box<dyn Future<Output = AppResult<Option<Self::Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Sourcefn get_batch<'life0, 'life1, 'async_trait>(
&'life0 self,
req: &'life1 [Self::Key],
) -> Pin<Box<dyn Future<Output = AppResult<HashMap<Self::Key, Self::Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get_batch<'life0, 'life1, 'async_trait>(
&'life0 self,
req: &'life1 [Self::Key],
) -> Pin<Box<dyn Future<Output = AppResult<HashMap<Self::Key, Self::Value>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Provided Methods§
Sourcefn refresh<'life0, 'life1, 'async_trait>(
&'life0 self,
req: &'life1 HashMap<Self::Key, Self::Value>,
) -> Pin<Box<dyn Future<Output = AppResult<()>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn refresh<'life0, 'life1, 'async_trait>(
&'life0 self,
req: &'life1 HashMap<Self::Key, Self::Value>,
) -> Pin<Box<dyn Future<Output = AppResult<()>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
批量刷新缓存(清除指定 map 中所有键对应的缓存)
§参数
req: 包含要清除的缓存键的 HashMap(值会被忽略,只使用键)
§返回
Ok(()): 清除成功(无论缓存是否存在)Err(e): 操作失败
§示例
use fbc_starter::cache::BatchCache;
use fbc_starter::AppResult;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert(1, "value1".to_string());
map.insert(2, "value2".to_string());
map.insert(3, "value3".to_string());
// 清除 map 中所有键对应的缓存
cache.refresh(&map).await?;