Skip to main content

LocalCache

Struct LocalCache 

Source
pub struct LocalCache<T> { /* private fields */ }
Expand description

A cache that can store only a single type.

It is internally reference counted, so cloning it is cheap.

Implementations§

Source§

impl<T> LocalCache<T>

Source

pub fn new() -> Self

Constructs a new cache with no entries.

Source

pub fn is_cached(&self, path: impl AsRef<str>) -> bool

Checks if a value exists in the cache.

Source

pub async fn is_cached_async(&self, path: impl AsRef<str>) -> bool

Checks if a value exists in the cache. Waiting is done asynchronously.

Source

pub fn get(&self, path: impl AsRef<str>) -> Option<Arc<T>>

Obtains a value from the cache, if it exists.

Source

pub async fn get_async(&self, path: impl AsRef<str>) -> Option<Arc<T>>

Obtains a value from the cache, if it exists. Waiting is done asynchronously.

Source

pub fn get_or(&self, path: impl AsRef<str>, default: Arc<T>) -> Arc<T>

Obtains a value from the cache, or adds default if it does not exist.

Source

pub async fn get_or_async( &self, path: impl AsRef<str>, default: Arc<T>, ) -> Arc<T>

Obtains a value from the cache, or adds default if it does not exist. Waiting is done asynchronously.

Source

pub fn get_or_else<F: FnOnce() -> Arc<T>>( &self, path: impl AsRef<str>, f: F, ) -> Arc<T>

Obtains a value from the cache, or loads it from a closure if it does not exist.

Source

pub async fn get_or_else_async<F: AsyncFnOnce() -> Arc<T>>( &self, path: impl AsRef<str>, f: F, ) -> Arc<T>

Similar to get_or_else, but for async closures.

Unlike the non-async variants, this not an atomic operation. Your closure may be run more than once, but only the first run will write to the cache.

Source

pub fn get_or_else_try<F>(&self, path: impl AsRef<str>, f: F) -> Option<Arc<T>>
where F: FnOnce() -> Option<Arc<T>>,

Similar to get_or_else, but allows the closure to return None, in which case the value is not added to the cache and None is returned.

Source

pub async fn get_or_else_try_async<F: AsyncFnOnce() -> Option<Arc<T>>>( &self, path: impl AsRef<str>, f: F, ) -> Option<Arc<T>>

Similar to get_or_else_async, but allows the closure to return None, in which case the value is not added to the cache and None is returned.

Unlike the non-async variants, this not an atomic operation. Your closure may be run more than once, but only the first run will write to the cache.

Source

pub fn uncache(&self, path: impl AsRef<str>)

Removes a file from the cache, if it exists.

Source

pub fn clear(&self)

Removes all entries from the cache.

Trait Implementations§

Source§

impl<T: Clone> Clone for LocalCache<T>

Source§

fn clone(&self) -> LocalCache<T>

Returns a duplicate 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<T> Default for LocalCache<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for LocalCache<T>

§

impl<T> RefUnwindSafe for LocalCache<T>

§

impl<T> Send for LocalCache<T>
where T: Sync + Send,

§

impl<T> Sync for LocalCache<T>
where T: Sync + Send,

§

impl<T> Unpin for LocalCache<T>

§

impl<T> UnsafeUnpin for LocalCache<T>

§

impl<T> UnwindSafe for LocalCache<T>

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.