Struct cache_loader_async::cache_api::LoadingCache [−][src]
pub struct LoadingCache<K, V, E: Debug> { /* fields omitted */ }
Implementations
Creates a new instance of a LoadingCache with the default HashMapBacking
Arguments
loader
- A function which returns a Future<Output=Option>
Return Value
This method returns a tuple, with: 0 - The instance of the LoadingCache 1 - The CacheHandle which is a JoinHandle<()> and represents the task which operates the cache
Examples
use cache_loader_async::cache_api::LoadingCache;
use std::collections::HashMap;
async fn example() {
let static_db: HashMap<String, u32> =
vec![("foo".into(), 32), ("bar".into(), 64)]
.into_iter()
.collect();
let (cache, _) = LoadingCache::new(move |key: String| {
let db_clone = static_db.clone();
async move {
db_clone.get(&key).cloned().ok_or(1)
}
});
let result = cache.get("foo".to_owned()).await.unwrap();
assert_eq!(result, 32);
}
pub fn with_backing<T, F, B>(
backing: B,
loader: T
) -> (LoadingCache<K, V, E>, CacheHandle) where
F: Future<Output = Result<V, E>> + Sized + Send + 'static,
T: Fn(K) -> F + Send + 'static,
B: CacheBacking<K, CacheEntry<V, E>> + Send + 'static,
pub fn with_backing<T, F, B>(
backing: B,
loader: T
) -> (LoadingCache<K, V, E>, CacheHandle) where
F: Future<Output = Result<V, E>> + Sized + Send + 'static,
T: Fn(K) -> F + Send + 'static,
B: CacheBacking<K, CacheEntry<V, E>> + Send + 'static,
Creates a new instance of a LoadingCache with a custom CacheBacking
Arguments
backing
- The custom backing which the cache should useloader
- A function which returns a Future<Output=Option>
Return Value
This method returns a tuple, with: 0 - The instance of the LoadingCache 1 - The CacheHandle which is a JoinHandle<()> and represents the task which operates the cache
Examples
use cache_loader_async::cache_api::LoadingCache;
use std::collections::HashMap;
use cache_loader_async::backing::HashMapBacking;
async fn example() {
let static_db: HashMap<String, u32> =
vec![("foo".into(), 32), ("bar".into(), 64)]
.into_iter()
.collect();
let (cache, _) = LoadingCache::with_backing(
HashMapBacking::new(), // this is the default implementation of `new`
move |key: String| {
let db_clone = static_db.clone();
async move {
db_clone.get(&key).cloned().ok_or(1)
}
}
);
let result = cache.get("foo".to_owned()).await.unwrap();
assert_eq!(result, 32);
}
Retrieves or loads the value for specified key from either cache or loader function
Arguments
key
- The key which should be loaded
Return Value
Returns a Result with: Ok - Value of type V Err - Error of type CacheLoadingError
Retrieves or loads the value for specified key from either cache or loader function with meta information, i.e. if the key was loaded from cache or from the loader function
Arguments
key
- The key which should be loaded
Return Value
Returns a Result with:
Ok - Value of type ResultMeta
Sets the value for specified key and bypasses eventual currently ongoing loads If a key has been set programmatically, eventual concurrent loads will not change the value of the key.
Arguments
key
- The key which should be loaded
Return Value
Returns a Result with: Ok - Previous value of type V wrapped in an Option depending whether there was a previous value Err - Error of type CacheLoadingError
Loads the value for the specified key from the cache and returns None if not present
Arguments
key
- The key which should be loaded
Return Value
Returns a Result with:
Ok - Value of type Option
Checks whether a specific value is mapped for the given key
Arguments
key
- The key which should be checked
Return Value
Returns a Result with: Ok - bool Err - Error of type CacheLoadingError
Removes a specific key-value mapping from the cache and returns the previous result if there was any or None
Arguments
key
- The key which should be evicted
Return Value
Returns a Result with:
Ok - Value of type Option
Removes all entries which match the specified predicate
Arguments
predicate
- The predicate to test all entries against
Return Value
Returns a Result with: Ok - Nothing, the removed values are discarded Err - Error of type CacheLoadingError -> the values were not discarded
Updates a key on the cache with the given update function and returns the updated value
If the key is not present yet, it’ll be loaded using the loader function and will be
updated once this loader function completes.
In case the key was manually updated via set
during the loader function the update will
take place on the manually updated value, so user-controlled input takes precedence over
the loader function
Arguments
key
- The key which should be updatedupdate_fn
- AFnOnce(V) -> V
which has the current value as parameter and should return the updated value
Return Value
Returns a Result with: Ok - Value of type V which is the previously mapped value Err - Error of type CacheLoadingError
pub async fn update_if_exists<U>(
&self,
key: K,
update_fn: U
) -> Result<Option<V>, CacheLoadingError<E>> where
U: FnOnce(V) -> V + Send + 'static,
pub async fn update_if_exists<U>(
&self,
key: K,
update_fn: U
) -> Result<Option<V>, CacheLoadingError<E>> where
U: FnOnce(V) -> V + Send + 'static,
Updates a key on the cache with the given update function and returns the updated value if it existed
If the key is not present yet, it’ll be ignored. This also counts for keys in LOADING state.
Arguments
key
- The key which should be updatedupdate_fn
- AFnOnce(V) -> V
which has the current value as parameter and should return the updated value
Return Value
Returns a Result with: Ok - Optional value of type V which is the previously mapped value Err - Error of type CacheLoadingError
pub async fn update_mut<U>(
&self,
key: K,
update_fn: U
) -> Result<V, CacheLoadingError<E>> where
U: FnMut(&mut V) + Send + 'static,
pub async fn update_mut<U>(
&self,
key: K,
update_fn: U
) -> Result<V, CacheLoadingError<E>> where
U: FnMut(&mut V) + Send + 'static,
Updates a key on the cache with the given update function and returns the updated value
If the key is not present yet, it’ll be loaded using the loader function and will be
updated once this loader function completes.
In case the key was manually updated via set
during the loader function the update will
take place on the manually updated value, so user-controlled input takes precedence over
the loader function
Arguments
key
- The key which should be updatedupdate_fn
- AFnMut(&mut V) -> ()
which has the current value as parameter and should update it accordingly
Return Value
Returns a Result with: Ok - Value of type V Err - Error of type CacheLoadingError
pub async fn update_mut_if_exists<U>(
&self,
key: K,
update_fn: U
) -> Result<Option<V>, CacheLoadingError<E>> where
U: FnMut(&mut V) + Send + 'static,
pub async fn update_mut_if_exists<U>(
&self,
key: K,
update_fn: U
) -> Result<Option<V>, CacheLoadingError<E>> where
U: FnMut(&mut V) + Send + 'static,
Updates a key on the cache with the given update function and returns the updated value if it existed
If the key is not present yet, it’ll be ignored. Keys in LOADING state will still be updated as they get available.
Arguments
key
- The key which should be updatedupdate_fn
- AFnMut(&mut V) -> ()
which has the current value as parameter and should update it accordingly
Return Value
Returns a Result with: Ok - Optional value of type V Err - Error of type CacheLoadingError
Trait Implementations
Auto Trait Implementations
impl<K, V, E> !RefUnwindSafe for LoadingCache<K, V, E>
impl<K, V, E> Send for LoadingCache<K, V, E> where
E: Send,
K: Send,
V: Send,
impl<K, V, E> Sync for LoadingCache<K, V, E> where
E: Send,
K: Send,
V: Send,
impl<K, V, E> Unpin for LoadingCache<K, V, E>
impl<K, V, E> !UnwindSafe for LoadingCache<K, V, E>
Blanket Implementations
Mutably borrows from an owned value. Read more