pub struct ExpiringLruCache<K: Hash + Eq, V: Expires> { /* 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.
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 builder() -> ExpiringLruCacheBuilder<K, V>
pub fn builder() -> ExpiringLruCacheBuilder<K, V>
Return a builder for constructing an ExpiringLruCache.
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 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. If no on_evict callback was configured, it falls back to
the plain cache_clear.
Trait Implementations§
Source§impl<K: Hash + Eq + Clone, V: Expires> CacheEvict for ExpiringLruCache<K, V>
impl<K: Hash + Eq + Clone, V: Expires> CacheEvict for ExpiringLruCache<K, V>
Source§impl<K: Hash + Eq + Clone, V: Expires> Cached<K, V> for ExpiringLruCache<K, V>
impl<K: Hash + Eq + Clone, V: Expires> Cached<K, V> for ExpiringLruCache<K, V>
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<F: FnOnce() -> V>(&mut self, k: K, f: F) -> &mut V
fn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, k: K, f: F) -> &mut V
Source§fn cache_try_get_or_set_with<F: FnOnce() -> Result<V, E>, E>(
&mut self,
key: K,
f: F,
) -> Result<&mut V, E>
fn cache_try_get_or_set_with<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_remove<Q>(&mut self, k: &Q) -> Option<V>
fn cache_remove<Q>(&mut self, k: &Q) -> Option<V>
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)>
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_capacity(&self) -> Option<usize>
fn cache_capacity(&self) -> Option<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>, Box<dyn Error>>
fn cache_try_set(&mut self, k: K, v: V) -> Result<Option<V>, Box<dyn Error>>
Self::cache_set. Returns Err if the store cannot accept the entry
(e.g. the TTL duration overflows Instant bounds). The default implementation is
infallible and delegates to Self::cache_set.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>, Box<dyn Error>>
fn try_set(&mut self, k: K, v: V) -> Result<Option<V>, Box<dyn Error>>
cache_try_set.Source§fn get_or_set_with<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &mut V
fn get_or_set_with<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &mut V
cache_get_or_set_with.Source§fn try_get_or_set_with<F: FnOnce() -> Result<V, E>, E>(
&mut self,
k: K,
f: F,
) -> Result<&mut V, E>
fn try_get_or_set_with<F: FnOnce() -> Result<V, E>, E>( &mut self, k: K, f: F, ) -> Result<&mut V, E>
cache_try_get_or_set_with.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 cache_delete<Q>(&mut self, k: &Q) -> bool
fn cache_delete<Q>(&mut self, k: &Q) -> bool
true if an entry was
physically deleted (including expired entries), false if the key was absent. Read moreSource§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 contains<Q>(&mut self, k: &Q) -> bool
fn contains<Q>(&mut self, k: &Q) -> bool
true if the cache contains a value for the given key. Read moreSource§fn clear(&mut self)
fn clear(&mut self)
cache_clear.Source§fn len(&self) -> usize
fn len(&self) -> usize
cache_size.Source§fn metrics(&self) -> CacheMetrics
fn metrics(&self) -> CacheMetrics
Source§impl<K, V> CachedAsync<K, V> for ExpiringLruCache<K, V>
Available on crate feature async_core only.
impl<K, V> CachedAsync<K, V> for ExpiringLruCache<K, V>
async_core only.Source§fn async_get_or_set_with<'a, F, Fut>(
&'a mut self,
k: K,
f: F,
) -> impl Future<Output = &'a mut V> + Send + 'a
fn async_get_or_set_with<'a, F, Fut>( &'a mut self, k: K, f: F, ) -> impl Future<Output = &'a mut V> + Send + 'a
Source§fn async_try_get_or_set_with<'a, F, Fut, E>(
&'a mut self,
k: K,
f: F,
) -> impl Future<Output = Result<&'a mut V, E>> + Send + 'a
fn async_try_get_or_set_with<'a, F, Fut, E>( &'a mut self, k: K, f: F, ) -> impl Future<Output = Result<&'a mut V, E>> + Send + 'a
async_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.