Skip to main content

NegativeCache

Struct NegativeCache 

Source
pub struct NegativeCache<C>
where C: Cache,
{ /* private fields */ }
Expand description

带负缓存的缓存包装器

在正缓存(inner)之上叠加负缓存层:当 get 返回 None 时,将 key 记录到 负缓存,在 negative_ttl 期内再次查询直接返回 NotFound,避免缓存穿透。

写入(set)或删除(delete)key 时自动清除对应的负缓存条目。

Implementations§

Source§

impl<C> NegativeCache<C>
where C: Cache,

Source

pub fn new(inner: C) -> NegativeCache<C>

创建负缓存包装器,默认负缓存 TTL 为 60 秒

Source

pub fn with_negative_ttl(self, ttl: Duration) -> NegativeCache<C>

设置负缓存 TTL

Source

pub fn get_or_negative(&self, key: &str) -> Result<CacheLookup, CacheError>

获取值,区分“未命中“和“明确不存在“

查找顺序:负缓存 → 正缓存。

  • 负缓存命中且未过期 → NotFound(不回源)
  • 正缓存命中 → Found
  • 正缓存未命中 → 记录到负缓存,返回 Miss
Source

pub fn purge_expired(&self) -> Result<usize, CacheError>

清理所有已过期的负缓存条目

Trait Implementations§

Source§

impl<C> Cache for NegativeCache<C>
where C: Cache,

Source§

fn get(&self, key: &str) -> Result<Option<Vec<u8>>, CacheError>

Source§

fn set( &self, key: &str, value: Vec<u8>, ttl: Option<Duration>, ) -> Result<(), CacheError>

Source§

fn delete(&self, key: &str) -> Result<(), CacheError>

Source§

fn clear(&self) -> Result<(), CacheError>

Source§

fn exists(&self, key: &str) -> Result<bool, CacheError>

Source§

fn expire(&self, key: &str, ttl: Duration) -> Result<(), CacheError>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more