pub struct NegativeCache<C: Cache> { /* private fields */ }Expand description
带负缓存的缓存包装器
在正缓存(inner)之上叠加负缓存层:当 get 返回 None 时,将 key 记录到
负缓存,在 negative_ttl 期内再次查询直接返回 NotFound,避免缓存穿透。
写入(set)或删除(delete)key 时自动清除对应的负缓存条目。
Implementations§
Source§impl<C: Cache> NegativeCache<C>
impl<C: Cache> NegativeCache<C>
Sourcepub fn with_negative_ttl(self, ttl: Duration) -> Self
pub fn with_negative_ttl(self, ttl: Duration) -> Self
设置负缓存 TTL
Sourcepub fn get_or_negative(&self, key: &str) -> Result<CacheLookup, CacheError>
pub fn get_or_negative(&self, key: &str) -> Result<CacheLookup, CacheError>
获取值,区分“未命中“和“明确不存在“
查找顺序:负缓存 → 正缓存。
- 负缓存命中且未过期 →
NotFound(不回源) - 正缓存命中 →
Found - 正缓存未命中 → 记录到负缓存,返回
Miss
Sourcepub fn purge_expired(&self) -> Result<usize, CacheError>
pub fn purge_expired(&self) -> Result<usize, CacheError>
清理所有已过期的负缓存条目
Trait Implementations§
Source§impl<C: Cache> Cache for NegativeCache<C>
impl<C: Cache> Cache for NegativeCache<C>
fn get(&self, key: &str) -> Result<Option<Vec<u8>>, CacheError>
fn set( &self, key: &str, value: Vec<u8>, ttl: Option<Duration>, ) -> Result<(), CacheError>
fn delete(&self, key: &str) -> Result<(), CacheError>
fn clear(&self) -> Result<(), CacheError>
fn exists(&self, key: &str) -> Result<bool, CacheError>
fn expire(&self, key: &str, ttl: Duration) -> Result<(), CacheError>
fn ttl(&self, key: &str) -> Result<Option<Duration>, CacheError>
Auto Trait Implementations§
impl<C> !Freeze for NegativeCache<C>
impl<C> RefUnwindSafe for NegativeCache<C>where
C: RefUnwindSafe,
impl<C> Send for NegativeCache<C>
impl<C> Sync for NegativeCache<C>
impl<C> Unpin for NegativeCache<C>where
C: Unpin,
impl<C> UnsafeUnpin for NegativeCache<C>where
C: UnsafeUnpin,
impl<C> UnwindSafe for NegativeCache<C>where
C: UnwindSafe,
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