pub struct TwoLevelStore<T> { /* private fields */ }Expand description
内存 LRU + Redis 双层缓存。 读操作优先命中内存 LRU,未命中再查 Redis 并回填内存层。
Implementations§
Source§impl<T: Clone + Serialize + DeserializeOwned> TwoLevelStore<T>
impl<T: Clone + Serialize + DeserializeOwned> TwoLevelStore<T>
Sourcepub fn new(redis: RedisCache, size: NonZeroUsize, ttl: Duration) -> Self
pub fn new(redis: RedisCache, size: NonZeroUsize, ttl: Duration) -> Self
创建新的 TwoLevelStore 实例。
size 为内存 LRU 的最大条目数,ttl 为缓存默认过期时长。
Sourcepub async fn set(&self, key: &str, value: T) -> Result<(), Error>
pub async fn set(&self, key: &str, value: T) -> Result<(), Error>
将值写入两层缓存。 TTL 与间隔边界对齐,先写 Redis 再更新内存 LRU。
Sourcepub async fn get(&self, key: &str) -> Result<Option<T>, Error>
pub async fn get(&self, key: &str) -> Result<Option<T>, Error>
从缓存读取值,优先查询内存 LRU,未命中则查 Redis 并回填 LRU。 若 Redis 中条目剩余 TTL 已超出预期范围则不回填内存层。
Sourcepub async fn purge_expired(&self)
pub async fn purge_expired(&self)
清除内存 LRU 中的过期条目,应定期调用以释放内存。 Redis 侧的 TTL 由 Redis 自身管理,无需手动清理。
Auto Trait Implementations§
impl<T> !Freeze for TwoLevelStore<T>
impl<T> !RefUnwindSafe for TwoLevelStore<T>
impl<T> Send for TwoLevelStore<T>where
T: Send,
impl<T> Sync for TwoLevelStore<T>
impl<T> Unpin for TwoLevelStore<T>
impl<T> UnsafeUnpin for TwoLevelStore<T>
impl<T> !UnwindSafe for TwoLevelStore<T>
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