Struct cached::stores::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§
source§impl<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
Panics
Will panic if size is 0
sourcepub fn try_with_size_and_lifespan(
size: usize,
seconds: u64
) -> Result<TimedSizedCache<K, V>>
pub fn try_with_size_and_lifespan( size: usize, seconds: u64 ) -> Result<TimedSizedCache<K, V>>
Creates a new TimedSizedCache
with a specified lifespan and 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. 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
sourcepub fn get_store(&self) -> &SizedCache<K, (Instant, V)>
pub fn get_store(&self) -> &SizedCache<K, (Instant, V)>
Returns a reference to the cache’s store
Trait Implementations§
source§impl<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>
source§fn cache_get<Q>(&mut self, key: &Q) -> Option<&V>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
fn cache_get<Q>(&mut self, key: &Q) -> Option<&V>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
source§fn cache_get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
fn cache_get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
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
source§fn cache_set(&mut self, key: K, val: V) -> Option<V>
fn cache_set(&mut self, key: K, val: V) -> Option<V>
source§fn cache_remove<Q>(&mut self, k: &Q) -> Option<V>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
fn cache_remove<Q>(&mut self, k: &Q) -> Option<V>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
source§fn cache_clear(&mut self)
fn cache_clear(&mut self)
source§fn cache_reset(&mut self)
fn cache_reset(&mut self)
source§fn cache_reset_metrics(&mut self)
fn cache_reset_metrics(&mut self)
source§fn cache_size(&self) -> usize
fn cache_size(&self) -> 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_capacity(&self) -> Option<usize>
fn cache_capacity(&self) -> Option<usize>
source§fn cache_lifespan(&self) -> Option<u64>
fn cache_lifespan(&self) -> Option<u64>
source§impl<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,
source§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 + 'async_trait,
Fut: Future<Output = V> + Send + 'async_trait,
Self: 'async_trait,
'life0: 'async_trait,
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 + 'async_trait, Fut: Future<Output = V> + Send + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,
async
only.source§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 + 'async_trait,
Fut: Future<Output = Result<V, E>> + Send + 'async_trait,
E: 'async_trait,
Self: 'async_trait,
'life0: '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 + 'async_trait, Fut: Future<Output = Result<V, E>> + Send + 'async_trait, E: 'async_trait, Self: 'async_trait, 'life0: 'async_trait,
async
only.