pub struct Cache<S: CacheStore> { /* private fields */ }Expand description
A flexible caching system with pluggable storage strategies.
§Features
- Works with any
CacheStoreimplementation - Structured cache keys with wildcard invalidation
- Negative caching (caches
Noneresults) - Configurable expiration per operation
- Local stampede protection (prevents duplicate fetches within same process)
- Cost hints embedded in keys for tier-aware stores
Implementations§
Source§impl<S: CacheStore> Cache<S>
impl<S: CacheStore> Cache<S>
Sourcepub fn key<I, T>(&self, parts: I) -> CacheQuery<'_, S>
pub fn key<I, T>(&self, parts: I) -> CacheQuery<'_, S>
Create a cache query from key parts.
Parts can be:
- String literals:
"users","123" KeyPart::hash(unbounded_value)for URLs, file paths, etc.KeyPart::hash_with_prefix("prefix", value)for debugging
Use "*" as a segment for wildcard matching during invalidation.
An empty array [] maps to __root__.
§Example
ⓘ
// Simple key from literals
cache.key(["users", "123"]).get(...).await?;
// Key with hashed segment for unbounded input
cache.key(["convertPCMtoMP3", KeyPart::hash(url)]).get(...).await?;
// Wildcard invalidation
cache.key(["users", "*"]).invalidate().await?;Sourcepub async fn stats(&self) -> StoreStats
pub async fn stats(&self) -> StoreStats
Get cache statistics
Sourcepub fn default_ttl(&self) -> Duration
pub fn default_ttl(&self) -> Duration
Get the default TTL
Sourcepub fn namespace(&self, namespace: impl Into<String>) -> NamespacedCache<'_, S>
pub fn namespace(&self, namespace: impl Into<String>) -> NamespacedCache<'_, S>
Create a namespaced view of this cache.
All keys created through the namespaced cache will be prefixed with the namespace. This is useful for isolating cache entries by domain or feature.
§Example
ⓘ
let audio_cache = cache.namespace("audio");
// Key becomes "audio:convert:123"
audio_cache.key(["convert", "123"]).get(...).await?;
// Invalidate all audio cache entries
audio_cache.key(["*"]).invalidate().await?;Sourcepub async fn get_cached<T, F, Fut>(
&self,
id: T::Id,
fetch: F,
) -> Result<Option<T>>
pub async fn get_cached<T, F, Fut>( &self, id: T::Id, fetch: F, ) -> Result<Option<T>>
Get a cached entity by ID, using the entity’s Cached trait implementation.
This is a convenience method that constructs the cache key from the entity’s
CACHE_PREFIX and the provided ID, and uses the entity’s cache_ttl().
§Example
ⓘ
// Entity implements Cached trait
let member: Option<Member> = cache
.get_cached::<Member, _>(member_id, || async {
Member::find_by_id(member_id, db).await.ok()
})
.await?;Trait Implementations§
Auto Trait Implementations§
impl<S> Freeze for Cache<S>
impl<S> !RefUnwindSafe for Cache<S>
impl<S> Send for Cache<S>
impl<S> Sync for Cache<S>
impl<S> Unpin for Cache<S>
impl<S> !UnwindSafe for Cache<S>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more