Skip to main content

MemoCache

Struct MemoCache 

Source
pub struct MemoCache<V: Clone> { /* private fields */ }
Expand description

A memoization cache for inference results keyed by MemoKey.

§Eviction

When insert is called and the cache has reached config.max_entries, one entry is evicted according to config.eviction:

  • Lru — the entry with the oldest last_accessed timestamp.
  • Fifo — the entry at the front of the insertion-order deque.
  • Ttl — any expired entry first; if none, the front of the insertion deque (i.e., FIFO fall-back).

§TTL on access

If config.ttl is Some(d), entries are checked on every get call. An entry whose inserted_at is older than d will be removed and MemoLookupResult::Expired returned instead of Hit.

Implementations§

Source§

impl<V: Clone + Debug> MemoCache<V>

Source

pub fn new(config: MemoConfig) -> Self

Create a new cache with the given configuration.

Source

pub fn with_default() -> Self

Create a cache with MemoConfig::default().

Source

pub fn with_max_entries(max: usize) -> Self

Create a cache with LRU eviction and a custom capacity.

Source

pub fn get(&mut self, key: &MemoKey) -> MemoLookupResult<V>

Look up key in the cache.

Returns:

  • Hit(v) — the key was present, fresh, and v is a clone of the value.
  • Miss — the key was not found.
  • Expired— the key was found but had exceeded its TTL; it has been removed from the cache.
Source

pub fn insert(&mut self, key: MemoKey, value: V)

Insert value under key.

If the cache is already at capacity, one entry is evicted first according to config.eviction.

Source

pub fn invalidate(&mut self, key: &MemoKey) -> bool

Remove the entry for key, returning true if it was present.

Source

pub fn clear(&mut self)

Remove all entries and reset the entry count in stats.

Source

pub fn stats(&self) -> &MemoStats

Snapshot of the current statistics.

Source

pub fn len(&self) -> usize

Number of entries currently stored.

Source

pub fn is_empty(&self) -> bool

Whether the cache is empty.

Auto Trait Implementations§

§

impl<V> Freeze for MemoCache<V>

§

impl<V> RefUnwindSafe for MemoCache<V>
where V: RefUnwindSafe,

§

impl<V> Send for MemoCache<V>
where V: Send,

§

impl<V> Sync for MemoCache<V>
where V: Sync,

§

impl<V> Unpin for MemoCache<V>
where V: Unpin,

§

impl<V> UnsafeUnpin for MemoCache<V>

§

impl<V> UnwindSafe for MemoCache<V>
where V: 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<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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V