pub struct DynamicCache<K, V, S = RandomState> { /* private fields */ }Expand description
A cache that will only hold onto items that have been reqeuested 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 it requested has expired. The length of the memory is adjustable, and must be set at initialization.
This version of the cache can be shared across threads without issue, as it is an instance of
DynamicCacheLocal held by an Arc<Mutex<T>>.
Implementations§
Source§impl<K: Clone + Eq + Hash, V, S> DynamicCache<K, V, S>
impl<K: Clone + Eq + Hash, V, S> DynamicCache<K, V, S>
Sourcepub fn with_hasher(mem_len: usize, hash_builder: S) -> DynamicCache<K, V, S>
pub fn with_hasher(mem_len: usize, hash_builder: S) -> DynamicCache<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> DynamicCache<K, V>
impl<K: Clone + Eq + Hash, V> DynamicCache<K, V>
Sourcepub fn get(&self, key: &K) -> Option<Arc<V>>
pub fn get(&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(&self, key: &K) -> Option<Arc<V>>
pub fn pop(&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(&self, key: &K, value: V) -> Arc<V>
pub fn insert(&self, key: &K, value: 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>(&self, key: &K, f: F) -> Arc<V>
pub fn get_or_insert<F: FnOnce() -> V>(&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. The cache is unlocked while calling the function, so f may be called more than once
with the same parameters if there are several threads using the cache.
Sourcepub fn set_mem_len(&self, new_len: usize)
pub fn set_mem_len(&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(&self)
pub fn clear_cache(&self)
Clear out all stored values and all memory in the cache.
Sourcepub fn hits_misses(&self) -> (u64, u64)
pub fn hits_misses(&self) -> (u64, u64)
Get the cache metrics as a pair (hits, misses).
Sourcepub fn reset_metrics(&self)
pub fn reset_metrics(&self)
Reset the cache hit/miss metrics.