Skip to main content

Cache

Struct Cache 

Source
pub struct Cache<K, V> { /* private fields */ }
Expand description

A fixed-capacity key-value cache using Clock2Q+ replacement.

Keys must keep the same Hash and Eq identity throughout each cache operation, including value factories and other callbacks, and while resident or retained in Ghost history. Violating this requirement is a logic error and may cause incorrect lookups or admission decisions.

Implementations§

Source§

impl<K: Hash + Eq, V> Cache<K, V>

Source

pub fn new(capacity: NonZeroUsize) -> Self

Creates an empty cache with room for capacity residents.

Source

pub const fn capacity(&self) -> usize

Returns the maximum number of entries the cache can hold.

Source

pub fn len(&self) -> usize

Returns the number of entries currently in the cache.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache holds no entries.

Source

pub fn contains(&self, key: &K) -> bool

Returns true if key is in the cache without recording use.

Source

pub fn peek(&self, key: &K) -> Option<&V>

Returns a reference to the value for key without recording use.

Unlike Self::get, this does not affect replacement decisions.

Source

pub fn get(&self, key: &K) -> Option<&V>

Returns a reference to the value for key, recording use.

This takes &self so it can be called concurrently behind a shared lock. Hits update only relaxed atomic replacement metadata.

Source

pub fn get_at(&self, slot: usize, key: &K) -> Option<&V>

Returns a reference to the value in slot if that slot currently holds key as a live entry, recording use.

This is the read half of an external slot index: callers that recorded a key’s slot (via Self::get_or_insert_mut) can resolve it with a key compare instead of a hash lookup. Any stale index entry reads as a miss rather than a wrong value: a slot reused for another key fails the key compare, a slot freed by Self::remove or Self::retain is not live, and an out-of-range slot does not exist. External indexes therefore need no maintenance beyond tolerating misses.

Source

pub fn get_mut(&mut self, key: &K) -> Option<&mut V>

Returns a mutable reference to the value for key, recording use.

Source

pub fn put(&mut self, key: K, value: V) -> Option<V>

Inserts value for key.

If key was already present, replaces and returns the previous value, recording use. Otherwise claims vacant capacity or selects a resident to evict.

Source

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

Returns the value for key, computing and inserting it with f on a miss.

On a hit, f is not called. On a miss, f is called, its result is inserted (evicting a resident if necessary), and a reference to the stored value is returned.

Source

pub fn try_get_or_insert_with<F: FnOnce() -> Result<V, E>, E>( &mut self, key: K, f: F, ) -> Result<&V, E>

Returns the value for key, computing and inserting it with a fallible f on a miss.

On a hit, f is not called. On a miss, f is called; if it returns an error the error is propagated and nothing is inserted, so failures are not cached.

Source

pub fn get_or_insert_mut<F: FnOnce() -> V>( &mut self, key: K, make: F, ) -> (usize, &mut V)

Returns the slot index and a mutable reference to the slot for key, reusing an existing allocation where possible.

On a hit, records use and returns the current value. On a miss into a reused slot (a freed slot or an eviction victim), returns the reused slot’s stale value for the caller to overwrite. Only when the cache grows is make called to produce a fresh value. This lets callers whose values are buffers overwrite them in place without allocating on every insert.

The slot index identifies the entry until it is evicted or removed, so callers can record it in an external index and resolve later reads with Self::get_at instead of a hash lookup.

Source

pub fn remove(&mut self, key: &K) -> bool

Removes key, returning whether it was present.

The slot and its allocation are retained for reuse, so the value is not returned.

Source

pub fn retain<F: FnMut(&K, &V) -> bool>(&mut self, keep: F)

Retains only the entries for which keep returns true.

Dropped entries’ slots and allocations are retained for reuse.

Source

pub fn clear(&mut self)

Removes all entries, dropping their values and retaining the allocated capacity of the indexes and slot vectors.

Source§

impl<K: Hash + Eq + Default, V> Cache<K, V>

Source

pub fn prefill<F: FnMut() -> V>(&mut self, make: F)

Pre-allocates all slots up to capacity, each holding a value from make, and leaves them free for reuse.

After this call, the first capacity inserts reuse a pre-allocated slot instead of growing, so make (and any allocation it performs) runs only here. Use this to front-load value allocation at construction so steady-state inserts never allocate values. Free slots are seeded with the default key as a throwaway placeholder that is overwritten when the slot is first filled.

Trait Implementations§

Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for Cache<K, V>
where Vec<Entry<K, V>>: Freeze, GhostQueue<K>: Freeze,

§

impl<K, V> RefUnwindSafe for Cache<K, V>
where Vec<Entry<K, V>>: RefUnwindSafe, GhostQueue<K>: RefUnwindSafe,

§

impl<K, V> Send for Cache<K, V>
where Vec<Entry<K, V>>: Send, GhostQueue<K>: Send,

§

impl<K, V> Sync for Cache<K, V>
where Vec<Entry<K, V>>: Sync, GhostQueue<K>: Sync,

§

impl<K, V> Unpin for Cache<K, V>
where Vec<Entry<K, V>>: Unpin, GhostQueue<K>: Unpin,

§

impl<K, V> UnsafeUnpin for Cache<K, V>
where Vec<Entry<K, V>>: UnsafeUnpin, GhostQueue<K>: UnsafeUnpin,

§

impl<K, V> UnwindSafe for Cache<K, V>
where Vec<Entry<K, V>>: UnwindSafe, GhostQueue<K>: UnwindSafe,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Threaded<T> for T

Source§

type Rest = ()

The outputs beyond the threaded value.
Source§

fn split(self) -> (T, ())

Splits into the threaded value and the extra outputs.
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more