Skip to main content

GlobalCache

Struct GlobalCache 

Source
pub struct GlobalCache { /* private fields */ }
Expand description

The shared, global cache, supporting dynamic typing. See the module-level docs for more info.

Implementations§

Source§

impl GlobalCache

Source

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

Checks if a value exists in the global cache, regardless of type.

Source

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

Checks if a value exists in the global cache, regardless of type, asynchronously.

Source

pub fn is_cached_as<T: Any + Send + Sync>(path: impl AsRef<str>) -> bool

Checks if a value exists in the global cache, is initialized, and is of the correct type.

Source

pub async fn is_cached_as_async<T: Any + Send + Sync>( path: impl AsRef<str>, ) -> bool

Checks if a value exists in the global cache, is initialized, and is of the correct type. Waits asynchronously.

Source

pub fn get_dyn(path: impl AsRef<str>) -> Option<Arc<dyn Any + Send + Sync>>

Retrieves a value from the global cache, if it exists, without trying to downcast it.

Source

pub async fn get_dyn_async( path: impl AsRef<str>, ) -> Option<Arc<dyn Any + Send + Sync>>

Retrieves a value from the global cache, if it exists, without trying to downcast it, asynchronously.

Source

pub fn get<T: Any + Send + Sync>(path: impl AsRef<str>) -> Option<Arc<T>>

Retrieves a value from the global cache, if it exists. Also returns None if the cached value is of the wrong type.

If you wish to insert a default value, use get_or instead.

This is not suitable for async.

Source

pub async fn get_async<T: Any + Send + Sync>( path: impl AsRef<str>, ) -> Option<Arc<T>>

Retrieves a value from the global cache, if it exists, asynchronously. Also returns None if the cached value is of the wrong type.

If you wish to insert a default value, use get_or_async instead.

This is not suitable for async.

Source

pub fn get_or<T: Any + Send + Sync>( path: impl AsRef<str>, default: Arc<T>, ) -> Option<Arc<T>>

Retrieves a value from the global cache or adds default if it does not exist. Also returns None if the cached value is of the wrong type.

This eagerly evaluates default even if the value already exists in the cache. It is generally better to use get_or_else or one of its variants instead.

Source

pub async fn get_or_async<T: Any + Send + Sync>( path: impl AsRef<str>, default: Arc<T>, ) -> Option<Arc<T>>

Retrieves a value from the global cache asynchronously or adds default if it does not exist. Also returns None if the cached value is of the wrong type.

This eagerly evaluates default even if the value already exists in the cache. It is generally better to use get_or_else_async or one of its variants instead.

Source

pub fn get_or_else<T: Any + Send + Sync, F: FnOnce() -> Arc<T>>( path: impl AsRef<str>, f: F, ) -> Option<Arc<T>>

Retrieves a value from the global cache or loads it from a closure if it does not exist. Also returns None if the cached value is of the wrong type.

Source

pub async fn get_or_else_async<T: Any + Send + Sync, F: AsyncFnOnce() -> Arc<T>>( path: impl AsRef<str>, f: F, ) -> Option<Arc<T>>

Retrieves a value from the global cache or loads it from an async closure if it does not exist. Also returns None if the cached value is of the wrong type.

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(path: impl AsRef<str>)

Removes a path from the global cache, if it exists.

Any values that have been acquired from the cache will remain valid until they are dropped.

Source

pub fn clear()

Removes all paths from the global cache.

This is dangerous, as it can cause other crates to lose their dependent values. Any values that have been acquired from the cache will remain valid until they are dropped.

If you want a safer alternative, iterate and use uncache.

Source

pub fn namespaced_paths(namespace: impl AsRef<str>) -> HashSet<String>

Returns all paths starting with namespace, stripping the namespace prefix.

This is only a snapshot, and future insertions and removals will not be reflected. Some entries may be in the process of being written, but most functions will wait until they are ready.

Source

pub async fn namespaced_paths_async( namespace: impl AsRef<str>, ) -> HashSet<String>

Returns all paths starting with namespace asynchronously, stripping the namespace prefix.

This is only a snapshot, and future insertions and removals will not be reflected. Some entries may be in the process of being written, but most functions will wait until they are ready.

Auto Trait Implementations§

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> 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, 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.