pub struct ExpiringLruCache<K, V, S = DefaultHashBuilder> { /* private fields */ }Expand description
LRU-bounded cache with per-value expiry.
Stores values that implement the Expires trait so that expiration
is determined by the values themselves. This is useful for caching
values which themselves contain an expiry timestamp.
For an unbounded variant (no size cap), see ExpiringCache.
When using the #[cached] proc macro, expires = true selects this store when max_size
is also specified; without max_size, it selects the unbounded ExpiringCache.
Note: This cache is in-memory only.
cache_size / iter / evict contract: cache_size() returns the raw stored entry count
and may include expired-but-not-yet-swept entries. iter() omits expired entries
from the view but does not remove them. Call evict() (via CacheEvict)
to physically remove expired entries and obtain an accurate live count.
Note: once specialization is stable (#[feature(specialization)]), the expiry-checking
behavior here could be folded into LruCache via a specialized Cached<K, V> impl
for V: Expires, eliminating this separate type. Until then, the two must remain
distinct because overlapping blanket impls are not allowed on stable Rust.
Implementations§
Source§impl<K: Clone + Hash + Eq, V: Expires> ExpiringLruCache<K, V>
impl<K: Clone + Hash + Eq, V: Expires> ExpiringLruCache<K, V>
Sourcepub fn new(max_size: usize) -> Self
pub fn new(max_size: usize) -> Self
Construct a ready-to-use ExpiringLruCache holding up to max_size entries.
For optional settings (on_evict) use builder.
§Panics
Panics if max_size is 0, or if pre-allocating the backing store for
max_size entries fails (e.g. usize::MAX). Use builder
with build to handle those cases without panicking.
Sourcepub fn builder() -> ExpiringLruCacheBuilder<K, V>
pub fn builder() -> ExpiringLruCacheBuilder<K, V>
Return a builder for constructing an ExpiringLruCache.
Source§impl<K: Clone + Hash + Eq, V: Expires, S: BuildHasher> ExpiringLruCache<K, V, S>
impl<K: Clone + Hash + Eq, V: Expires, S: BuildHasher> ExpiringLruCache<K, V, S>
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the maximum number of entries this cache will hold before evicting.
This is the bound set via ExpiringLruCacheBuilder::max_size,
not the current number of entries — use cache_size for that.
Sourcepub fn set_max_size(&mut self, max_size: usize) -> Option<usize>
pub fn set_max_size(&mut self, max_size: usize) -> Option<usize>
Change the maximum number of entries, returning the previous capacity; shrinking below the current entry count immediately evicts least-recently-used entries.
Eviction on shrink fires on_evict and counts evictions until the cache
fits. Growing the capacity does not pre-allocate; the backing stores grow
on demand as entries are inserted.
This is useful for sizing a #[cached(create = "{ ... }")] cache from a value
loaded at startup (e.g. config), then adjusting it later as load changes.
§Panics
Panics if max_size is 0. Use try_set_max_size
to validate first and avoid the panic.
Sourcepub fn try_set_max_size(
&mut self,
max_size: usize,
) -> Result<Option<usize>, SetMaxSizeError>
pub fn try_set_max_size( &mut self, max_size: usize, ) -> Result<Option<usize>, SetMaxSizeError>
Fallible counterpart of set_max_size: validates
that max_size is non-zero and then delegates to set_max_size.
Returns the previous capacity wrapped in Some on success.
§Errors
Returns SetMaxSizeError::ZeroMaxSize if max_size is 0.
Sourcepub fn retain<F: FnMut(&K, &V) -> bool>(&mut self, keep: F) -> usize
pub fn retain<F: FnMut(&K, &V) -> bool>(&mut self, keep: F) -> usize
Retain only entries that are unexpired and satisfy keep.
Iterates the entries held in the underlying LRU store (most- to
least-recently-used) and removes every entry that is already expired
(per Expires::is_expired) or for which keep returns false —
expired entries are removed without consulting keep. on_evict is
called and the eviction counter incremented for each removed entry.
The LRU recency order of the surviving entries is unchanged.
This matches LruTtlCache::retain; the plain
LruCache::retain has no expiry dimension and
removes solely on the predicate.
Returns the number of entries removed: the count folds together entries keep
rejected and entries swept for having already expired, since expiry removal is
unconditional regardless of what keep returns. retain is deliberately not
#[must_use]: discarding the count is a legitimate and common use, matching
existing bare cache.retain(...); call sites.
Sourcepub fn cache_clear_with_on_evict(&mut self)
pub fn cache_clear_with_on_evict(&mut self)
Remove all entries and fire the on_evict callback for each one, incrementing the
evictions counter.
Unlike cache_clear (which removes entries silently),
this method invokes on_evict for every removed entry (whether or not they had expired)
and increments evictions. The eviction count does not depend on whether an
on_evict callback is configured.
Sourcepub fn iter_order(&self) -> Vec<(K, CacheValue<V>)>
pub fn iter_order(&self) -> Vec<(K, CacheValue<V>)>
Return all live entries in current LRU order (most-recently-used first)
as (K, CacheValue<V>) pairs. ExpiringLruCache
carries no per-entry metadata beyond what V: Expires itself exposes, so
the wrapper’s metadata type is (); the wrapper Derefs to V.
Expired entries are excluded.
Sourcepub fn key_order(&self) -> Vec<K>where
K: Clone,
pub fn key_order(&self) -> Vec<K>where
K: Clone,
Return a Vec of keys in the current order from most to least recently
used. Expired entries are excluded.
Sourcepub fn value_order(&self) -> Vec<CacheValue<V>>where
V: Clone,
pub fn value_order(&self) -> Vec<CacheValue<V>>where
V: Clone,
Return a Vec of CacheValue-wrapped values in the
current order from most to least recently used. Expired entries are excluded.
Trait Implementations§
Source§impl<K: Hash + Eq + Clone, V: Expires, S: BuildHasher> CacheEvict for ExpiringLruCache<K, V, S>
impl<K: Hash + Eq + Clone, V: Expires, S: BuildHasher> CacheEvict for ExpiringLruCache<K, V, S>
Source§impl<K: Hash + Eq + Clone, V: Expires, S: BuildHasher> CacheExpiry<K, V> for ExpiringLruCache<K, V, S>
impl<K: Hash + Eq + Clone, V: Expires, S: BuildHasher> CacheExpiry<K, V> for ExpiringLruCache<K, V, S>
Source§fn cache_peek_expires_at<Q>(&self, k: &Q) -> (Option<V>, Option<Instant>)
fn cache_peek_expires_at<Q>(&self, k: &Q) -> (Option<V>, Option<Instant>)
Returns the stored value and its expiry instant, with no read side effects.
The instant is whatever Expires::expires_at reports for the value, and on
this store that is advisory only: it is None unless the value type overrides
expires_at (including for an entry that is expired), and it may be in the
past for an entry Expires::is_expired reports as live. is_expired remains
the authority on liveness here; use
cache_peek_with_expiry_status for
that. Uses the same lookup as that peek (the inner LruCache’s non-promoting
cache_peek): no hit/miss counting, no LRU promotion, no removal of an expired
entry.
Source§fn cache_expires_at<Q>(&self, k: &Q) -> (bool, Option<Instant>)
fn cache_expires_at<Q>(&self, k: &Q) -> (bool, Option<Instant>)
Returns whether the key is present and its expiry instant, without the value.
The value-free counterpart of
cache_peek_expires_at: same non-promoting
lookup (the inner LruCache’s cache_peek), same advisory deadline, no clone and
no V: Clone bound. (false, None) when the key is absent; (true, deadline)
when it is present, where deadline is whatever Expires::expires_at reports
for the stored value.
The presence flag is not advisory: the deadline is. On this store the deadline
is None for any value type that does not override Expires::expires_at,
including an entry that is expired – so (true, None) means “present, deadline
unknown”, not “present and live”. is_expired, not this deadline, remains the
authority on liveness; see the CacheExpiry trait docs’ Expires-store caveat.
An expired entry is reported present and is not removed. No hit/miss
counting, no LRU promotion.
Source§fn peek_expires_at<Q>(&self, key: &Q) -> (Option<V>, Option<Instant>)
fn peek_expires_at<Q>(&self, key: &Q) -> (Option<V>, Option<Instant>)
cache_peek_expires_at.Source§impl<K: Hash + Eq + Clone, V: Expires, S: BuildHasher> Cached<K, V> for ExpiringLruCache<K, V, S>
impl<K: Hash + Eq + Clone, V: Expires, S: BuildHasher> Cached<K, V> for ExpiringLruCache<K, V, S>
Source§fn cache_remove<Q>(&mut self, k: &Q) -> Option<V>
fn cache_remove<Q>(&mut self, k: &Q) -> Option<V>
Removes the entry and returns the value only if it is still live;
an expired value is removed but reported as None. Use
cache_remove_entry to receive the
value regardless of expiry.
Source§fn cache_remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
fn cache_remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
Removes the entry and returns it regardless of expiry (unlike
cache_remove, which filters expired values).
Source§fn cache_contains<Q>(&mut self, k: &Q) -> bool
fn cache_contains<Q>(&mut self, k: &Q) -> bool
Check whether the cache contains a live (non-expired) entry for k.
Delegates to CachedPeek::cache_peek, so it records no hit/miss
metrics, performs no recency promotion, and reports absent/expired
entries as false.
Source§fn cache_get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
fn cache_get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Source§fn cache_get_or_set_with_mut<F: FnOnce() -> V>(&mut self, k: K, f: F) -> &mut V
fn cache_get_or_set_with_mut<F: FnOnce() -> V>(&mut self, k: K, f: F) -> &mut V
Source§fn cache_try_get_or_set_with_mut<F: FnOnce() -> Result<V, E>, E>(
&mut self,
key: K,
f: F,
) -> Result<&mut V, E>
fn cache_try_get_or_set_with_mut<F: FnOnce() -> Result<V, E>, E>( &mut self, key: K, f: F, ) -> Result<&mut V, E>
Source§fn cache_set(&mut self, k: K, v: V) -> Option<V>
fn cache_set(&mut self, k: K, v: V) -> Option<V>
Source§fn cache_clear(&mut self)
fn cache_clear(&mut self)
cache_reset_metrics afterward,
or use cache_reset to do both at once.Source§fn cache_reset(&mut self)
fn cache_reset(&mut self)
on_evict callbacks — is preserved.
To reset entries without resetting metrics, use cache_clear.Source§fn cache_size(&self) -> usize
fn cache_size(&self) -> usize
Source§fn cache_hits(&self) -> Option<u64>
fn cache_hits(&self) -> Option<u64>
Source§fn cache_misses(&self) -> Option<u64>
fn cache_misses(&self) -> Option<u64>
Source§fn cache_evictions(&self) -> Option<u64>
fn cache_evictions(&self) -> Option<u64>
Source§fn cache_reset_metrics(&mut self)
fn cache_reset_metrics(&mut self)
Source§fn cache_try_set(&mut self, k: K, v: V) -> Result<Option<V>, Self::Error>
fn cache_try_set(&mut self, k: K, v: V) -> Result<Option<V>, Self::Error>
Self::cache_set for custom stores whose insertion can fail.
The default implementation is infallible and delegates to Self::cache_set; it
always returns Ok. Every built-in in-memory store keeps that default
(type Error = std::convert::Infallible). Read moreSource§fn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &V
fn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &V
Source§impl<K, V, S> CachedGetOrSetAsync<K, V> for ExpiringLruCache<K, V, S>
Available on crate feature async_core only.
impl<K, V, S> CachedGetOrSetAsync<K, V> for ExpiringLruCache<K, V, S>
async_core only.Source§fn async_cache_get_or_set_with_mut<'a, F, Fut>(
&'a mut self,
k: K,
f: F,
) -> impl Future<Output = &'a mut V> + Send + 'a
fn async_cache_get_or_set_with_mut<'a, F, Fut>( &'a mut self, k: K, f: F, ) -> impl Future<Output = &'a mut V> + Send + 'a
async_cache_get_or_set_with: returns
&mut V. Stores implement this method; the shared-reference variant
delegates to it.Source§fn async_cache_try_get_or_set_with_mut<'a, F, Fut, E>(
&'a mut self,
k: K,
f: F,
) -> impl Future<Output = Result<&'a mut V, E>> + Send + 'a
fn async_cache_try_get_or_set_with_mut<'a, F, Fut, E>( &'a mut self, k: K, f: F, ) -> impl Future<Output = Result<&'a mut V, E>> + Send + 'a
async_cache_try_get_or_set_with:
returns Result<&mut V, E>.Source§fn async_cache_get_or_set_with<'a, F, Fut>(
&'a mut self,
k: K,
f: F,
) -> impl Future<Output = &'a V> + Send + 'a
fn async_cache_get_or_set_with<'a, F, Fut>( &'a mut self, k: K, f: F, ) -> impl Future<Output = &'a V> + Send + 'a
Source§fn async_cache_try_get_or_set_with<'a, F, Fut, E>(
&'a mut self,
k: K,
f: F,
) -> impl Future<Output = Result<&'a V, E>> + Send + 'a
fn async_cache_try_get_or_set_with<'a, F, Fut, E>( &'a mut self, k: K, f: F, ) -> impl Future<Output = Result<&'a V, E>> + Send + 'a
async_cache_get_or_set_with, but
f is fallible: on a miss the value is cached only if f resolves to
Ok, and an Err is returned without caching. Read moreSource§impl<K: Hash + Eq + Clone, V: Expires, S: BuildHasher> CachedIter<K, V> for ExpiringLruCache<K, V, S>
impl<K: Hash + Eq + Clone, V: Expires, S: BuildHasher> CachedIter<K, V> for ExpiringLruCache<K, V, S>
Source§fn iter<'a>(&'a self) -> impl Iterator<Item = (&'a K, &'a V)> + 'awhere
K: 'a,
V: 'a,
fn iter<'a>(&'a self) -> impl Iterator<Item = (&'a K, &'a V)> + 'awhere
K: 'a,
V: 'a,
Source§impl<K: Hash + Eq + Clone, V: Expires, S: BuildHasher> CachedPeek<K, V> for ExpiringLruCache<K, V, S>
impl<K: Hash + Eq + Clone, V: Expires, S: BuildHasher> CachedPeek<K, V> for ExpiringLruCache<K, V, S>
Source§impl<K, V, S> Clone for ExpiringLruCache<K, V, S>
impl<K, V, S> Clone for ExpiringLruCache<K, V, S>
Source§impl<K: Hash + Eq + Clone, V: Expires + Clone, S: BuildHasher> CloneCached<K, V> for ExpiringLruCache<K, V, S>
impl<K: Hash + Eq + Clone, V: Expires + Clone, S: BuildHasher> CloneCached<K, V> for ExpiringLruCache<K, V, S>
Source§fn cache_peek_with_expiry_status<Q>(&self, k: &Q) -> (Option<V>, bool)
fn cache_peek_with_expiry_status<Q>(&self, k: &Q) -> (Option<V>, bool)
Peek at the entry (including expired entries) without any read side effects.
Returns (Some(v), true) for an expired entry, (Some(v), false) for a live
entry, and (None, false) when the key is absent. Does not update hit/miss
counters and does not promote in LRU order.
Source§fn cache_get_with_expiry_status<Q>(&mut self, k: &Q) -> (Option<V>, bool)
fn cache_get_with_expiry_status<Q>(&mut self, k: &Q) -> (Option<V>, bool)
Source§fn get_with_expiry_status<Q>(&mut self, key: &Q) -> (Option<V>, bool)
fn get_with_expiry_status<Q>(&mut self, key: &Q) -> (Option<V>, bool)
cache_get_with_expiry_status.Source§impl<K, V, S> Debug for ExpiringLruCache<K, V, S>
impl<K, V, S> Debug for ExpiringLruCache<K, V, S>
impl<K, V, S> Eq for ExpiringLruCache<K, V, S>
Source§impl<K, V, S> PartialEq for ExpiringLruCache<K, V, S>
Two ExpiringLruCache values are equal when their stored entries are equal
(same keys, same values). Equality is membership-based: LRU recency order is
not compared. Metrics (hits, misses, evictions) and the on_evict callback
are not part of the comparison.
impl<K, V, S> PartialEq for ExpiringLruCache<K, V, S>
Two ExpiringLruCache values are equal when their stored entries are equal
(same keys, same values). Equality is membership-based: LRU recency order is
not compared. Metrics (hits, misses, evictions) and the on_evict callback
are not part of the comparison.
Auto Trait Implementations§
impl<K, V, S = RandomState> !Freeze for ExpiringLruCache<K, V, S>
impl<K, V, S = RandomState> !RefUnwindSafe for ExpiringLruCache<K, V, S>
impl<K, V, S = RandomState> !UnwindSafe for ExpiringLruCache<K, V, S>
impl<K, V, S> Send for ExpiringLruCache<K, V, S>
impl<K, V, S> Sync for ExpiringLruCache<K, V, S>
impl<K, V, S> Unpin for ExpiringLruCache<K, V, S>
impl<K, V, S> UnsafeUnpin for ExpiringLruCache<K, V, S>
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
Source§impl<K, V, T> CachedExt<K, V> for Twhere
T: Cached<K, V>,
impl<K, V, T> CachedExt<K, V> for Twhere
T: Cached<K, V>,
Source§fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
cache_get_mut.Source§fn set(&mut self, k: K, v: V) -> Option<V>
fn set(&mut self, k: K, v: V) -> Option<V>
cache_set.Source§fn try_set(
&mut self,
k: K,
v: V,
) -> Result<Option<V>, <T as Cached<K, V>>::Error>
fn try_set( &mut self, k: K, v: V, ) -> Result<Option<V>, <T as Cached<K, V>>::Error>
cache_try_set.Source§fn get_or_set_with<F>(&mut self, key: K, f: F) -> &Vwhere
F: FnOnce() -> V,
fn get_or_set_with<F>(&mut self, key: K, f: F) -> &Vwhere
F: FnOnce() -> V,
cache_get_or_set_with.Source§fn get_or_set_with_mut<F>(&mut self, key: K, f: F) -> &mut Vwhere
F: FnOnce() -> V,
fn get_or_set_with_mut<F>(&mut self, key: K, f: F) -> &mut Vwhere
F: FnOnce() -> V,
cache_get_or_set_with_mut.Source§fn try_get_or_set_with<F, E>(&mut self, k: K, f: F) -> Result<&V, E>
fn try_get_or_set_with<F, E>(&mut self, k: K, f: F) -> Result<&V, E>
cache_try_get_or_set_with.Source§fn try_get_or_set_with_mut<F, E>(&mut self, k: K, f: F) -> Result<&mut V, E>
fn try_get_or_set_with_mut<F, E>(&mut self, k: K, f: F) -> Result<&mut V, E>
cache_try_get_or_set_with_mut.Source§fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
cache_remove_entry.Source§fn delete<Q>(&mut self, k: &Q) -> bool
fn delete<Q>(&mut self, k: &Q) -> bool
true if an entry was
physically deleted (including expired entries). Delegates to
cache_delete.Source§fn clear(&mut self)
fn clear(&mut self)
cache_clear.Source§fn reset(&mut self)
fn reset(&mut self)
cache_reset.Source§fn len(&self) -> usize
fn len(&self) -> usize
cache_size. Read moreSource§fn hits(&self) -> Option<u64>
fn hits(&self) -> Option<u64>
cache_hits.Source§fn misses(&self) -> Option<u64>
fn misses(&self) -> Option<u64>
cache_misses.Source§fn capacity(&self) -> Option<usize>
fn capacity(&self) -> Option<usize>
cache_capacity. Read moreSource§fn evictions(&self) -> Option<u64>
fn evictions(&self) -> Option<u64>
cache_evictions.Source§fn metrics(&self) -> CacheMetrics
fn metrics(&self) -> CacheMetrics
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.