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) -> L2Cache
pub fn with_default_ttl(self, ttl: Duration) -> L2Cache
设置默认 TTL
Sourcepub fn with_max_size(self, max_size: usize) -> L2Cache
pub fn with_max_size(self, max_size: usize) -> L2Cache
设置最大容量
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian().Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.