pub struct FusionCache<TKey: Hash + Eq + Send + Sync + Clone + Serialize + DeserializeOwned + 'static, TValue: Clone + Send + Sync + Serialize + DeserializeOwned + 'static> { /* private fields */ }
Expand description
The main struct of the fusioncache-rs library.
This struct is used to create a cache that can be used to store and retrieve values.
Implementations§
Source§impl<TKey: Hash + Eq + Send + Sync + Clone + Serialize + DeserializeOwned + Debug + 'static, TValue: Clone + Send + Sync + Serialize + DeserializeOwned + Debug + 'static> FusionCache<TKey, TValue>
impl<TKey: Hash + Eq + Send + Sync + Clone + Serialize + DeserializeOwned + Debug + 'static, TValue: Clone + Send + Sync + Serialize + DeserializeOwned + Debug + 'static> FusionCache<TKey, TValue>
Sourcepub async fn get_or_set<TError: Clone + Send + Sync + Display + 'static, F: Factory<TKey, TValue, TError>>(
&mut self,
key: TKey,
factory: F,
options: Option<FusionCacheOptions>,
) -> Result<TValue, FusionCacheError>
pub async fn get_or_set<TError: Clone + Send + Sync + Display + 'static, F: Factory<TKey, TValue, TError>>( &mut self, key: TKey, factory: F, options: Option<FusionCacheOptions>, ) -> Result<TValue, FusionCacheError>
Retrieves a value from the cache, or generates it using the provided factory if not found. If the distributed cache is enabled, the value will be retrieved and written to the distributed cache as well.
This method implements a sophisticated cache stampede protection mechanism:
- If the value exists in cache, returns it immediately
- If the value is being generated by another request, waits for and returns that result
- If no request is in flight, generates the value using the factory
§Arguments
key
- The key to look up in the cachefactory
- The factory to use for generating the value if not foundoptions
- The options to use for this specifc entry. If not provided, the default options will be used.
§Returns
Ok(TValue)
- The cached or newly generated valueErr(FusionCacheError)
- If value generation failed or timed out
Sourcepub async fn set(
&mut self,
key: TKey,
value: TValue,
options: Option<FusionCacheOptions>,
)
pub async fn set( &mut self, key: TKey, value: TValue, options: Option<FusionCacheOptions>, )
Directly sets a value in the cache.
§Arguments
key
- The key under which to store the valuevalue
- The value to storeoptions
- The options to use for this specifc entry. If not provided, the default options will be used.
Trait Implementations§
Source§impl<TKey: Clone + Hash + Eq + Send + Sync + Clone + Serialize + DeserializeOwned + 'static, TValue: Clone + Clone + Send + Sync + Serialize + DeserializeOwned + 'static> Clone for FusionCache<TKey, TValue>
impl<TKey: Clone + Hash + Eq + Send + Sync + Clone + Serialize + DeserializeOwned + 'static, TValue: Clone + Clone + Send + Sync + Serialize + DeserializeOwned + 'static> Clone for FusionCache<TKey, TValue>
Source§fn clone(&self) -> FusionCache<TKey, TValue>
fn clone(&self) -> FusionCache<TKey, TValue>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl<TKey, TValue> Freeze for FusionCache<TKey, TValue>
impl<TKey, TValue> !RefUnwindSafe for FusionCache<TKey, TValue>
impl<TKey, TValue> Send for FusionCache<TKey, TValue>
impl<TKey, TValue> Sync for FusionCache<TKey, TValue>
impl<TKey, TValue> Unpin for FusionCache<TKey, TValue>
impl<TKey, TValue> !UnwindSafe for FusionCache<TKey, TValue>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more