Struct lru_time_cache::LruCache
source · pub struct LruCache<Key, Value> { /* private fields */ }
Expand description
Implementation of LRU cache.
Implementations
sourceimpl<Key, Value> LruCache<Key, Value>where
Key: Ord + Clone,
impl<Key, Value> LruCache<Key, Value>where
Key: Ord + Clone,
sourcepub fn with_capacity(capacity: usize) -> LruCache<Key, Value>
pub fn with_capacity(capacity: usize) -> LruCache<Key, Value>
Constructor for capacity based LruCache
.
sourcepub fn with_expiry_duration(time_to_live: Duration) -> LruCache<Key, Value>
pub fn with_expiry_duration(time_to_live: Duration) -> LruCache<Key, Value>
Constructor for time based LruCache
.
sourcepub fn with_expiry_duration_and_capacity(
time_to_live: Duration,
capacity: usize
) -> LruCache<Key, Value>
pub fn with_expiry_duration_and_capacity(
time_to_live: Duration,
capacity: usize
) -> LruCache<Key, Value>
Constructor for dual-feature capacity and time based LruCache
.
sourcepub fn insert(&mut self, key: Key, value: Value) -> Option<Value>
pub fn insert(&mut self, key: Key, value: Value) -> Option<Value>
Inserts a key-value pair into the cache.
If the key already existed in the cache, the existing value is returned and overwritten in
the cache. Otherwise, the key-value pair is inserted and None
is returned.
sourcepub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<Value>where
Key: Borrow<Q>,
Q: Ord,
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<Value>where
Key: Borrow<Q>,
Q: Ord,
Removes a key-value pair from the cache.
sourcepub fn get<Q: ?Sized>(&mut self, key: &Q) -> Option<&Value>where
Key: Borrow<Q>,
Q: Ord,
pub fn get<Q: ?Sized>(&mut self, key: &Q) -> Option<&Value>where
Key: Borrow<Q>,
Q: Ord,
Retrieves a reference to the value stored under key
, or None
if the key doesn’t exist.
Also removes expired elements and updates the time.
sourcepub fn peek<Q: ?Sized>(&self, key: &Q) -> Option<&Value>where
Key: Borrow<Q>,
Q: Ord,
pub fn peek<Q: ?Sized>(&self, key: &Q) -> Option<&Value>where
Key: Borrow<Q>,
Q: Ord,
Returns a reference to the value with the given key
, if present and not expired, without
updating the timestamp.
sourcepub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut Value>where
Key: Borrow<Q>,
Q: Ord,
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut Value>where
Key: Borrow<Q>,
Q: Ord,
Retrieves a mutable reference to the value stored under key
, or None
if the key doesn’t
exist. Also removes expired elements and updates the time.
sourcepub fn contains_key<Q: ?Sized>(&self, key: &Q) -> boolwhere
Key: Borrow<Q>,
Q: Ord,
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> boolwhere
Key: Borrow<Q>,
Q: Ord,
Returns whether key
exists in the cache or not.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the size of the cache, i.e. the number of cached non-expired key-value pairs.
sourcepub fn entry(&mut self, key: Key) -> Entry<'_, Key, Value>
pub fn entry(&mut self, key: Key) -> Entry<'_, Key, Value>
Gets the given key’s corresponding entry in the map for in-place manipulation.