Cache

Trait Cache 

Source
pub trait Cache: Send + Sync {
    // Required methods
    fn get<'life0, 'life1, 'async_trait, T>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Option<T>, CacheError>> + Send + 'async_trait>>
       where T: DeserializeOwned + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn set<'life0, 'life1, 'life2, 'async_trait, T>(
        &'life0 self,
        key: &'life1 str,
        value: &'life2 T,
        ttl: Option<Duration>,
    ) -> Pin<Box<dyn Future<Output = Result<(), CacheError>> + Send + 'async_trait>>
       where T: Serialize + Sync + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn delete<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<bool, CacheError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn exists<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<bool, CacheError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn expire<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
        ttl: Duration,
    ) -> Pin<Box<dyn Future<Output = Result<bool, CacheError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn ttl<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Duration>, CacheError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn clear<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<u64, CacheError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn clear_module<'life0, 'life1, 'async_trait>(
        &'life0 self,
        module: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<u64, CacheError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

缓存接口 trait

所有缓存实现(内存、Redis 等)都需要实现此 trait

Required Methods§

Source

fn get<'life0, 'life1, 'async_trait, T>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<T>, CacheError>> + Send + 'async_trait>>
where T: DeserializeOwned + 'async_trait, Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

获取缓存值

§Arguments
  • key - 缓存键
§Returns
  • Result<Option<T>, CacheError> - 缓存值,None 表示不存在
Source

fn set<'life0, 'life1, 'life2, 'async_trait, T>( &'life0 self, key: &'life1 str, value: &'life2 T, ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = Result<(), CacheError>> + Send + 'async_trait>>
where T: Serialize + Sync + 'async_trait, Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

设置缓存值

§Arguments
  • key - 缓存键
  • value - 缓存值
  • ttl - 过期时间(可选)
§Returns
  • Result<(), CacheError> - 操作结果
Source

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<bool, CacheError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

删除缓存值

§Arguments
  • key - 缓存键
§Returns
  • Result<bool, CacheError> - 是否成功删除(true 表示存在并删除,false 表示不存在)
Source

fn exists<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<bool, CacheError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

检查缓存键是否存在

§Arguments
  • key - 缓存键
§Returns
  • Result<bool, CacheError> - 是否存在
Source

fn expire<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ttl: Duration, ) -> Pin<Box<dyn Future<Output = Result<bool, CacheError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

设置过期时间

§Arguments
  • key - 缓存键
  • ttl - 过期时间
§Returns
  • Result<bool, CacheError> - 是否成功设置(true 表示存在并设置,false 表示不存在)
Source

fn ttl<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<Duration>, CacheError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

获取剩余过期时间

§Arguments
  • key - 缓存键
§Returns
  • Result<Option<Duration>, CacheError> - 剩余过期时间,None 表示不存在或永不过期
Source

fn clear<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<u64, CacheError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

清空所有缓存

§Returns
  • Result<u64, CacheError> - 删除的键数量
Source

fn clear_module<'life0, 'life1, 'async_trait>( &'life0 self, module: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<u64, CacheError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

清空指定模块的缓存

§Arguments
  • module - 模块标识
§Returns
  • Result<u64, CacheError> - 删除的键数量

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.

Implementors§