Struct light_cache::cache::LightCache
source · pub struct LightCache<K, V, S = DefaultHashBuilder, P = NoopPolicy> { /* private fields */ }Expand description
A concurrent hashmap that allows for efficient async insertion of values
§LightCache offers two modes for async insertion:
§Cooperative:
Self::get_or_insert and Self::get_or_try_insert only allow one worker task at a time to poll thier futures
and will wake up the other tasks when the value is inserted
§Race:
Self::get_or_insert_race and Self::get_or_try_insert_race allow all worker tasks to poll thier futures at the same time
Implementations§
source§impl<K, V> LightCache<K, V>
impl<K, V> LightCache<K, V>
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
source§impl<K, V, S: BuildHasher, P> LightCache<K, V, S, P>
impl<K, V, S: BuildHasher, P> LightCache<K, V, S, P>
pub fn from_parts(policy: P, hasher: S) -> Self
pub fn from_parts_with_capacity(policy: P, hasher: S, capacity: usize) -> Self
source§impl<K, V, S, P> LightCache<K, V, S, P>
impl<K, V, S, P> LightCache<K, V, S, P>
sourcepub async fn get_or_insert<F, Fut>(&self, key: K, init: F) -> V
pub async fn get_or_insert<F, Fut>(&self, key: K, init: F) -> V
Get or insert the value for the given key
In the case that a key is being inserted by another thread using this method or Self::get_or_try_insert
tasks will cooperativley compute the value and notify the other task when the value is inserted.
If a task fails to insert the value, (via panic or error) another task will take over until theyve all tried.
§Note
If a call to remove is issued between the time of inserting, and waking up tasks, the other tasks will simply see the empty slot and try again
sourcepub async fn get_or_try_insert<F, Fut, Err>(
&self,
key: K,
init: F,
) -> Result<V, Err>
pub async fn get_or_try_insert<F, Fut, Err>( &self, key: K, init: F, ) -> Result<V, Err>
Get or insert the value for the given key, returning an error if the value could not be inserted
See Self::get_or_insert for more information
sourcepub async fn get_or_insert_race<F, Fut>(&self, key: K, init: F) -> V
pub async fn get_or_insert_race<F, Fut>(&self, key: K, init: F) -> V
Get or insert a value into the cache, but instead of waiting for a single caller to finish the insertion (coopertively), any callers of this method will always attempt to poll thier own future
This is safe to use with any other get_or_* method
sourcepub async fn get_or_try_insert_race<F, Fut, E>(
&self,
key: K,
init: F,
) -> Result<V, E>
pub async fn get_or_try_insert_race<F, Fut, E>( &self, key: K, init: F, ) -> Result<V, E>
See Self::get_or_insert_race for more information
sourcepub fn insert(&self, key: K, value: V) -> Option<V>
pub fn insert(&self, key: K, value: V) -> Option<V>
Insert a value directly into the cache
§Warning:
Doing a Self::insert while another task is doing any type of async write (Self::get_or_insert, Self::get_or_try_insert, etc)
will “leak” a wakers entry, which will never be removed unless another get_or_* method is fully completed without being
interrupted by a call to this method.
This is mostly not a big deal as Wakers is small, and this pattern really should be avoided in practice.
sourcepub fn remove(&self, key: &K) -> Option<V>
pub fn remove(&self, key: &K) -> Option<V>
Remove a value from the cache, returning the value if it was present
§Note:
If this is called while another task is trying to Self::get_or_insert or Self::get_or_try_insert,
it will force them to recompute the value and insert it again.
Trait Implementations§
source§impl<K, V, S, P> Clone for LightCache<K, V, S, P>
impl<K, V, S, P> Clone for LightCache<K, V, S, P>
Auto Trait Implementations§
impl<K, V, S, P> Freeze for LightCache<K, V, S, P>
impl<K, V, S, P> RefUnwindSafe for LightCache<K, V, S, P>where
P: RefUnwindSafe,
S: RefUnwindSafe,
impl<K, V, S, P> Send for LightCache<K, V, S, P>
impl<K, V, S, P> Sync for LightCache<K, V, S, P>
impl<K, V, S, P> Unpin for LightCache<K, V, S, P>
impl<K, V, S, P> UnwindSafe for LightCache<K, V, S, P>where
P: RefUnwindSafe,
S: RefUnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)