pub struct DynamicCacheLocal<K, V, S = RandomState> { /* private fields */ }
Expand description
A cache that will only hold onto items that have been requested more than once in recent memory. Single-use items are not held at all. Once an item is requested twice, it is cached until all memory of seeing a request has expired. The length of the memory is adjustable, and must be set at initialization.
Implementations§
Source§impl<K: Clone + Eq + Hash, V, S> DynamicCacheLocal<K, V, S>
impl<K: Clone + Eq + Hash, V, S> DynamicCacheLocal<K, V, S>
Sourcepub fn with_hasher(
mem_len: usize,
hash_builder: S,
) -> DynamicCacheLocal<K, V, S>
pub fn with_hasher( mem_len: usize, hash_builder: S, ) -> DynamicCacheLocal<K, V, S>
Create and initialize a new cache, using the given hash builder to hash keys. The same
warnings given for HashMap::with_hasher
apply here.
Source§impl<K: Clone + Eq + Hash, V> DynamicCacheLocal<K, V>
impl<K: Clone + Eq + Hash, V> DynamicCacheLocal<K, V>
Sourcepub fn get(&mut self, key: &K) -> Option<Arc<V>>
pub fn get(&mut self, key: &K) -> Option<Arc<V>>
Attempt to retrieve a value from the cache. This updates the cache’s memory of what values have been requested.
Sourcepub fn pop(&mut self, key: &K) -> Option<Arc<V>>
pub fn pop(&mut self, key: &K) -> Option<Arc<V>>
Attempt to remove a value from the cache.
This will remove the value itself from the cache, but doesn’t change the cache’s stored
request history. This means that any new [get_or_insert
] calls will re-load the value
into the cache.
Sourcepub fn insert(&mut self, key: &K, v: V) -> Arc<V>
pub fn insert(&mut self, key: &K, v: V) -> Arc<V>
Insert a value into the cache. If the value is already present, an Arc<V>
of the stored
value is returned. If the value is not stored but has been requested more than once, then
it is stored and returned. If the value is not stored and hasn’t been requested more than
once, it is not stored and is simply returned, wrapped as an Arc<V>
.
Sourcepub fn get_or_insert<F: FnOnce() -> V>(&mut self, key: &K, f: F) -> Arc<V>
pub fn get_or_insert<F: FnOnce() -> V>(&mut self, key: &K, f: F) -> Arc<V>
Fetch an item via the cache, potentially filling in the cache on a miss via the function
f
.
Sourcepub fn set_mem_len(&mut self, new_len: usize)
pub fn set_mem_len(&mut self, new_len: usize)
Change the length of the cache’s recent request memory. Some contents of the cache may be removed immediately if the new memory length is shorter than the old memory length.
Sourcepub fn clear_cache(&mut self)
pub fn clear_cache(&mut self)
Clear out all stored values and all memory in the cache.
Sourcepub fn reset_metrics(&mut self)
pub fn reset_metrics(&mut self)
Reset the cache hit/miss metrics.