pub struct Cache<Key, Val, We = UnitWeighter, B = DefaultHashBuilder, L = DefaultLifecycle<Key, Val>> {
shard: CacheShard<Key, Val, We, B, L, SharedPlaceholder>,
}Expand description
A non-concurrent cache.
Fields§
§shard: CacheShard<Key, Val, We, B, L, SharedPlaceholder>Implementations§
Source§impl<Key: Eq + Hash, Val, We: Weighter<Key, Val>> Cache<Key, Val, We>
impl<Key: Eq + Hash, Val, We: Weighter<Key, Val>> Cache<Key, Val, We>
pub fn with_weighter( estimated_items_capacity: usize, weight_capacity: u64, weighter: We, ) -> Self
Source§impl<Key: Eq + Hash, Val, We: Weighter<Key, Val>, B: BuildHasher, L: Lifecycle<Key, Val>> Cache<Key, Val, We, B, L>
impl<Key: Eq + Hash, Val, We: Weighter<Key, Val>, B: BuildHasher, L: Lifecycle<Key, Val>> Cache<Key, Val, We, B, L>
Sourcepub fn with(
estimated_items_capacity: usize,
weight_capacity: u64,
weighter: We,
hash_builder: B,
lifecycle: L,
) -> Self
pub fn with( estimated_items_capacity: usize, weight_capacity: u64, weighter: We, hash_builder: B, lifecycle: L, ) -> Self
Creates a new cache that can hold up to weight_capacity in weight.
estimated_items_capacity is the estimated number of items the cache is expected to hold,
roughly equivalent to weight_capacity / average item weight.
Sourcepub fn with_options(
options: Options,
weighter: We,
hash_builder: B,
lifecycle: L,
) -> Self
pub fn with_options( options: Options, weighter: We, hash_builder: B, lifecycle: L, ) -> Self
Constructs a cache based on OptionsBuilder.
§Example
use quick_cache::{unsync::{Cache, DefaultLifecycle}, OptionsBuilder, UnitWeighter, DefaultHashBuilder};
Cache::<(String, u64), String>::with_options(
OptionsBuilder::new()
.estimated_items_capacity(10000)
.weight_capacity(10000)
.build()
.unwrap(),
UnitWeighter,
DefaultHashBuilder::default(),
DefaultLifecycle::default(),
);Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserver additional space for additional entries.
Note that this is counted in entries, and is not weighted.
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
Check if a key exist in the cache.
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<RefMut<'_, Key, Val, We, B, L>>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<RefMut<'_, Key, Val, We, B, L>>
Fetches an item from the cache.
Note: Leaking the returned RefMut might cause cache weight tracking to be inaccurate.
Sourcepub fn peek<Q>(&self, key: &Q) -> Option<&Val>
pub fn peek<Q>(&self, key: &Q) -> Option<&Val>
Peeks an item from the cache. Contrary to gets, peeks don’t alter the key “hotness”.
Sourcepub fn peek_mut<Q>(&mut self, key: &Q) -> Option<RefMut<'_, Key, Val, We, B, L>>
pub fn peek_mut<Q>(&mut self, key: &Q) -> Option<RefMut<'_, Key, Val, We, B, L>>
Peeks an item from the cache. Contrary to gets, peeks don’t alter the key “hotness”.
Note: Leaking the returned RefMut might cause cache weight tracking to be inaccurate.
Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<(Key, Val)>
pub fn remove<Q>(&mut self, key: &Q) -> Option<(Key, Val)>
Remove an item from the cache whose key is key.
Returns the removed entry, if any.
Sourcepub fn remove_if<Q, F>(&mut self, key: &Q, f: F) -> Option<(Key, Val)>
pub fn remove_if<Q, F>(&mut self, key: &Q, f: F) -> Option<(Key, Val)>
Remove an item from the cache whose key is key if f(&value) returns true for that entry.
Compared to peek and remove, this method is more efficient as it requires only 1 lookup.
Returns the removed entry, if any.
Sourcepub fn replace(
&mut self,
key: Key,
value: Val,
soft: bool,
) -> Result<(), (Key, Val)>
pub fn replace( &mut self, key: Key, value: Val, soft: bool, ) -> Result<(), (Key, Val)>
Replaces an item in the cache, but only if it already exists.
If soft is set, the replace operation won’t affect the “hotness” of the key,
even if the value is replaced.
Returns Ok if the entry was admitted and Err(_) if it wasn’t.
Sourcepub fn replace_with_lifecycle(
&mut self,
key: Key,
value: Val,
soft: bool,
) -> Result<L::RequestState, (Key, Val)>
pub fn replace_with_lifecycle( &mut self, key: Key, value: Val, soft: bool, ) -> Result<L::RequestState, (Key, Val)>
Replaces an item in the cache, but only if it already exists.
If soft is set, the replace operation won’t affect the “hotness” of the key,
even if the value is replaced.
Returns Ok if the entry was admitted and Err(_) if it wasn’t.
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the items specified by the predicate.
In other words, remove all items for which f(&key, &value) returns false. The
elements are visited in arbitrary order.
Sourcepub fn get_or_insert_with<Q, E>(
&mut self,
key: &Q,
with: impl FnOnce() -> Result<Val, E>,
) -> Result<Option<&Val>, E>
pub fn get_or_insert_with<Q, E>( &mut self, key: &Q, with: impl FnOnce() -> Result<Val, E>, ) -> Result<Option<&Val>, E>
Gets or inserts an item in the cache with key key.
Returns a reference to the inserted value if it was admitted to the cache.
See also get_ref_or_guard.
Sourcepub fn get_mut_or_insert_with<'a, Q, E>(
&'a mut self,
key: &Q,
with: impl FnOnce() -> Result<Val, E>,
) -> Result<Option<RefMut<'a, Key, Val, We, B, L>>, E>
pub fn get_mut_or_insert_with<'a, Q, E>( &'a mut self, key: &Q, with: impl FnOnce() -> Result<Val, E>, ) -> Result<Option<RefMut<'a, Key, Val, We, B, L>>, E>
Gets or inserts an item in the cache with key key.
Returns a mutable reference to the inserted value if it was admitted to the cache.
See also get_mut_or_guard.
Sourcepub fn get_ref_or_guard<Q>(
&mut self,
key: &Q,
) -> Result<&Val, Guard<'_, Key, Val, We, B, L>>
pub fn get_ref_or_guard<Q>( &mut self, key: &Q, ) -> Result<&Val, Guard<'_, Key, Val, We, B, L>>
Gets an item from the cache with key key .
If the corresponding value isn’t present in the cache, this functions returns a guard
that can be used to insert the value once it’s computed.
Sourcepub fn get_mut_or_guard<'a, Q>(
&'a mut self,
key: &Q,
) -> Result<Option<RefMut<'a, Key, Val, We, B, L>>, Guard<'a, Key, Val, We, B, L>>
pub fn get_mut_or_guard<'a, Q>( &'a mut self, key: &Q, ) -> Result<Option<RefMut<'a, Key, Val, We, B, L>>, Guard<'a, Key, Val, We, B, L>>
Gets an item from the cache with key key .
If the corresponding value isn’t present in the cache, this functions returns a guard
that can be used to insert the value once it’s computed.
Note: Leaking the returned RefMut might cause cache weight tracking to be inaccurate.
Sourcepub fn insert_with_lifecycle(&mut self, key: Key, value: Val) -> L::RequestState
pub fn insert_with_lifecycle(&mut self, key: Key, value: Val) -> L::RequestState
Inserts an item in the cache with key key.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&Key, &Val)> + '_
pub fn iter(&self) -> impl Iterator<Item = (&Key, &Val)> + '_
Iterator for the items in the cache
Sourcepub fn drain(&mut self) -> impl Iterator<Item = (Key, Val)> + '_
pub fn drain(&mut self) -> impl Iterator<Item = (Key, Val)> + '_
Drain all items from the cache
The cache will be emptied even if the returned iterator isn’t fully consumed.
Sourcepub fn set_capacity(&mut self, new_weight_capacity: u64)
pub fn set_capacity(&mut self, new_weight_capacity: u64)
Sets the cache to a new weight capacity.
If the new capacity is smaller than the current weight, items will be evicted to bring the cache within the new limit.
Sourcepub fn memory_used(&self) -> MemoryUsed
pub fn memory_used(&self) -> MemoryUsed
Get total memory used by cache data structures
It should be noted that if cache key or value is some type like Vec<T>,
the memory allocated in the heap will not be counted.