pub struct EvictingCacheMap<K, V, const N: usize, F, S = DefaultHashBuilder>where
F: Fn(K, V),{ /* private fields */ }Expand description
An evicting cache map.
Backed by a HashMap but maintains order and a maximum capacity. Upon inserting beyond capacity the cache map will remove the eldest elements to make room. Each removal or prune will call the provided prune hook with the values.
Being backed by a HashMap, this container has all the same requirements on its keys as
HashMap. Specifically, Keys must implement Eq and Hash. You can typically derive
these for types with #[derive(PartialEq, Eq, hash)]. If you implement these yourself you must
ensure the following holds:
k1 == k2 -> hash(k1) == hash(k2)You are also responsible to ensure Keys do not mutate such that their hash can change while in
the map. For more information, check HashMap.
Implementations§
Source§impl<K, V, const N: usize> EvictingCacheMap<K, V, N, fn(K, V), DefaultHashBuilder>
impl<K, V, const N: usize> EvictingCacheMap<K, V, N, fn(K, V), DefaultHashBuilder>
Source§impl<K, V, const N: usize, F> EvictingCacheMap<K, V, N, F, DefaultHashBuilder>where
F: Fn(K, V),
impl<K, V, const N: usize, F> EvictingCacheMap<K, V, N, F, DefaultHashBuilder>where
F: Fn(K, V),
Sourcepub fn with_prune_hook(func: F) -> Self
pub fn with_prune_hook(func: F) -> Self
Create a new map with the provided prune hook
Examples found in repository?
14fn main() {
15 let (tx, rx) = mpsc::channel();
16 let mut cachemap: EvictingCacheMap<String, u8, 10, _> =
17 EvictingCacheMap::with_prune_hook(move |k, v| tx.send((k, v)).unwrap());
18 let send = thread::spawn(move || {
19 for x in 0..20 {
20 cachemap.insert(x.to_string(), x)
21 }
22 });
23
24 let recv = thread::spawn(move || {
25 while let Ok((k, v)) = rx.recv() {
26 println!("{k}:{v}")
27 }
28 });
29
30 let _ = send.join();
31 let _ = recv.join();
32}Source§impl<K, V, const N: usize, S> EvictingCacheMap<K, V, N, fn(K, V), S>
impl<K, V, const N: usize, S> EvictingCacheMap<K, V, N, fn(K, V), S>
Sourcepub fn with_hasher(hash_builder: S) -> Self
pub fn with_hasher(hash_builder: S) -> Self
Create a new map with provided hasher
Source§impl<K, V, const N: usize, F, S> EvictingCacheMap<K, V, N, F, S>where
F: Fn(K, V),
impl<K, V, const N: usize, F, S> EvictingCacheMap<K, V, N, F, S>where
F: Fn(K, V),
Sourcepub fn with_hasher_and_prune_hook(hash_builer: S, func: F) -> Self
pub fn with_hasher_and_prune_hook(hash_builer: S, func: F) -> Self
Create a new map with the provided hasher and prune hook
Source§impl<K, V, const N: usize, F> EvictingCacheMap<K, V, N, F>
impl<K, V, const N: usize, F> EvictingCacheMap<K, V, N, F>
Sourcepub fn prune_by(&mut self, len: usize)
pub fn prune_by(&mut self, len: usize)
Remove elements and call prune hook
Removes len elements from the cache and calls the prune hook for each. If len is
greater than the len() of the cache it will leave it empty.
Sourcepub fn prune_by_with(&mut self, len: usize, f: impl Fn(K, V))
pub fn prune_by_with(&mut self, len: usize, f: impl Fn(K, V))
Remove elements and calls provided prune hook
Removes len elements from the cache and calls the prune hook for each. If len is
greater than the len() of th cache it will leave it empty.
Sourcepub fn prune_to(&mut self, len: usize)
pub fn prune_to(&mut self, len: usize)
Remove elements and call the prune hook
Removes elements until the cache is the provided size.
Sourcepub fn prune_to_with(&mut self, len: usize, f: impl Fn(K, V))
pub fn prune_to_with(&mut self, len: usize, f: impl Fn(K, V))
Removeelements and call the provided prune hook
Removes elements until the cache is the provided size.
Sourcepub fn promote<Q>(&mut self, key: &Q)
pub fn promote<Q>(&mut self, key: &Q)
Promote the element if it exists
This moves an element to the top of the cache as-if removed and re-inserted but does not call the prune hook.
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Checks if cache contains this key
Does not promote the key.
Sourcepub fn insert(&mut self, key: K, value: V)
pub fn insert(&mut self, key: K, value: V)
Insert element to the top of the cache
If the container is full this will prune a single element, calling the prune hook.
Examples found in repository?
14fn main() {
15 let (tx, rx) = mpsc::channel();
16 let mut cachemap: EvictingCacheMap<String, u8, 10, _> =
17 EvictingCacheMap::with_prune_hook(move |k, v| tx.send((k, v)).unwrap());
18 let send = thread::spawn(move || {
19 for x in 0..20 {
20 cachemap.insert(x.to_string(), x)
21 }
22 });
23
24 let recv = thread::spawn(move || {
25 while let Ok((k, v)) = rx.recv() {
26 println!("{k}:{v}")
27 }
28 });
29
30 let _ = send.join();
31 let _ = recv.join();
32}Sourcepub fn get<Q>(&mut self, key: &Q) -> Option<&V>
pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
Get a reference to the corresponding element and promotes it to the top of the cache.
Sourcepub fn get_no_promote<Q>(&mut self, key: &Q) -> Option<&V>
pub fn get_no_promote<Q>(&mut self, key: &Q) -> Option<&V>
Get a reference to the corresponding element without promoting it.
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Get a mutable reference to the corresponding element and promote it to the top of the cache.
Sourcepub fn get_mut_no_promote<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut_no_promote<Q>(&mut self, key: &Q) -> Option<&mut V>
Get a mutable reference to the corresponding element without promoting it.
Sourcepub fn get_key_value<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
Get a reference to the key-value pair and promote it to the top of the cache.
Sourcepub fn get_key_value_no_promote<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
pub fn get_key_value_no_promote<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
Get a reference to the key-value pair without promoting it.
Sourcepub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)>
pub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)>
Get a mutable reference to the key-value pair and promote it to the top of the cache.
Sourcepub fn get_key_value_mut_no_promote<Q>(
&mut self,
key: &Q,
) -> Option<(&K, &mut V)>
pub fn get_key_value_mut_no_promote<Q>( &mut self, key: &Q, ) -> Option<(&K, &mut V)>
Get a mutable reference to the key-value pair without promoting it.
Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
Remove and return a value from the map.
Does not call the prune hook.
Sourcepub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
Remove and return a key-value pair from the map.
Does not call the prune hook.