pub struct ExpiringCache<T, E> { /* private fields */ }Expand description
Expiry-aware cache
ExpiringCache implements two important features:
- Respect expiry of contents
- Deduplicate load requests to prevent thundering herds when no value is present.
Implementations§
Source§impl<T, E> ExpiringCache<T, E>where
T: Clone,
impl<T, E> ExpiringCache<T, E>where
T: Clone,
Sourcepub async fn get_or_load<F, Fut>(&self, f: F) -> Result<T, E>
pub async fn get_or_load<F, Fut>(&self, f: F) -> Result<T, E>
Attempts to refresh the cached value with the given future.
If multiple threads attempt to refresh at the same time, one of them will win,
and the others will await that thread’s result rather than multiple refreshes occurring.
The function given to acquire a value future, f, will not be called
if another thread is chosen to load the value.
Sourcepub async fn yield_or_clear_if_expired(&self, now: SystemTime) -> Option<T>
pub async fn yield_or_clear_if_expired(&self, now: SystemTime) -> Option<T>
If the value is expired, clears the cache. Otherwise, yields the current value.
Trait Implementations§
Source§impl<T, E> Clone for ExpiringCache<T, E>
impl<T, E> Clone for ExpiringCache<T, E>
Auto Trait Implementations§
impl<T, E> Freeze for ExpiringCache<T, E>
impl<T, E> !RefUnwindSafe for ExpiringCache<T, E>
impl<T, E> Send for ExpiringCache<T, E>
impl<T, E> Sync for ExpiringCache<T, E>
impl<T, E> Unpin for ExpiringCache<T, E>where
E: Unpin,
impl<T, E> !UnwindSafe for ExpiringCache<T, E>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 moreCreates a shared type from an unshared type.