CacheExt

Trait CacheExt 

Source
pub trait CacheExt: CacheOps {
    // Provided methods
    fn get<'life0, 'life1, 'async_trait, T>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Option<T>>> + Send + 'async_trait>>
       where T: 'async_trait + DeserializeOwned + Send,
             Self: Sync + '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<u64>,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where T: 'async_trait + Serialize + Send + Sync,
             Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn set_l1_only<'life0, 'life1, 'life2, 'async_trait, T>(
        &'life0 self,
        key: &'life1 str,
        value: &'life2 T,
        ttl: Option<u64>,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where T: 'async_trait + Serialize + Send + Sync,
             Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn set_l2_only<'life0, 'life1, 'life2, 'async_trait, T>(
        &'life0 self,
        key: &'life1 str,
        value: &'life2 T,
        ttl: Option<u64>,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where T: 'async_trait + Serialize + Send + Sync,
             Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn get_or_fetch<'life0, 'life1, 'async_trait, T, F, Fut>(
        &'life0 self,
        key: &'life1 str,
        ttl: Option<u64>,
        fetch: F,
    ) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>
       where T: DeserializeOwned + Serialize + Send + Sync + Clone + 'async_trait,
             F: FnOnce() -> Fut + Send + 'async_trait,
             Fut: Future<Output = Result<T>> + Send + 'async_trait,
             Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn try_get<'life0, 'life1, 'async_trait, T>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Option<T>>> + Send + 'async_trait>>
       where T: 'async_trait + DeserializeOwned + Send,
             Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn remove<'life0, 'life1, 'async_trait, T>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Option<T>>> + Send + 'async_trait>>
       where T: 'async_trait + DeserializeOwned + Send,
             Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn contains<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

缓存扩展特征

提供类型安全的缓存操作接口

Provided Methods§

Source

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

获取缓存值(反序列化)

Source

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

设置缓存值(序列化)

Source

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

仅设置 L1 缓存(如果支持)

Source

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

Source

fn get_or_fetch<'life0, 'life1, 'async_trait, T, F, Fut>( &'life0 self, key: &'life1 str, ttl: Option<u64>, fetch: F, ) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>
where T: DeserializeOwned + Serialize + Send + Sync + Clone + 'async_trait, F: FnOnce() -> Fut + Send + 'async_trait, Fut: Future<Output = Result<T>> + Send + 'async_trait, Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

获取缓存值,如果不存在则调用回调函数获取并缓存

§参数
  • key - 缓存键
  • ttl - 缓存时间(秒)
  • fetch - 当缓存未命中时调用的回调函数
§返回值

返回缓存的值或从回调函数获取的值

§示例
let user = cache
    .get_or_fetch("user:123", Some(3600), || async {
        database::get_user("123").await
    })
    .await?;
Source

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

尝试获取,如果不存在返回 None(不触发 fetch)

这是一个便捷方法,避免使用 Option 处理

Source

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

删除并返回旧值

§参数
  • key - 缓存键
§返回值

返回被删除的值(如果存在)

Source

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

检查键是否存在

§参数
  • key - 缓存键
§返回值

如果键存在返回 true,否则返回 false

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§

Source§

impl<T: CacheOps + ?Sized> CacheExt for T