Skip to main content

Cache

Struct Cache 

Source
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>
where K: Eq + Hash + Send + Sync + 'static, V: Clone + Send + Sync + 'static,

Source

pub fn new<W, L>(name: &str, capacity: u64, weigher: W, listener: L) -> Self
where W: Fn(&K, &V) -> u32 + Send + Sync + 'static, L: Fn(Arc<K>, V, RemovalCause) + Send + Sync + 'static,

Creates a cache with the caller’s weight and eviction accounting.

Source

pub fn with_metrics(self, metrics: Arc<CacheMetrics>) -> Self

Attaches caller-bound metrics without changing existing hit/miss accounting.

Source

pub fn with_size_metric(self, metric: CacheSizeMetric) -> Self

Samples this cache’s retained weight when the caller collects byte metrics.

Source

pub fn get<'a, Q>( &'a self, key: &'a Q, ) -> impl Future<Output = Option<V>> + Send + 'a
where K: Borrow<Q>, Q: Hash + Eq + Sync + ?Sized,

Gets a completed value without starting a load.

Source

pub async fn try_get_with<Q, F, E>(&self, key: &Q, load: F) -> Result<V, Arc<E>>
where K: Borrow<Q>, Q: ToOwned<Owned = K> + Hash + Eq + ?Sized, F: Future<Output = Result<V, E>>, E: Send + Sync + 'static,

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.

Source

pub async fn insert(&self, key: K, value: V)

Submits a completed value to the cache’s admission and eviction policy.

Source

pub async fn invalidate<Q>(&self, key: &Q)
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Invalidates a completed entry. This does not cancel a concurrent loader.

Source

pub fn invalidate_if<F>(&self, predicate: F) -> Result<()>
where F: Fn(&K, &V) -> bool + Send + Sync + 'static,

Invalidates completed entries matching a predicate. Pending loaders are not fenced. Physical removal and eviction accounting run asynchronously.

Source

pub fn capacity(&self) -> u64

Returns the configured weight capacity.

Source

pub fn entry_count(&self) -> u64

Returns the approximate number of retained entries.

Source

pub fn weighted_size(&self) -> u64

Returns the approximate total retained weight.

Source

pub async fn run_pending_tasks(&self)

Applies pending admission, eviction, and accounting work.

Trait Implementations§

Source§

impl<K: Clone, V: Clone> Clone for Cache<K, V>

Source§

fn clone(&self) -> Cache<K, V>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<K, V> !RefUnwindSafe for Cache<K, V>

§

impl<K, V> !UnwindSafe for Cache<K, V>

§

impl<K, V> Freeze for Cache<K, V>

§

impl<K, V> Send for Cache<K, V>
where K: Send + Sync, V: Send + Sync,

§

impl<K, V> Sync for Cache<K, V>
where K: Send + Sync, V: Send + Sync,

§

impl<K, V> Unpin for Cache<K, V>

§

impl<K, V> UnsafeUnpin for Cache<K, V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.