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 { ... }
}
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 缓存(如果支持) 注意:此实现默认行为是 set_bytes,因为 CacheOps 没有区分 L1/L2。 如果需要真正的 L1-only,需要底层支持或使用 L1OnlyClient。 这里我们为了兼容示例代码,提供默认实现,但实际可能需要扩展 CacheOps。 然而,CacheOps 是基础 trait,L1/L2 控制应该由具体 Client 类型或配置决定。 如果用户想要手动控制,应该使用 set_bytes 并自己处理逻辑,或者我们在 CacheOps 中增加方法。 鉴于 PRD F3 “独立缓存层支持”,可以通过 Client 类型实现。 但示例代码中使用了 set_l1_only。 我们这里先添加 set_l1_only 到 CacheExt,但行为可能只是 set。 为了真正支持,我们需要在 CacheOps 中添加 set_l1_bytes 等,或者扩展 CacheExt 在特定实现中覆盖。 但 CacheExt 是 trait default impl,无法访问私有字段。

既然 F3 已经实现(通过 L1OnlyClient),我们可以让用户获取特定类型的 client。 但示例代码是在同一个 client 上调用 set_l1_only。这意味着 TwoLevelClient 应该支持这个。

让我们修改 CacheOps 增加 set_l1_bytes / set_l2_bytes 接口,默认实现可以是 no-op 或 fallback。 或者,我们只在 CacheExt 中提供,但需要 CacheOps 支持。

考虑到接口稳定性,我们在 CacheOps 中添加 set_l1_bytes 和 set_l2_bytes,并在 TwoLevelClient 中实现。

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,

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