pub struct TtlCache<K, V> { /* private fields */ }Expand description
Thread-safe TTL cache with LRU eviction.
The cache is keyed by any Eq + Hash + Clone type and stores any
Clone value. Each insert takes a per-entry TTL. Entries expire on
the first get/insert that observes the expired deadline; an explicit
TtlCache::prune is also provided for bulk collection.
Implementations§
Source§impl<K, V> TtlCache<K, V>
impl<K, V> TtlCache<K, V>
Sourcepub fn new(capacity: NonZeroUsize) -> Self
pub fn new(capacity: NonZeroUsize) -> Self
Create a new cache with the given capacity, backed by TokioClock.
Capacity is a non-zero usize because a zero-capacity cache is degenerate (every insert would immediately evict itself).
Sourcepub fn with_clock(capacity: NonZeroUsize, clock: Arc<dyn Clock>) -> Self
pub fn with_clock(capacity: NonZeroUsize, clock: Arc<dyn Clock>) -> Self
Create a cache backed by a custom Clock implementation.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Current number of entries in the cache (may include not-yet-pruned expired entries).
Sourcepub fn get(&self, key: &K) -> Option<V>
pub fn get(&self, key: &K) -> Option<V>
Look up key. Returns Some(value) on cache hit (and bumps the
entry’s recency); None on miss or expired entry. Expired entries
are removed on observation.
Sourcepub fn insert(&self, key: K, value: V, ttl: Duration)
pub fn insert(&self, key: K, value: V, ttl: Duration)
Insert value under key with the given ttl. If the cache is at
capacity, evicts the least recently used live entry first.
Auto Trait Implementations§
impl<K, V> !Freeze for TtlCache<K, V>
impl<K, V> !RefUnwindSafe for TtlCache<K, V>
impl<K, V> !UnwindSafe for TtlCache<K, V>
impl<K, V> Send for TtlCache<K, V>
impl<K, V> Sync for TtlCache<K, V>
impl<K, V> Unpin for TtlCache<K, V>
impl<K, V> UnsafeUnpin for TtlCache<K, V>
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
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>
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>
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