Skip to main content

Cache

Trait Cache 

Source
pub trait Cache: Send + Sync {
Show 20 methods // Required 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: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn set<'life0, 'life1, 'life2, 'async_trait, T>( &'life0 self, key: &'life1 str, value: &'life2 T, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where T: 'async_trait + Serialize + Send + Sync, Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait; fn set_ex<'life0, 'life1, 'life2, 'async_trait, T>( &'life0 self, key: &'life1 str, value: &'life2 T, ttl_secs: u64, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where T: 'async_trait + Serialize + Send + Sync, Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait; fn del<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + 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>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn incr<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, delta: i64, ) -> Pin<Box<dyn Future<Output = Result<i64>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn keys<'life0, 'life1, 'async_trait>( &'life0 self, pattern: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<String>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn set_nx<'life0, 'life1, 'life2, 'async_trait, T>( &'life0 self, key: &'life1 str, value: &'life2 T, ttl_secs: u64, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>> where T: 'async_trait + Serialize + Send + Sync, Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait; fn delete_pattern<'life0, 'life1, 'async_trait>( &'life0 self, pattern: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<u64>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn sadd<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, members: &'life2 [String], ) -> Pin<Box<dyn Future<Output = Result<u64>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait; fn srem<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, members: &'life2 [String], ) -> Pin<Box<dyn Future<Output = Result<u64>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait; fn smembers<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<String>>> + 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_secs: u64, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; // Provided methods fn sismember<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, member: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn ttl<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<u64>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn sadd_with_ttl<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, members: &'life2 [String], ttl_secs: u64, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn get_raw<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<u8>>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn set_raw<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, value: &'life2 [u8], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn set_raw_ex<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, value: &'life2 [u8], ttl_secs: u64, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn stats(&self) -> CacheStats { ... }
}
Expand description

统一缓存接口(本地/Redis 实现同一 trait)

注意:方法带泛型参数,本 trait 不满足 object safety,无法以 dyn Cache 形式分发;请使用具体实现(LocalCache/RedisCache) 或 SharedCache 枚举。

§示例

ⓘ
let cache = SharedCache::Local(LocalCache::new(1024, 0));
cache.set("key", &"value".to_string()).await?;
let val: Option<String> = cache.get("key").await?;

Required 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: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

读取缓存值,返回 Ok(None) 表示 key 不存在或已过期

Source

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

设置缓存(永不过期,与 Redis SET 语义一致),值通过 serde_json 序列化

本地与 Redis 两个后端行为一致:写入的 key 均不带 TTL。需要过期时间 请使用 Cache::set_ex。

Source

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

设置缓存(指定过期秒数),到期后自动不可见

ttl_secs == 0 返回错误(对齐 Redis SETEX 的 invalid expire time)

Source

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

删除单个 key(不存在不报错)

Source

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

检查 key 是否存在且未过期

Source

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

计数器递增(key 不存在则从 0 开始),返回递增后的值。

对齐 Redis INCR 语义:存量值不是整数时返回错误,i64 溢出返回错误。

Source

fn keys<'life0, 'life1, 'async_trait>( &'life0 self, pattern: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<String>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

获取匹配模式(glob:*/?)的所有 key

注意:本地后端只支持 */? 通配符;Redis 后端走 SCAN MATCH,额外 支持 [...] 字符类。跨后端可移植的公共子集是 */?。

⚠️ 全键扫描:本地后端 O(全部 key) 线性遍历(与 Redis KEYS 同病), 大缓存上可能造成毫秒级停顿;热路径请按精确 key 读写, 本方法仅限运维/管理类低频场景。

Source

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

原子设置:仅当 key 不存在时设置,返回 true 表示设置成功,false 表示 key 已存在

ttl_secs == 0 返回错误(对齐 Redis SET ... EX 的 invalid expire time)

Source

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

删除匹配模式的所有 key,返回删除数

⚠️ 全键扫描:本地后端 O(全部 key) 线性遍历,大缓存上可能造成 毫秒级停顿;仅限运维/管理类低频场景,热路径请按精确 key 删除。

Source

fn sadd<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, members: &'life2 [String], ) -> Pin<Box<dyn Future<Output = Result<u64>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

向集合添加一个或多个成员,返回实际新增的数量。

对齐 Redis SADD 语义:key 持有非集合值时返回错误(不静默清数据)。

Source

fn srem<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, members: &'life2 [String], ) -> Pin<Box<dyn Future<Output = Result<u64>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

从集合移除一个或多个成员,返回实际移除的数量;集合变空时删除 key(对齐 Redis)。

Source

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

获取集合的所有成员;key 持有非集合值时返回错误(对齐 Redis WRONGTYPE)。

Source

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

刷新 key 的过期时间(秒);key 不存在或已过期返回 false(对齐 Redis)

ttl_secs == 0 返回错误(对齐 Redis EXPIRE 的 invalid expire time)

Provided Methods§

Source

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

检查成员是否在集合中

Source

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

查询 key 剩余过期秒数(向上取整)。

返回 None 表示 key 不存在、已过期或未设置过期时间(三者不做区分, 可先用 Cache::exists 区分是否存在)。默认实现恒返回 None(未覆盖 的第三方实现不支持 TTL 查询);内置两个后端均已覆盖。

Source

fn sadd_with_ttl<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, members: &'life2 [String], ttl_secs: u64, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

组合操作:添加集合成员并为 key 设置 TTL。

默认实现按顺序执行 SADD + EXPIRE(两步间进程崩溃会留下无 TTL 的 key);Redis 实现覆盖为 pipeline 原子发送,消除该窗口。需要 “集合成员随 TTL 自动自愈“语义(如在线状态登记)时应使用本方法。 members 为空时退化为仅刷新 TTL(与默认实现的两步行为一致)。

Source

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

获取原始字节数据(默认经 serde_json::Value 中转;实现可覆盖为字节直通)

Source

fn set_raw<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, value: &'life2 [u8], ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

设置原始字节数据(必须是合法 JSON;默认经 serde_json::Value 中转)

与 Cache::set 语义一致:写入的 key 永不过期

Source

fn set_raw_ex<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, key: &'life1 str, value: &'life2 [u8], ttl_secs: u64, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

设置原始字节数据(带 TTL;默认经 serde_json::Value 中转)

ttl_secs == 0 返回错误(对齐 Redis SETEX 的 invalid expire time)

Source

fn stats(&self) -> CacheStats

缓存统计信息(内存缓存支持,Redis 返回全零)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§