Struct cached::TimedSizedCache
source · [−]pub struct TimedSizedCache<K, V> { /* private fields */ }
Expand description
Timed LRU Cache
Stores a limited number of values, evicting expired and least-used entries. Time expiration is determined based on entry insertion time.. The TTL of an entry is not updated when retrieved.
Note: This cache is in-memory only
Implementations
sourceimpl<K: Hash + Eq + Clone, V> TimedSizedCache<K, V>
impl<K: Hash + Eq + Clone, V> TimedSizedCache<K, V>
sourcepub fn with_size_and_lifespan(
size: usize,
seconds: u64
) -> TimedSizedCache<K, V>
pub fn with_size_and_lifespan(
size: usize,
seconds: u64
) -> TimedSizedCache<K, V>
Creates a new SizedCache
with a given size limit and pre-allocated backing data
sourcepub fn with_size_and_lifespan_and_refresh(
size: usize,
seconds: u64,
refresh: bool
) -> TimedSizedCache<K, V>
pub fn with_size_and_lifespan_and_refresh(
size: usize,
seconds: u64,
refresh: bool
) -> TimedSizedCache<K, V>
Creates a new SizedCache
with a given size limit and pre-allocated backing data.
Also set if the ttl should be refreshed on retrieving
pub fn try_with_size_and_lifespan(
size: usize,
seconds: u64
) -> Result<TimedSizedCache<K, V>>
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. Items passed their expiration seconds will be excluded.
sourcepub fn value_order(&self) -> impl Iterator<Item = &(Instant, V)>
pub fn value_order(&self) -> impl Iterator<Item = &(Instant, V)>
Return an iterator of timestamped values in the current order from most to least recently used. Items passed their expiration seconds will be excluded.
sourcepub fn set_refresh(&mut self, refresh: bool)
pub fn set_refresh(&mut self, refresh: bool)
Sets if the lifetime is refreshed when the value is retrieved
Trait Implementations
sourceimpl<K: Hash + Eq + Clone, V> Cached<K, V> for TimedSizedCache<K, V>
impl<K: Hash + Eq + Clone, V> Cached<K, V> for TimedSizedCache<K, V>
sourcefn 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
sourcefn 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
sourcefn 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
sourcefn cache_remove(&mut self, k: &K) -> Option<V>
fn cache_remove(&mut self, k: &K) -> Option<V>
Remove a cached value
sourcefn cache_clear(&mut self)
fn cache_clear(&mut self)
Remove all cached values. Keeps the allocated memory for reuse.
sourcefn cache_reset(&mut self)
fn cache_reset(&mut self)
Remove all cached values. Free memory and return to initial state
sourcefn cache_reset_metrics(&mut self)
fn cache_reset_metrics(&mut self)
Reset misses/hits counters
sourcefn cache_size(&self) -> usize
fn cache_size(&self) -> usize
Return the current cache size (number of elements)
sourcefn cache_hits(&self) -> Option<u64>
fn cache_hits(&self) -> Option<u64>
Return the number of times a cached value was successfully retrieved
sourcefn cache_misses(&self) -> Option<u64>
fn cache_misses(&self) -> Option<u64>
Return the number of times a cached value was unable to be retrieved
sourcefn cache_capacity(&self) -> Option<usize>
fn cache_capacity(&self) -> Option<usize>
Return the cache capacity
sourcefn cache_lifespan(&self) -> Option<u64>
fn cache_lifespan(&self) -> Option<u64>
Return the lifespan of cached values (time to eviction)
sourcefn cache_set_lifespan(&mut self, seconds: u64) -> Option<u64>
fn cache_set_lifespan(&mut self, seconds: u64) -> Option<u64>
Set the lifespan of cached values, returns the old value
sourceimpl<K, V> CachedAsync<K, V> for TimedSizedCache<K, V> where
K: Hash + Eq + Clone + Send,
impl<K, V> CachedAsync<K, V> for TimedSizedCache<K, V> where
K: Hash + Eq + Clone + Send,
fn get_or_set_with<'life0, 'async_trait, F, Fut>(
&'life0 mut self,
key: K,
f: F
) -> Pin<Box<dyn Future<Output = &mut V> + Send + 'async_trait>> where
V: Send,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = V> + Send,
F: 'async_trait,
Fut: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn try_get_or_set_with<'life0, 'async_trait, F, Fut, E>(
&'life0 mut self,
key: K,
f: F
) -> Pin<Box<dyn Future<Output = Result<&mut V, E>> + Send + 'async_trait>> where
V: Send,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Result<V, E>> + Send,
F: 'async_trait,
Fut: 'async_trait,
E: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
sourceimpl<K: Clone, V: Clone> Clone for TimedSizedCache<K, V>
impl<K: Clone, V: Clone> Clone for TimedSizedCache<K, V>
sourcefn clone(&self) -> TimedSizedCache<K, V>
fn clone(&self) -> TimedSizedCache<K, V>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
Auto Trait Implementations
impl<K, V> RefUnwindSafe for TimedSizedCache<K, V> where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for TimedSizedCache<K, V> where
K: Send,
V: Send,
impl<K, V> Sync for TimedSizedCache<K, V> where
K: Sync,
V: Sync,
impl<K, V> Unpin for TimedSizedCache<K, V> where
K: Unpin,
V: Unpin,
impl<K, V> UnwindSafe for TimedSizedCache<K, V> where
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more