BatchCache

Trait BatchCache 

Source
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 + Eq
  • Value: 缓存值类型,需要实现 Send + Sync + Clone 和序列化(当使用 Redis 默认实现时)
  • RedisClient: (可选)Redis 客户端类型,用于提供基于 Redis 的默认实现

Required Associated Types§

Source

type Key: Send + Sync + Clone + Hash + Eq

缓存键类型

Source

type Value: Send + Sync + Clone

缓存值类型

Required Methods§

Source

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,

获取单个缓存

§参数
  • req: 缓存键
§返回
  • Ok(Some(value)): 找到缓存值
  • Ok(None): 缓存不存在
  • Err(e): 操作失败
Source

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,

批量获取缓存

§参数
  • req: 缓存键列表
§返回
  • Ok(map): 返回键值对映射,只包含存在的缓存项
  • Err(e): 操作失败
Source

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,

删除单个缓存

§参数
  • req: 缓存键
§返回
  • Ok(()): 删除成功(无论缓存是否存在)
  • Err(e): 操作失败
Source

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,

批量删除缓存

§参数
  • req: 缓存键列表
§返回
  • Ok(()): 删除成功(无论缓存是否存在)
  • Err(e): 操作失败

Provided Methods§

Source

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?;

Implementors§