Skip to main content

CacheManager

Struct CacheManager 

Source
pub struct CacheManager<S: CacheStore> { /* private fields */ }
Expand description

High-level cache manager with type-safe operations.

Implementations§

Source§

impl<S: CacheStore> CacheManager<S>

Source

pub fn new(store: S) -> Self

Create a new cache manager.

Source

pub async fn get<T: DeserializeOwned>( &self, key: &str, ) -> CacheResult<Option<T>>

Get a typed value from the cache.

Source

pub async fn set<T: Serialize>( &self, key: &str, value: &T, ttl: Option<Duration>, ) -> CacheResult<()>

Set a typed value in the cache.

Source

pub async fn get_or_set<T, F, Fut>( &self, key: &str, ttl: Option<Duration>, factory: F, ) -> CacheResult<T>
where T: Serialize + DeserializeOwned, F: FnOnce() -> Fut, Fut: Future<Output = 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.

Source

pub async fn delete(&self, key: &str) -> CacheResult<()>

Delete a key from the cache.

Source

pub async fn exists(&self, key: &str) -> CacheResult<bool>

Check if a key exists.

Source

pub async fn clear(&self) -> CacheResult<()>

Clear all keys.

Source

pub async fn ttl(&self, key: &str) -> CacheResult<Option<Duration>>

Get the TTL of a key.

Source

pub async fn expire(&self, key: &str, ttl: Duration) -> CacheResult<()>

Set the expiration of a key.

Source

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