Struct quick_cache::sync::Cache
source · pub struct Cache<Key, Val, We = UnitWeighter, B = DefaultHashBuilder>(_);
Expand description
A concurrent cache.
Value
Cache values are cloned when fetched. Users should wrap their values with Arc<_>
if necessary to avoid expensive clone operations. If interior mutability is required
Arc<Mutex<_>>
or Arc<RwLock<_>>
can also be used.
Thread Safety and Concurrency
The cache instance can wrapped with an Arc
(or equivalent) and shared between threads.
All methods are accessible via non-mut references so no further synchronization (e.g. Mutex) is needed.
Implementations§
source§impl<Key: Eq + Hash, Val: Clone> Cache<Key, Val, UnitWeighter, DefaultHashBuilder>
impl<Key: Eq + Hash, Val: Clone> Cache<Key, Val, UnitWeighter, DefaultHashBuilder>
source§impl<Key: Eq + Hash, Val: Clone, We: Weighter<Key, (), Val> + Clone> Cache<Key, Val, We, DefaultHashBuilder>
impl<Key: Eq + Hash, Val: Clone, We: Weighter<Key, (), Val> + Clone> Cache<Key, Val, We, DefaultHashBuilder>
pub fn with_weighter( estimated_items_capacity: usize, weight_capacity: u64, weighter: We ) -> Self
source§impl<Key: Eq + Hash, Val: Clone, We: Weighter<Key, (), Val> + Clone, B: BuildHasher + Clone> Cache<Key, Val, We, B>
impl<Key: Eq + Hash, Val: Clone, We: Weighter<Key, (), Val> + Clone, B: BuildHasher + Clone> Cache<Key, Val, We, B>
sourcepub fn with(
estimated_items_capacity: usize,
weight_capacity: u64,
weighter: We,
hash_builder: B
) -> Self
pub fn with( estimated_items_capacity: usize, weight_capacity: u64, weighter: We, hash_builder: B ) -> 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
.
pub fn with_options(options: Options, weighter: We, hash_builder: B) -> Self
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 get<Q>(&self, key: &Q) -> Option<Val>where
Key: Borrow<Q>,
Q: Eq + Hash + ?Sized,
pub fn get<Q>(&self, key: &Q) -> Option<Val>where Key: Borrow<Q>, Q: Eq + Hash + ?Sized,
Fetches an item from the cache.
Callers should prefer get_mut
whenever possible as it’s more efficient.
sourcepub fn peek<Q>(&self, key: &Q) -> Option<Val>where
Key: Borrow<Q>,
Q: Eq + Hash + ?Sized,
pub fn peek<Q>(&self, key: &Q) -> Option<Val>where Key: Borrow<Q>, Q: Eq + Hash + ?Sized,
Peeks an item from the cache. Contrary to gets, peeks don’t alter the key “hotness”.