pub struct CacheManager<S: CacheStore> { /* private fields */ }Expand description
High-level cache manager with type-safe operations.
Implementations§
Source§impl<S: CacheStore> CacheManager<S>
impl<S: CacheStore> CacheManager<S>
Sourcepub async fn get<T: DeserializeOwned>(
&self,
key: &str,
) -> CacheResult<Option<T>>
pub async fn get<T: DeserializeOwned>( &self, key: &str, ) -> CacheResult<Option<T>>
Get a typed value from the cache.
Sourcepub async fn set<T: Serialize>(
&self,
key: &str,
value: &T,
ttl: Option<Duration>,
) -> CacheResult<()>
pub async fn set<T: Serialize>( &self, key: &str, value: &T, ttl: Option<Duration>, ) -> CacheResult<()>
Set a typed value in the cache.
Sourcepub async fn get_or_set<T, F, Fut>(
&self,
key: &str,
ttl: Option<Duration>,
factory: F,
) -> CacheResult<T>
pub async fn get_or_set<T, F, Fut>( &self, key: &str, ttl: Option<Duration>, factory: F, ) -> CacheResult<T>
Get or set a value using a factory function.
If the key exists, returns the cached value. If not, calls the factory function, caches the result, and returns it.
§Single-flight
Concurrent misses on the same key are coalesced: only one caller runs
factory() while the others wait and then read the value it cached.
This prevents a cache-miss stampede (many simultaneous DB loads) for hot
keys. The returned value is identical to the non-coalesced behavior.
Sourcepub async fn delete(&self, key: &str) -> CacheResult<()>
pub async fn delete(&self, key: &str) -> CacheResult<()>
Delete a key from the cache.
Sourcepub async fn exists(&self, key: &str) -> CacheResult<bool>
pub async fn exists(&self, key: &str) -> CacheResult<bool>
Check if a key exists.
Sourcepub async fn clear(&self) -> CacheResult<()>
pub async fn clear(&self) -> CacheResult<()>
Clear all keys.
Sourcepub async fn expire(&self, key: &str, ttl: Duration) -> CacheResult<()>
pub async fn expire(&self, key: &str, ttl: Duration) -> CacheResult<()>
Set the expiration of a key.
Sourcepub fn namespace(&self, prefix: &str) -> NamespacedCache<S>
pub fn namespace(&self, prefix: &str) -> NamespacedCache<S>
Create a namespaced cache manager.
Auto Trait Implementations§
impl<S> !Freeze for CacheManager<S>
impl<S> RefUnwindSafe for CacheManager<S>where
S: RefUnwindSafe,
impl<S> Send for CacheManager<S>
impl<S> Sync for CacheManager<S>
impl<S> Unpin for CacheManager<S>
impl<S> UnsafeUnpin for CacheManager<S>
impl<S> UnwindSafe for CacheManager<S>where
S: RefUnwindSafe,
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