pub struct PortableCache<K, V> { /* private fields */ }Expand description
Portable, runtime-agnostic in-process cache.
- Max capacity with FIFO eviction
- TTL (time-to-live) and TTI (time-to-idle)
- Single-flight
get_with/get_with_by_ref
Implementations§
Source§impl<K, V> PortableCache<K, V>
impl<K, V> PortableCache<K, V>
pub fn builder() -> PortableCacheBuilder<K, V>
pub async fn get<Q>(&self, key: &Q) -> Option<V>
pub async fn insert(&self, key: K, value: V)
Sourcepub async fn upsert_with_by_ref<Q, R>(
&self,
key: &Q,
update: impl FnOnce(Option<&V>) -> (Option<V>, R),
) -> R
pub async fn upsert_with_by_ref<Q, R>( &self, key: &Q, update: impl FnOnce(Option<&V>) -> (Option<V>, R), ) -> R
Atomically derive and optionally store a value from the current entry.
The closure runs synchronously under the cache’s existing write lock.
Returning None leaves an existing value unchanged and keeps a missing
key absent. The key is cloned only when the operation inserts a new
entry.
pub async fn remove<Q>(&self, key: &Q) -> Option<V>
pub async fn invalidate<Q>(&self, key: &Q)
Sourcepub async fn clear(&self)
pub async fn clear(&self)
Reliably remove all entries, awaiting the write lock. Prefer this in
async contexts over invalidate_all, whose
best-effort sync spin can skip the clear under sustained write
contention.
Sourcepub fn invalidate_all(&self)
pub fn invalidate_all(&self)
Sync invalidate. Spins briefly if the lock is held; kept for moka API
parity. In async contexts prefer clear, which can’t
silently skip the clear.
pub fn entry_count(&self) -> u64
Sourcepub async fn snapshot_entries(&self) -> Vec<(Arc<K>, V)>
pub async fn snapshot_entries(&self) -> Vec<(Arc<K>, V)>
Reliable awaited snapshot of (Arc<K>, V) pairs. Prefer this over
iter in async contexts: iter is best-effort (a
try_read spin that yields an empty snapshot under write contention),
which would silently skip entries an invalidation pass must see.
Sourcepub async fn fold_entries<A>(&self, init: A, f: impl FnMut(A, &K, &V) -> A) -> A
pub async fn fold_entries<A>(&self, init: A, f: impl FnMut(A, &K, &V) -> A) -> A
Reliable awaited fold over (&K, &V). Unlike the snapshot walks this
clones nothing — memory reports must not themselves allocate in
proportion to the cache — and unlike iter it cannot
degrade to an empty walk under write contention.
Sourcepub async fn memory_stats(
&self,
per_entry: impl FnMut(&K, &V) -> usize,
) -> CollectionStats
pub async fn memory_stats( &self, per_entry: impl FnMut(&K, &V) -> usize, ) -> CollectionStats
Entry count plus estimated retained bytes, summing per_entry under a
single awaited read guard so the pair is mutually consistent (and never
the empty best-effort snapshot iter can degrade to).
Sourcepub fn iter(&self) -> IntoIter<(Arc<K>, V)> ⓘ
pub fn iter(&self) -> IntoIter<(Arc<K>, V)> ⓘ
Eager snapshot iterator over (Arc<K>, V): snapshot, not lazy. Includes
expired-but-not-yet-evicted entries (consistent with entry_count).
Best-effort (try_read spin); use snapshot_entries
when missing an entry would be a correctness bug. Caller must not .await
with the writer guard held from the same task — would deadlock on
single-threaded runtimes.
Sourcepub async fn get_with<F>(&self, key: K, init: F) -> V
pub async fn get_with<F>(&self, key: K, init: F) -> V
Get or insert (single-flight). Takes key by value.
The initializer is boxed only on cache miss — a hit returns without
allocating. The boxing keeps the slow path monomorphic per <K, V>
instead of per call-site future type. A racer that loses the
double-check inside get_with_slow pays one
spare box; deferring the box past the double-check would drag the
future type parameter back into the slow path, re-stamping it per
call site.
Sourcepub async fn get_with_by_ref<Q, F>(&self, key: &Q, init: F) -> V
pub async fn get_with_by_ref<Q, F>(&self, key: &Q, init: F) -> V
Get or insert (single-flight). Takes key by reference — only allocates the owned key (and the boxed initializer) on cache miss.
Sourcepub async fn run_pending_tasks(&self)
pub async fn run_pending_tasks(&self)
Evict expired entries and clean up unused init locks.
Trait Implementations§
Auto Trait Implementations§
impl<K, V> !RefUnwindSafe for PortableCache<K, V>
impl<K, V> !UnwindSafe for PortableCache<K, V>
impl<K, V> Freeze for PortableCache<K, V>
impl<K, V> Send for PortableCache<K, V>
impl<K, V> Sync for PortableCache<K, V>
impl<K, V> Unpin for PortableCache<K, V>
impl<K, V> UnsafeUnpin for PortableCache<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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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
impl<T> ErasedDestructor for Twhere
T: 'static,
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