Struct quick_cache::sync::Cache
source · pub struct Cache<Key, Val, We = UnitWeighter, B = DefaultHashBuilder, L = DefaultLifecycle<Key, Val>> { /* private fields */ }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, We: Weighter<Key, Val> + Clone> Cache<Key, Val, We>
impl<Key: Eq + Hash, Val: Clone, We: Weighter<Key, Val> + Clone> Cache<Key, Val, We>
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, L: Lifecycle<Key, Val> + Clone> Cache<Key, Val, We, B, L>
impl<Key: Eq + Hash, Val: Clone, We: Weighter<Key, Val> + Clone, B: BuildHasher + Clone, L: Lifecycle<Key, Val> + Clone> 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::{sync::{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(&self, additional: usize)
pub fn reserve(&self, additional: usize)
Reserver additional space for additional entries.
Note that this is counted in entries, and is not weighted.
sourcepub fn peek<Q>(&self, key: &Q) -> Option<Val>
pub fn peek<Q>(&self, key: &Q) -> Option<Val>
Peeks an item from the cache whose key is key.
Contrary to gets, peeks don’t alter the key “hotness”.
sourcepub fn remove<Q>(&self, key: &Q) -> Option<(Key, Val)>
pub fn remove<Q>(&self, key: &Q) -> Option<(Key, Val)>
Remove an item from the cache whose key is key.
Returns the removed entry, if any.
sourcepub fn replace(
&self,
key: Key,
value: Val,
soft: bool,
) -> Result<(), (Key, Val)>
pub fn replace( &self, key: Key, value: Val, soft: bool, ) -> Result<(), (Key, Val)>
Inserts an item in the cache, but only if an entry with key key already exists.
If soft is set, the replace operation won’t affect the “hotness” of the entry,
even if the value is replaced.
Returns Ok if the entry was admitted and Err(_) if it wasn’t.
sourcepub fn replace_with_lifecycle(
&self,
key: Key,
value: Val,
soft: bool,
) -> Result<L::RequestState, (Key, Val)>
pub fn replace_with_lifecycle( &self, key: Key, value: Val, soft: bool, ) -> Result<L::RequestState, (Key, Val)>
Inserts an item in the cache, but only if an entry with key key already exists.
If soft is set, the replace operation won’t affect the “hotness” of the entry,
even if the value is replaced.
Returns Ok if the entry was admitted and Err(_) if it wasn’t.
sourcepub fn insert_with_lifecycle(&self, key: Key, value: Val) -> L::RequestState
pub fn insert_with_lifecycle(&self, key: Key, value: Val) -> L::RequestState
Inserts an item in the cache with key key.
sourcepub fn get_value_or_guard<'a, Q>(
&'a self,
key: &Q,
timeout: Option<Duration>,
) -> GuardResult<'a, Key, Val, We, B, L>
pub fn get_value_or_guard<'a, Q>( &'a self, key: &Q, timeout: Option<Duration>, ) -> GuardResult<'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.
While the returned guard is alive, other calls with the same key using the
get_value_guard or get_or_insert family of functions will wait until the guard
is dropped or the value is inserted.
A None timeout means waiting forever.
sourcepub fn get_or_insert_with<Q, E>(
&self,
key: &Q,
with: impl FnOnce() -> Result<Val, E>,
) -> Result<Val, E>
pub fn get_or_insert_with<Q, E>( &self, key: &Q, with: impl FnOnce() -> Result<Val, E>, ) -> Result<Val, E>
Gets or inserts an item in the cache with key key.
See also get_value_or_guard and get_value_or_guard_async.
sourcepub async fn get_value_or_guard_async<'a, Q>(
&'a self,
key: &Q,
) -> Result<Val, PlaceholderGuard<'a, Key, Val, We, B, L>>
pub async fn get_value_or_guard_async<'a, Q>( &'a self, key: &Q, ) -> Result<Val, PlaceholderGuard<'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.
While the returned guard is alive, other calls with the same key using the
get_value_guard or get_or_insert family of functions will wait until the guard
is dropped or the value is inserted.