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 oldestlast_accessedtimestamp.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>
impl<V: Clone + Debug> MemoCache<V>
Sourcepub fn new(config: MemoConfig) -> Self
pub fn new(config: MemoConfig) -> Self
Create a new cache with the given configuration.
Sourcepub fn with_default() -> Self
pub fn with_default() -> Self
Create a cache with MemoConfig::default().
Sourcepub fn with_max_entries(max: usize) -> Self
pub fn with_max_entries(max: usize) -> Self
Create a cache with LRU eviction and a custom capacity.
Sourcepub fn get(&mut self, key: &MemoKey) -> MemoLookupResult<V>
pub fn get(&mut self, key: &MemoKey) -> MemoLookupResult<V>
Look up key in the cache.
Returns:
Hit(v)— the key was present, fresh, andvis 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.
Sourcepub fn insert(&mut self, key: MemoKey, value: V)
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.
Sourcepub fn invalidate(&mut self, key: &MemoKey) -> bool
pub fn invalidate(&mut self, key: &MemoKey) -> bool
Remove the entry for key, returning true if it was present.
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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