pub struct Cache<K, V> {
inner: Cache<K, V>,
capacity: u64,
size_registration: Option<CacheSizeRegistration>,
metrics: Option<Arc<CacheMetrics>>,
}Expand description
A weighted memory cache with backend-provided asynchronous load deduplication.
Values should be inexpensive to clone, such as Arc<T> or Bytes.
Capacity is maintained asynchronously; insertion does not guarantee retention.
Fields§
§inner: Cache<K, V>§capacity: u64§size_registration: Option<CacheSizeRegistration>§metrics: Option<Arc<CacheMetrics>>Implementations§
Source§impl<K, V> Cache<K, V>
impl<K, V> Cache<K, V>
Sourcepub fn new<W, L>(name: &str, capacity: u64, weigher: W, listener: L) -> Self
pub fn new<W, L>(name: &str, capacity: u64, weigher: W, listener: L) -> Self
Creates a cache with the caller’s weight and eviction accounting.
Sourcepub fn with_metrics(self, metrics: Arc<CacheMetrics>) -> Self
pub fn with_metrics(self, metrics: Arc<CacheMetrics>) -> Self
Attaches caller-bound metrics without changing existing hit/miss accounting.
Sourcepub fn with_size_metric(self, metric: CacheSizeMetric) -> Self
pub fn with_size_metric(self, metric: CacheSizeMetric) -> Self
Samples this cache’s retained weight when the caller collects byte metrics.
Sourcepub fn get<'a, Q>(
&'a self,
key: &'a Q,
) -> impl Future<Output = Option<V>> + Send + 'a
pub fn get<'a, Q>( &'a self, key: &'a Q, ) -> impl Future<Output = Option<V>> + Send + 'a
Gets a completed value without starting a load.
Sourcepub async fn try_get_with<Q, F, E>(&self, key: &Q, load: F) -> Result<V, Arc<E>>
pub async fn try_get_with<Q, F, E>(&self, key: &Q, load: F) -> Result<V, Arc<E>>
Shares a successful load among concurrent callers with the same key and error type. Errors are shared with current waiters but are not retained as cached values. Cancelling the active loader lets a remaining caller execute its own loader.
Sourcepub async fn insert(&self, key: K, value: V)
pub async fn insert(&self, key: K, value: V)
Submits a completed value to the cache’s admission and eviction policy.
Sourcepub async fn invalidate<Q>(&self, key: &Q)
pub async fn invalidate<Q>(&self, key: &Q)
Invalidates a completed entry. This does not cancel a concurrent loader.
Sourcepub fn invalidate_if<F>(&self, predicate: F) -> Result<()>
pub fn invalidate_if<F>(&self, predicate: F) -> Result<()>
Invalidates completed entries matching a predicate. Pending loaders are not fenced. Physical removal and eviction accounting run asynchronously.
Sourcepub fn entry_count(&self) -> u64
pub fn entry_count(&self) -> u64
Returns the approximate number of retained entries.
Sourcepub fn weighted_size(&self) -> u64
pub fn weighted_size(&self) -> u64
Returns the approximate total retained weight.
Sourcepub async fn run_pending_tasks(&self)
pub async fn run_pending_tasks(&self)
Applies pending admission, eviction, and accounting work.