Struct 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 is cloneable and can be shared across threads, so theres no need to explicitly wrap it in an Arc

§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 their 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 their futures at the same time

Implementations§

Source§

impl<K, V> LightCache<K, V>

Source

pub fn new() -> Self

Source

pub fn with_capacity(capacity: usize) -> Self

Source§

impl<K, V, S: BuildHasher, P> LightCache<K, V, S, P>

Source

pub fn from_parts(policy: P, hasher: S) -> Self

Source

pub fn from_parts_with_capacity(policy: P, hasher: S, capacity: usize) -> Self

Source§

impl<K, V, S, P> LightCache<K, V, S, P>

Source

pub fn len(&self) -> usize

Source§

impl<K, V, S, P> LightCache<K, V, S, P>
where K: Eq + Hash + Copy, V: Clone + Sync, S: BuildHasher, P: Policy<K, V>,

Source

pub async fn get_or_insert<F, Fut>(&self, key: K, init: F) -> V
where F: FnOnce() -> Fut, Fut: Future<Output = 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

Source

pub async fn get_or_try_insert<F, Fut, Err>( &self, key: K, init: F, ) -> Result<V, Err>
where F: FnOnce() -> Fut, Fut: Future<Output = 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

Source

pub async fn get_or_insert_race<F, Fut>(&self, key: K, init: F) -> V
where F: FnOnce() -> Fut, Fut: Future<Output = 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 their own future

This is safe to use with any other get_or_* method

Source

pub async fn get_or_try_insert_race<F, Fut, E>( &self, key: K, init: F, ) -> Result<V, E>
where F: FnOnce() -> Fut, Fut: Future<Output = Result<V, E>>,

See Self::get_or_insert_race for more information

Source

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.

Source

pub fn get(&self, key: &K) -> Option<V>

Try to get a value from the cache

Source

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.

Source

pub fn prune(&self)

Prune the cache of any expired keys

Trait Implementations§

Source§

impl<K, V, S, P> Clone for LightCache<K, V, S, P>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K, V, S, P> NoPolicy<K, V, P> for LightCache<K, V, S, P>
where K: Eq + Hash + Copy, V: Clone + Sync, S: BuildHasher,

Source§

fn get_no_policy(&self, key: &K) -> Option<V>

Source§

fn insert_no_policy(&self, key: K, value: V) -> Option<V>

Source§

fn remove_no_policy(&self, key: &K) -> Option<V>

Source§

fn policy(&self) -> &P

Auto Trait Implementations§

§

impl<K, V, S, P> Freeze for LightCache<K, V, S, P>

§

impl<K, V, S = BuildHasherDefault<AHasher>, P = NoopPolicy> !RefUnwindSafe for LightCache<K, V, S, P>

§

impl<K, V, S, P> Send for LightCache<K, V, S, P>
where P: Sync + Send, S: Sync + Send, K: Send + Sync, V: Send + Sync,

§

impl<K, V, S, P> Sync for LightCache<K, V, S, P>
where P: Sync + Send, S: Sync + Send, K: Send + Sync, V: Send + Sync,

§

impl<K, V, S, P> Unpin for LightCache<K, V, S, P>

§

impl<K, V, S = BuildHasherDefault<AHasher>, P = NoopPolicy> !UnwindSafe for LightCache<K, V, S, P>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.