pub struct LfuCache<K, V> { /* private fields */ }Expand description
An LFU cache with a fixed capacity and optional per-entry TTL.
Entries with equal frequency are evicted in FIFO order (the entry inserted earlier at that frequency is evicted first).
§Type parameters
K: key type — must beEq + Hash + Clone.V: value type.
Implementations§
Source§impl<K, V> LfuCache<K, V>
impl<K, V> LfuCache<K, V>
Sourcepub fn new(cap: usize) -> Self
pub fn new(cap: usize) -> Self
Create a new LfuCache with the given capacity.
A capacity of 0 is valid; every insert will immediately be evicted on
the next insert.
Sourcepub fn get(&mut self, key: &K) -> Option<&V>
pub fn get(&mut self, key: &K) -> Option<&V>
Return the value for key, incrementing its frequency.
If the entry has expired (TTL), it is removed and None is returned
without updating frequency or recency.
Sourcepub fn put_with_ttl(&mut self, key: K, value: V, ttl: Duration) -> Option<V>
pub fn put_with_ttl(&mut self, key: K, value: V, ttl: Duration) -> Option<V>
Insert or update key -> value with a TTL.
Sourcepub fn peek(&self, key: &K) -> Option<&V>
pub fn peek(&self, key: &K) -> Option<&V>
Read a value without updating frequency or recency.
Returns None if absent or expired.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Return true if key is present and not expired.
Trait Implementations§
Source§impl<K, V> Cache<K, V> for LfuCache<K, V>
impl<K, V> Cache<K, V> for LfuCache<K, V>
Source§fn get(&mut self, key: &K) -> Option<&V>
fn get(&mut self, key: &K) -> Option<&V>
Look up
key, returning a reference to the value if present. Read moreSource§fn put_with_ttl(&mut self, key: K, value: V, ttl: Duration) -> Option<V>
fn put_with_ttl(&mut self, key: K, value: V, ttl: Duration) -> Option<V>
Source§fn remove(&mut self, key: &K) -> Option<V>
fn remove(&mut self, key: &K) -> Option<V>
Remove the entry associated with
key, returning its value if present. Read moreSource§fn peek(&self, key: &K) -> Option<&V>
fn peek(&self, key: &K) -> Option<&V>
Look up
key without updating access metadata (no promotion). Read moreSource§fn contains_key(&self, key: &K) -> bool
fn contains_key(&self, key: &K) -> bool
Auto Trait Implementations§
impl<K, V> Freeze for LfuCache<K, V>
impl<K, V> RefUnwindSafe for LfuCache<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for LfuCache<K, V>
impl<K, V> Sync for LfuCache<K, V>
impl<K, V> Unpin for LfuCache<K, V>
impl<K, V> UnsafeUnpin for LfuCache<K, V>
impl<K, V> UnwindSafe for LfuCache<K, V>
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