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§
Sourcefn get<Q>(&mut self, k: &Q) -> Option<&V>
fn get<Q>(&mut self, k: &Q) -> Option<&V>
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).
Sourcefn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
Retrieve a cached value with mutable access. Delegates to cache_get_mut.
Sourcefn set(&mut self, k: K, v: V) -> Option<V>
fn set(&mut self, k: K, v: V) -> Option<V>
Insert a key-value pair and return the previous value. Delegates to cache_set.
Sourcefn try_set(&mut self, k: K, v: V) -> Result<Option<V>, Self::Error>
fn try_set(&mut self, k: K, v: V) -> Result<Option<V>, Self::Error>
Fallible insert. Delegates to cache_try_set.
Sourcefn get_or_set_with<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &V
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.
Sourcefn get_or_set_with_mut<F: FnOnce() -> V>(&mut self, key: K, f: F) -> &mut V
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.
Sourcefn 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<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.
Sourcefn try_get_or_set_with_mut<F: FnOnce() -> Result<V, E>, E>(
&mut self,
k: K,
f: F,
) -> Result<&mut 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>
Get or insert a key-value pair with error handling, returning a mutable reference.
Delegates to cache_try_get_or_set_with_mut.
Sourcefn remove<Q>(&mut self, k: &Q) -> Option<V>
fn remove<Q>(&mut self, k: &Q) -> Option<V>
Remove a cached value. Delegates to cache_remove.
Sourcefn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
Remove a cached entry, returning the stored key and value. Delegates to
cache_remove_entry.
Sourcefn delete<Q>(&mut self, k: &Q) -> bool
fn delete<Q>(&mut self, k: &Q) -> bool
Delete a cached entry without returning it. Returns true if an entry was
physically deleted (including expired entries). Delegates to
cache_delete.
Sourcefn contains<Q>(&mut self, k: &Q) -> bool
fn contains<Q>(&mut self, k: &Q) -> bool
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.
Sourcefn clear(&mut self)
fn clear(&mut self)
Remove all entries, keeping allocated memory for reuse. Delegates to
cache_clear.
Sourcefn reset(&mut self)
fn reset(&mut self)
Remove all entries and reset metrics to zero. Delegates to
cache_reset.
Sourcefn len(&self) -> usize
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.
Sourcefn is_empty(&self) -> bool
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.
Sourcefn hits(&self) -> Option<u64>
fn hits(&self) -> Option<u64>
Return the number of cache hits, if tracked. Delegates to
cache_hits.
Sourcefn misses(&self) -> Option<u64>
fn misses(&self) -> Option<u64>
Return the number of cache misses, if tracked. Delegates to
cache_misses.
Sourcefn capacity(&self) -> Option<usize>
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.
Sourcefn evictions(&self) -> Option<u64>
fn evictions(&self) -> Option<u64>
Return the number of evictions, if tracked. Delegates to
cache_evictions.
Sourcefn metrics(&self) -> CacheMetrics
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".