pub struct LocalCache { /* private fields */ }Expand description
本地内存缓存(DashMap 分片 + ahash + TTL + 统计 + 后台清理)
存储使用序列化后的字节(Arc<[u8]> 共享,命中零拷贝);容量满时按
有界采样淘汰最早过期的条目(近似 TTL 淘汰)。
Implementations§
Source§impl LocalCache
impl LocalCache
Sourcepub fn new(max_capacity: u64, default_ttl_secs: u64) -> Self
pub fn new(max_capacity: u64, default_ttl_secs: u64) -> Self
创建本地内存缓存
max_capacity: 超过此容量后按最早过期策略淘汰;0表示不限容量default_ttl_secs: 已废弃(兼容保留,不再生效):set已对齐 Redis 契约为永不过期,default TTL 不再自动应用;需要过期请显式Cache::set_ex。传 0 即可。
pub fn with_cleanup_interval(self, interval_secs: u64) -> Self
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
获取缓存统计快照
Sourcepub fn cleanup_expired(&self) -> u64
pub fn cleanup_expired(&self) -> u64
手动清理所有过期条目,返回清理数
Sourcepub fn start_background_cleanup(&self)
pub fn start_background_cleanup(&self)
启动后台过期清理任务(每 interval_secs 秒执行一次,幂等:多次调用只启动一次)。
任务持有 Weak<Inner>:当所有 LocalCache 句柄被释放后,任务在下一轮
唤醒时自动退出,不会泄漏。
Sourcepub async fn get_or_insert_with<T, F, Fut>(
&self,
key: &str,
ttl_secs: u64,
load: F,
) -> Result<T>
pub async fn get_or_insert_with<T, F, Fut>( &self, key: &str, ttl_secs: u64, load: F, ) -> Result<T>
单飞加载(缓存击穿防护):命中直接返回;未命中则按 key 去重——
同一 key 的并发调用只有一个执行 load 回源,其余等待其完成后读缓存。
ttl_secs > 0:回源结果以Cache::set_ex写入;ttl_secs == 0表示永不过期(以Cache::set写入,不走 invalid-expire 校验)load返回Err时不写入缓存,等待中的调用将依次重试回源- 去重范围为单个进程内的缓存实例(克隆共享同一张去重表)
Trait Implementations§
Source§impl Cache for LocalCache
impl Cache for LocalCache
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,
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 剩余过期秒数(向上取整,对齐 Redis TTL 的舍入方式)
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,
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>>
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>>
设置缓存(永不过期,与 Redis
SET 语义一致),值通过 serde_json 序列化 Read moreSource§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>>
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>>
设置缓存(指定过期秒数),到期后自动不可见 Read more
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>>
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>>
原子设置:仅当 key 不存在时设置,返回 true 表示设置成功,false 表示 key 已存在 Read more
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,
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,
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,
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 开始),返回递增后的值。 Read more
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,
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,
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,
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,返回删除数 Read more
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,
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,
向集合添加一个或多个成员,返回实际新增的数量。 Read more
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,
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,
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,
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) Read more
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,
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,
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 中转) Read moreSource§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 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 中转) Read moreSource§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 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 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 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。 Read more
Source§fn stats(&self) -> CacheStats
fn stats(&self) -> CacheStats
缓存统计信息(内存缓存支持,Redis 返回全零)
Source§impl Clone for LocalCache
impl Clone for LocalCache
Auto Trait Implementations§
impl !RefUnwindSafe for LocalCache
impl !UnwindSafe for LocalCache
impl Forget for LocalCache
impl Freeze for LocalCache
impl Move for LocalCache
impl Send for LocalCache
impl Sync for LocalCache
impl Unpin for LocalCache
impl UnsafeUnpin for LocalCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more