Skip to main content

CachedExt

Trait CachedExt 

Source
pub trait CachedExt<K, V>: Cached<K, V> {
Show 21 methods // Required methods fn get<Q>(&mut self, k: &Q) -> Option<&V> where K: Borrow<Q>, Q: Hash + Eq + ?Sized; fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V> where K: Borrow<Q>, Q: Hash + Eq + ?Sized; fn set(&mut self, k: K, v: V) -> Option<V>; fn try_set(&mut self, k: K, v: V) -> Result<Option<V>, Self::Error>; fn get_or_set_with<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &V; fn get_or_set_with_mut<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &mut V; fn try_get_or_set_with<F: FnOnce() -> Result<V, E>, E>( &mut self, k: K, f: F, ) -> Result<&V, E>; fn try_get_or_set_with_mut<F: FnOnce() -> Result<V, E>, E>( &mut self, k: K, f: F, ) -> Result<&mut V, E>; fn remove<Q>(&mut self, k: &Q) -> Option<V> where K: Borrow<Q>, Q: Hash + Eq + ?Sized; fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)> where K: Borrow<Q>, Q: Hash + Eq + ?Sized; fn delete<Q>(&mut self, k: &Q) -> bool where K: Borrow<Q>, Q: Hash + Eq + ?Sized; fn contains<Q>(&mut self, k: &Q) -> bool where K: Borrow<Q>, Q: Hash + Eq + ?Sized; fn clear(&mut self); fn reset(&mut self); fn len(&self) -> usize; fn is_empty(&self) -> bool; fn hits(&self) -> Option<u64>; fn misses(&self) -> Option<u64>; fn capacity(&self) -> Option<usize>; fn evictions(&self) -> Option<u64>; fn metrics(&self) -> CacheMetrics;
}
Expand description

Short-alias extension for Cached stores.

Every type that implements Cached<K, V> automatically implements this trait through a blanket impl. The methods here are ergonomic short names that delegate to the cache_-prefixed core methods on Cached. Import this trait (or use cached::prelude::*;) to bring the short names into scope:

use cached::{CachedExt, UnboundCache};

let mut cache: UnboundCache<String, u32> = UnboundCache::builder().build().unwrap();
cache.set("key".to_string(), 42);
assert_eq!(cache.get("key"), Some(&42u32));
assert!(cache.delete("key"));
assert!(!cache.delete("key"));

When you need a short name that would collide with another trait’s method, or when only Cached is in scope, use the cache_-prefixed form:

use cached::{Cached, UnboundCache};

let mut cache: UnboundCache<String, u32> = UnboundCache::builder().build().unwrap();
cache.cache_set("key".to_string(), 42);
assert_eq!(cache.cache_get("key"), Some(&42u32));

Custom store implementations do not implement this trait directly. Only implement the core Cached trait; this extension is provided automatically via the blanket impl.

Note: shards() and shard_sizes() on the sharded stores are inherent-only - they are sharding-specific introspection not part of the generic cache trait surface.

Required Methods§

Source

fn get<Q>(&mut self, k: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Retrieve a cached value. Delegates to cache_get.

§Mutability

Like cache_get, this takes &mut self (unlike HashMap::get) because some stores update recency or refresh TTL on read. For a &self read where the store supports it, use CachedPeek::cache_peek (non-mutating, no recency/TTL/metrics updates) or CachedRead::cache_get_read (shared-lock read preserving normal read semantics).

Source

fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Retrieve a cached value with mutable access. Delegates to cache_get_mut.

Source

fn set(&mut self, k: K, v: V) -> Option<V>

Insert a key-value pair and return the previous value. Delegates to cache_set.

Source

fn try_set(&mut self, k: K, v: V) -> Result<Option<V>, Self::Error>

Fallible insert. Delegates to cache_try_set.

Source

fn get_or_set_with<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &V

Get or insert a key-value pair. Delegates to cache_get_or_set_with.

Source

fn get_or_set_with_mut<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &mut V

Get or insert a key-value pair, returning a mutable reference. Delegates to cache_get_or_set_with_mut.

Source

fn try_get_or_set_with<F: FnOnce() -> Result<V, E>, E>( &mut self, k: K, f: F, ) -> Result<&V, E>

Get or insert a key-value pair with error handling. Delegates to cache_try_get_or_set_with.

Source

fn try_get_or_set_with_mut<F: FnOnce() -> Result<V, E>, E>( &mut self, k: K, f: F, ) -> Result<&mut V, E>

Get or insert a key-value pair with error handling, returning a mutable reference. Delegates to cache_try_get_or_set_with_mut.

Source

fn remove<Q>(&mut self, k: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Remove a cached value. Delegates to cache_remove.

Source

fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Remove a cached entry, returning the stored key and value. Delegates to cache_remove_entry.

Source

fn delete<Q>(&mut self, k: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Delete a cached entry without returning it. Returns true if an entry was physically deleted (including expired entries). Delegates to cache_delete.

Source

fn contains<Q>(&mut self, k: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Return true if the cache contains a value for the given key. Delegates to cache_contains.

The receiver stays &mut self because a third-party store’s implementation may mutate, but the built-in stores’ overrides are peek-based and do not mutate observable state (no hit/miss count, no recency update, no TTL refresh). For a non-mutating borrow-returning lookup use CachedPeek::cache_peek if the store implements it.

Source

fn clear(&mut self)

Remove all entries, keeping allocated memory for reuse. Delegates to cache_clear.

Source

fn reset(&mut self)

Remove all entries and reset metrics to zero. Delegates to cache_reset.

Source

fn len(&self) -> usize

Return the number of entries currently in the cache. Delegates to cache_size.

On lazy-eviction stores this count may include expired-but-not-yet-swept entries. Use evict() (via CacheEvict) before calling len() if you need an accurate count of live entries.

Source

fn is_empty(&self) -> bool

Return true if the cache contains no entries. Delegates to cache_size.

On lazy-eviction stores an expired-but-not-yet-swept entry causes this to return false even when no live entries remain.

Source

fn hits(&self) -> Option<u64>

Return the number of cache hits, if tracked. Delegates to cache_hits.

Source

fn misses(&self) -> Option<u64>

Return the number of cache misses, if tracked. Delegates to cache_misses.

Source

fn capacity(&self) -> Option<usize>

Return the cache’s capacity bound, if it has one. Delegates to cache_capacity.

Note: the always-bounded LRU-family stores (LruCache, LruTtlCache, ExpiringLruCache) also expose an inherent capacity returning a plain usize, which takes call-site priority over this alias on those concrete types; the alias applies in generic Cached-bounded code.

Source

fn evictions(&self) -> Option<u64>

Return the number of evictions, if tracked. Delegates to cache_evictions.

Source

fn metrics(&self) -> CacheMetrics

Return a snapshot of cache metrics.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<K, V, T: Cached<K, V>> CachedExt<K, V> for T