Struct cache_loader_async::cache_api::LoadingCache [−][src]
pub struct LoadingCache<K, V> { /* fields omitted */ }
Implementations
impl<K: Eq + Hash + Clone + Send + 'static, V: Clone + Sized + Send + 'static> LoadingCache<K, V>
[src]
impl<K: Eq + Hash + Clone + Send + 'static, V: Clone + Sized + Send + 'static> LoadingCache<K, V>
[src]pub fn new<T, F>(loader: T) -> (LoadingCache<K, V>, CacheHandle) where
F: Future<Output = Option<V>> + Sized + Send + 'static,
T: Fn(K) -> F + Send + 'static,
[src]
pub fn new<T, F>(loader: T) -> (LoadingCache<K, V>, CacheHandle) where
F: Future<Output = Option<V>> + Sized + Send + 'static,
T: Fn(K) -> F + Send + 'static,
[src]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() } }); 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>, CacheHandle) where
F: Future<Output = Option<V>> + Sized + Send + 'static,
T: Fn(K) -> F + Send + 'static,
B: CacheBacking<K, CacheEntry<V>> + Send + 'static,
[src]
pub fn with_backing<T, F, B>(
backing: B,
loader: T
) -> (LoadingCache<K, V>, CacheHandle) where
F: Future<Output = Option<V>> + Sized + Send + 'static,
T: Fn(K) -> F + Send + 'static,
B: CacheBacking<K, CacheEntry<V>> + Send + 'static,
[src]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() } } ); let result = cache.get("foo".to_owned()).await.unwrap(); assert_eq!(result, 32); }
pub async fn get(&self, key: K) -> Result<V, CacheLoadingError>
[src]
pub async fn get(&self, key: K) -> Result<V, CacheLoadingError>
[src]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
pub async fn set(
&self,
key: K,
value: V
) -> Result<Option<V>, CacheLoadingError>
[src]
pub async fn set(
&self,
key: K,
value: V
) -> Result<Option<V>, CacheLoadingError>
[src]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
pub async fn get_if_present(
&self,
key: K
) -> Result<Option<V>, CacheLoadingError>
[src]
pub async fn get_if_present(
&self,
key: K
) -> Result<Option<V>, CacheLoadingError>
[src]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
pub async fn exists(&self, key: K) -> Result<bool, CacheLoadingError>
[src]
pub async fn exists(&self, key: K) -> Result<bool, CacheLoadingError>
[src]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
pub async fn remove(&self, key: K) -> Result<Option<V>, CacheLoadingError>
[src]
pub async fn remove(&self, key: K) -> Result<Option<V>, CacheLoadingError>
[src]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
pub async fn update<U>(
&self,
key: K,
update_fn: U
) -> Result<V, CacheLoadingError> where
U: FnOnce(V) -> V + Send + 'static,
[src]
pub async fn update<U>(
&self,
key: K,
update_fn: U
) -> Result<V, CacheLoadingError> where
U: FnOnce(V) -> V + Send + 'static,
[src]Updates a key on the cache with the given update function and returns the previous 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_mut<U>(
&self,
key: K,
update_fn: U
) -> Result<V, CacheLoadingError> where
U: FnMut(&mut V) + Send + 'static,
[src]
&self,
key: K,
update_fn: U
) -> Result<V, CacheLoadingError> where
U: FnMut(&mut V) + Send + 'static,
Trait Implementations
impl<K: Clone, V: Clone> Clone for LoadingCache<K, V>
[src]
impl<K: Clone, V: Clone> Clone for LoadingCache<K, V>
[src]fn clone(&self) -> LoadingCache<K, V>
[src]
fn clone(&self) -> LoadingCache<K, V>
[src]Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]Performs copy-assignment from source
. Read more
Auto Trait Implementations
impl<K, V> !RefUnwindSafe for LoadingCache<K, V>
impl<K, V> Send for LoadingCache<K, V> where
K: Send,
V: Send,
K: Send,
V: Send,
impl<K, V> Sync for LoadingCache<K, V> where
K: Send,
V: Send,
K: Send,
V: Send,
impl<K, V> Unpin for LoadingCache<K, V>
impl<K, V> !UnwindSafe for LoadingCache<K, V>
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn to_owned(&self) -> T
[src]Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)
[src]
pub fn clone_into(&self, target: &mut T)
[src]🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more