pub struct TypedCache<K, V> { /* private fields */ }Expand description
A cache over K → V backed by either moka or any CacheStore.
The moka path has zero extra overhead — values are stored in-process
without any serialisation. The custom-store path serialises values with
serde_json and keys via Display.
Methods mirror moka’s API so call sites need no changes.
Implementations§
Source§impl<K, V> TypedCache<K, V>
impl<K, V> TypedCache<K, V>
Source§impl<K, V> TypedCache<K, V>
impl<K, V> TypedCache<K, V>
Sourcepub fn from_store(
store: Arc<dyn CacheStore>,
namespace: &'static str,
ttl: Option<Duration>,
) -> Self
pub fn from_store( store: Arc<dyn CacheStore>, namespace: &'static str, ttl: Option<Duration>, ) -> Self
Create a cache backed by a custom store.
namespace— unique string for this cache (e.g.,"group")ttl— forwarded toCacheStore::set;Nonemeans no expiry
Sourcepub async fn get<Q>(&self, key: &Q) -> Option<V>
pub async fn get<Q>(&self, key: &Q) -> Option<V>
Look up a value.
Accepts borrowed keys (&str for String, &Jid for Jid, etc.)
following the same pattern as std::collections::HashMap::get.
Cache misses and deserialisation failures both return None; the
caller re-fetches from the authoritative source.
Sourcepub async fn insert(&self, key: K, value: V)
pub async fn insert(&self, key: K, value: V)
Insert or update a value (takes ownership of key and value).
Sourcepub async fn invalidate<Q>(&self, key: &Q)
pub async fn invalidate<Q>(&self, key: &Q)
Remove a single key.
Accepts borrowed keys following the same pattern as get.
Sourcepub fn invalidate_all(&self)
pub fn invalidate_all(&self)
Remove all entries.
For the moka backend this is synchronous (matching moka’s API).
For the custom backend this spawns a fire-and-forget task via
tokio::runtime::Handle::try_current (requires tokio-runtime
feature) to avoid panicking if called outside a Tokio runtime.
Without tokio-runtime, the clear is skipped with a warning.
Sourcepub async fn run_pending_tasks(&self)
pub async fn run_pending_tasks(&self)
Run any pending internal housekeeping tasks (moka only).
For the moka backend this ensures all writes have been applied before
calling entry_count, which can otherwise lag.
For custom backends this is a no-op.
Sourcepub fn entry_count(&self) -> u64
pub fn entry_count(&self) -> u64
Approximate entry count (sync). Returns 0 for custom backends.
For diagnostics that need custom backend counts, use
entry_count_async instead.
Sourcepub async fn entry_count_async(&self) -> u64
pub async fn entry_count_async(&self) -> u64
Approximate entry count, delegating to the custom backend if available.
Auto Trait Implementations§
impl<K, V> Freeze for TypedCache<K, V>
impl<K, V> !RefUnwindSafe for TypedCache<K, V>
impl<K, V> Send for TypedCache<K, V>
impl<K, V> Sync for TypedCache<K, V>
impl<K, V> Unpin for TypedCache<K, V>
impl<K, V> UnsafeUnpin for TypedCache<K, V>
impl<K, V> !UnwindSafe for TypedCache<K, V>
Blanket Implementations§
Source§impl<T> AggregateExpressionMethods for T
impl<T> AggregateExpressionMethods for T
Source§fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
DISTINCT modifier for aggregate functions Read moreSource§fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
ALL modifier for aggregate functions Read moreSource§fn aggregate_filter<P>(self, f: P) -> Self::Output
fn aggregate_filter<P>(self, f: P) -> Self::Output
Source§fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
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
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expression
fn into_sql<T>(self) -> Self::Expression
self to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
&self to an expression for Diesel’s query builder. Read more