pub struct L2Cache { /* private fields */ }Expand description
L2 二级缓存 — 跨 Session 共享
线程安全:内部使用 RwLock,可在多线程环境下共享。
§示例
use sz_orm_core::l2_cache::{L2Cache, CacheKey};
use sz_orm_core::Value;
use std::time::Duration;
let cache = L2Cache::new();
// 缓存单行
let key = CacheKey::by_pk("users", 1);
cache.put(&key, Value::String("Alice".to_string()), None);
// 读取
assert!(cache.get(&key).is_some());
// 表级失效
cache.invalidate_table("users");
assert!(cache.get(&key).is_none());Implementations§
Source§impl L2Cache
impl L2Cache
Sourcepub fn with_default_ttl(self, ttl: Duration) -> Self
pub fn with_default_ttl(self, ttl: Duration) -> Self
设置默认 TTL
Sourcepub fn with_max_size(self, max_size: usize) -> Self
pub fn with_max_size(self, max_size: usize) -> Self
设置最大容量
Sourcepub fn put(&self, key: &CacheKey, value: Value, ttl: Option<Duration>)
pub fn put(&self, key: &CacheKey, value: Value, ttl: Option<Duration>)
存入缓存项
§TTL 语义
ttl = Some(d):使用d作为过期时间ttl = None:使用default_ttl(若未设置则永不过期)- 要显式表示“永不失效“,请传
Some(Duration::MAX)
Sourcepub fn get(&self, key: &CacheKey) -> Option<Value>
pub fn get(&self, key: &CacheKey) -> Option<Value>
读取缓存项(不存在或已过期返回 None)
命中时会更新 LRU 访问顺序(移到尾部)。
Sourcepub fn invalidate(&self, key: &CacheKey)
pub fn invalidate(&self, key: &CacheKey)
失效单个缓存项
Sourcepub fn invalidate_table(&self, table: &str)
pub fn invalidate_table(&self, table: &str)
失效整张表的所有缓存项
仅统计实际从缓存中删除的 key 数量,避免 evictions 偏大。
Sourcepub fn stats(&self) -> L2CacheStats
pub fn stats(&self) -> L2CacheStats
获取统计信息
Sourcepub fn reset_stats(&self)
pub fn reset_stats(&self)
重置统计信息
Sourcepub fn evict_expired(&self) -> usize
pub fn evict_expired(&self) -> usize
手动清理所有过期项
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for L2Cache
impl RefUnwindSafe for L2Cache
impl Send for L2Cache
impl Sync for L2Cache
impl Unpin for L2Cache
impl UnsafeUnpin for L2Cache
impl UnwindSafe for L2Cache
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