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 size
is also specified; without 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 with_size(size: usize) -> ExpiringLruCache<K, V>
pub fn with_size(size: usize) -> ExpiringLruCache<K, V>
Creates a new ExpiringLruCache with a given size limit and
pre-allocated backing data.
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_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 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.