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<Q>(&mut self, key: &Q) -> Option<&V>
fn cache_get<Q>(&mut self, key: &Q) -> Option<&V>
Attempt to retrieve a cached value Read more
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>
Attempt to retrieve a cached value with mutable access Read more
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_try_get_or_set_with<F: FnOnce() -> Result<V, E>, E>(
&mut self,
k: K,
f: F,
) -> Result<&mut V, E>
fn cache_try_get_or_set_with<F: FnOnce() -> Result<V, E>, E>( &mut self, k: K, f: F, ) -> Result<&mut V, E>
Get or insert a key, value pair with error handling
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>
impl<K, V> CachedAsync<K, V> for SizedCache<K, V>
Source§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>>
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>>
Available on crate feature
async
only.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> Debug for SizedCache<K, V>
impl<K, V> Debug for SizedCache<K, V>
Source§impl<K, V> PartialEq for SizedCache<K, V>
impl<K, V> PartialEq for SizedCache<K, V>
impl<K, V> Eq for SizedCache<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for SizedCache<K, V>
impl<K, V> RefUnwindSafe for SizedCache<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for SizedCache<K, V>
impl<K, V> Sync for SizedCache<K, V>
impl<K, V> Unpin for SizedCache<K, V>
impl<K, V> UnwindSafe for SizedCache<K, V>where
K: UnwindSafe,
V: UnwindSafe,
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)