Struct cached::SizedCache
source · pub struct SizedCache<K, V> { /* private fields */ }
Expand description
Least Recently Used / Sized
Cache
Stores up to a specified size before beginning to evict the least recently used keys
Note: This cache is in-memory only
Implementations§
source§impl<K: Hash + Eq + Clone, V> SizedCache<K, V>
impl<K: Hash + Eq + Clone, V> SizedCache<K, V>
pub fn with_capacity(size: usize) -> SizedCache<K, V>
👎Deprecated since 0.5.1: method renamed to
with_size
sourcepub fn with_size(size: usize) -> SizedCache<K, V>
pub fn with_size(size: usize) -> SizedCache<K, V>
Creates a new SizedCache
with a given size limit and pre-allocated backing data
Panics
Will panic if size is 0
sourcepub fn try_with_size(size: usize) -> Result<SizedCache<K, V>>
pub fn try_with_size(size: usize) -> Result<SizedCache<K, V>>
Creates a new SizedCache
with a given size limit and pre-allocated backing data
Errors
Will return a std::io::Error
, depending on the error
sourcepub fn key_order(&self) -> impl Iterator<Item = &K>
pub fn key_order(&self) -> impl Iterator<Item = &K>
Return an iterator of keys in the current order from most to least recently used.
sourcepub fn value_order(&self) -> impl Iterator<Item = &V>
pub fn value_order(&self) -> impl Iterator<Item = &V>
Return an iterator of values in the current order from most to least recently used.
pub fn retain<F: Fn(&K, &V) -> bool>(&mut self, keep: F)
Trait Implementations§
source§impl<K: Hash + Eq + Clone, V> Cached<K, V> for SizedCache<K, V>
impl<K: Hash + Eq + Clone, V> Cached<K, V> for SizedCache<K, V>
source§fn cache_get_mut(&mut self, key: &K) -> Option<&mut V>
fn cache_get_mut(&mut self, key: &K) -> Option<&mut V>
Attempt to retrieve a cached value with mutable access
source§fn cache_set(&mut self, key: K, val: V) -> Option<V>
fn cache_set(&mut self, key: K, val: V) -> Option<V>
Insert a key, value pair and return the previous value
source§fn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &mut V
fn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &mut V
Get or insert a key, value pair
source§fn cache_remove(&mut self, k: &K) -> Option<V>
fn cache_remove(&mut self, k: &K) -> Option<V>
Remove a cached value
source§fn cache_clear(&mut self)
fn cache_clear(&mut self)
Remove all cached values. Keeps the allocated memory for reuse.
source§fn cache_reset(&mut self)
fn cache_reset(&mut self)
Remove all cached values. Free memory and return to initial state
source§fn cache_reset_metrics(&mut self)
fn cache_reset_metrics(&mut self)
Reset misses/hits counters
source§fn cache_size(&self) -> usize
fn cache_size(&self) -> usize
Return the current cache size (number of elements)
source§fn cache_hits(&self) -> Option<u64>
fn cache_hits(&self) -> Option<u64>
Return the number of times a cached value was successfully retrieved
source§fn cache_misses(&self) -> Option<u64>
fn cache_misses(&self) -> Option<u64>
Return the number of times a cached value was unable to be retrieved
source§fn cache_capacity(&self) -> Option<usize>
fn cache_capacity(&self) -> Option<usize>
Return the cache capacity
source§fn cache_lifespan(&self) -> Option<u64>
fn cache_lifespan(&self) -> Option<u64>
Return the lifespan of cached values (time to eviction)
source§impl<K, V> CachedAsync<K, V> for SizedCache<K, V>where
K: Hash + Eq + Clone + Send,
impl<K, V> CachedAsync<K, V> for SizedCache<K, V>where
K: Hash + Eq + Clone + Send,
fn get_or_set_with<'life0, 'async_trait, F, Fut>(
&'life0 mut self,
k: K,
f: F
) -> Pin<Box<dyn Future<Output = &mut V> + Send + 'async_trait>>where
V: Send,
F: FnOnce() -> Fut + Send + 'async_trait,
Fut: Future<Output = V> + Send + 'async_trait,
Self: 'async_trait,
'life0: 'async_trait,
fn try_get_or_set_with<'life0, 'async_trait, F, Fut, E>(
&'life0 mut self,
k: K,
f: F
) -> Pin<Box<dyn Future<Output = Result<&mut V, E>> + Send + 'async_trait>>where
V: Send,
F: FnOnce() -> Fut + Send + 'async_trait,
Fut: Future<Output = Result<V, E>> + Send + 'async_trait,
E: 'async_trait,
Self: 'async_trait,
'life0: 'async_trait,
source§impl<K: Clone, V: Clone> Clone for SizedCache<K, V>
impl<K: Clone, V: Clone> Clone for SizedCache<K, V>
source§fn clone(&self) -> SizedCache<K, V>
fn clone(&self) -> SizedCache<K, V>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl<K, V> PartialEq<SizedCache<K, V>> for SizedCache<K, V>where
K: Eq + Hash + Clone,
V: PartialEq,
impl<K, V> PartialEq<SizedCache<K, V>> for SizedCache<K, V>where
K: Eq + Hash + Clone,
V: PartialEq,
source§fn eq(&self, other: &SizedCache<K, V>) -> bool
fn eq(&self, other: &SizedCache<K, V>) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.