Expand description
A HashMap which maintains an internal last recently used node list.
Implementations
sourceimpl<K, V> LruMap<K, V> where
K: Eq + Hash,
impl<K, V> LruMap<K, V> where
K: Eq + Hash,
sourcepub fn with_ttl(dur: Duration) -> Self
pub fn with_ttl(dur: Duration) -> Self
Create a new LRU HashMap, with node TTL’s.
An LruMap with a TTL can call LruMap::drain_timedout() to return an
iterator that will return items that have become stale.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Return true if the container is empty. Return false otherwise.
use qlrumap::LruMap;
let mut map = LruMap::new();
assert_eq!(map.is_empty(), true);
map.insert(1, 1);
assert_eq!(map.is_empty(), false);sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Add a new entry to the container. New nodes will be the least recently used node.
Adding a new node:
use qlrumap::LruMap;
let mut map = LruMap::new();
map.insert(1, 1);
assert_eq!(map.len(), 1);Adding an already existing key will replace the value, and return the old one:
use qlrumap::LruMap;
// Create a new LruMap, and insert a test node, make sure length == 1.
let mut map = LruMap::new();
map.insert(1, 1);
assert_eq!(map.len(), 1);
// Insert another value, with the same key, make sure length is
// unchanged and make sure the returned value is the old value.
let v = map.insert(1, 2);
assert_eq!(map.len(), 1);
assert_eq!(v, Some(1));
// Make sure the stored value is the new value.
let v = map.get(&1);
assert_eq!(v, Some(&2));sourcepub fn get<Q: ?Sized>(&mut self, k: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Hash + Eq,
pub fn get<Q: ?Sized>(&mut self, k: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Hash + Eq,
Given a reference to a key return the value associated with it.
The map entry will be moved to the front of the internal lru list.
If the LruMap has a TTL configured, the node’s timeout will be reset.
pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Hash + Eq,
sourcepub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Hash + Eq,
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Hash + Eq,
Remove a node from the map based on its key. Returns the value if its key was found in the map.
use qlrumap::LruMap;
let mut map = LruMap::new();
map.insert(1, 1);
let v = map.remove(&1);
assert_eq!(v, Some(1));
assert!(map.is_empty());sourcepub fn touch<Q: ?Sized>(&mut self, k: &Q) -> bool where
K: Borrow<Q>,
Q: Hash + Eq,
pub fn touch<Q: ?Sized>(&mut self, k: &Q) -> bool where
K: Borrow<Q>,
Q: Hash + Eq,
Move a node to the front of the LRU list given a key.
Returns true if the key exists in the map. Returns false otherwise.
use qlrumap::LruMap;
let mut map = LruMap::new();
map.insert(1, 1);
map.insert(2, 2);
assert_eq!(map.oldest(), Some((&1, &1)));
map.touch(&1);
assert_eq!(map.oldest(), Some((&2, &2)));sourcepub fn pop(&mut self) -> Option<(K, V)>
pub fn pop(&mut self) -> Option<(K, V)>
Remove and return the oldest node.
use qlrumap::LruMap;
let mut map = LruMap::new();
map.insert(1, 1);
map.insert(2, 2);
assert_eq!(map.pop(), Some((1, 1)));
assert_eq!(map.pop(), Some((2, 2)));
assert_eq!(map.pop(), None);
assert!(map.is_empty());sourcepub fn youngest(&self) -> Option<(&K, &V)>
pub fn youngest(&self) -> Option<(&K, &V)>
Get the least recently used (youngest) node.
Does not update the node’s timestamp.
use qlrumap::LruMap;
let mut map = LruMap::new();
map.insert(1, 1);
map.insert(2, 2);
assert_eq!(map.youngest(), Some((&2, &2)));sourcepub fn oldest(&self) -> Option<(&K, &V)>
pub fn oldest(&self) -> Option<(&K, &V)>
Get the oldest node.
Does not update the node’s timestamp.
use qlrumap::LruMap;
let mut map = LruMap::new();
map.insert(1, 1);
map.insert(2, 2);
assert_eq!(map.oldest(), Some((&1, &1)));sourcepub fn drain(&mut self) -> Drain<'_, K, V>ⓘNotable traits for Drain<'_, K, V>impl<K, V> Iterator for Drain<'_, K, V> where
K: Eq + Hash, type Item = (K, V);
pub fn drain(&mut self) -> Drain<'_, K, V>ⓘNotable traits for Drain<'_, K, V>impl<K, V> Iterator for Drain<'_, K, V> where
K: Eq + Hash, type Item = (K, V);
K: Eq + Hash, type Item = (K, V);
Return an iterator that will yield all the elements of the LruMap.
sourcepub fn drain_ordered(&mut self) -> DrainOrdered<'_, K, V>ⓘNotable traits for DrainOrdered<'_, K, V>impl<K, V> Iterator for DrainOrdered<'_, K, V> where
K: Eq + Hash, type Item = (K, V);
pub fn drain_ordered(&mut self) -> DrainOrdered<'_, K, V>ⓘNotable traits for DrainOrdered<'_, K, V>impl<K, V> Iterator for DrainOrdered<'_, K, V> where
K: Eq + Hash, type Item = (K, V);
K: Eq + Hash, type Item = (K, V);
Return an iterator that will yield all the elements of the LruMap,
ordered.
The youngest node will be returned first.
use qlrumap::LruMap;
let mut map = LruMap::new();
map.insert(1, "elena");
map.insert(2, "chloe");
map.insert(3, "drake");
// make second entry the youngest
map.touch(&2);
let v: Vec<(u32, &str)> = map.drain_ordered().collect();
assert_eq!(v[0], (2, "chloe"));
assert_eq!(v[1], (3, "drake"));
assert_eq!(v[2], (1, "elena"));
assert!(map.is_empty());sourcepub fn drain_filter<F>(&mut self, f: F) -> DrainFilter<'_, K, V, F>ⓘNotable traits for DrainFilter<'_, K, V, F>impl<K, V, F> Iterator for DrainFilter<'_, K, V, F> where
K: Eq + Hash,
F: FnMut(&K, &mut V) -> bool, type Item = (K, V); where
F: FnMut(&K, &mut V) -> bool,
pub fn drain_filter<F>(&mut self, f: F) -> DrainFilter<'_, K, V, F>ⓘNotable traits for DrainFilter<'_, K, V, F>impl<K, V, F> Iterator for DrainFilter<'_, K, V, F> where
K: Eq + Hash,
F: FnMut(&K, &mut V) -> bool, type Item = (K, V); where
F: FnMut(&K, &mut V) -> bool,
K: Eq + Hash,
F: FnMut(&K, &mut V) -> bool, type Item = (K, V);
Drain nodes using a closure to validate notes to be drained.
use qlrumap::LruMap;
let mut map = LruMap::new();
map.insert(1, 1);
map.insert(2, 2);
map.insert(3, 3);
let mut it = map.drain_filter(|k, v| {
// drain second element
*k == 2 && *v == 2
});
assert_eq!(it.next(), Some((2, 2)));
assert_eq!(it.next(), None);
drop(it);
assert_eq!(map.len(), 2);
assert_eq!(map.oldest(), Some((&1, &1)));
assert_eq!(map.youngest(), Some((&3, &3)));sourcepub fn drain_timedout(&mut self) -> DrainTimedout<'_, K, V>ⓘNotable traits for DrainTimedout<'_, K, V>impl<K, V> Iterator for DrainTimedout<'_, K, V> where
K: Eq + Hash, type Item = (K, V);
pub fn drain_timedout(&mut self) -> DrainTimedout<'_, K, V>ⓘNotable traits for DrainTimedout<'_, K, V>impl<K, V> Iterator for DrainTimedout<'_, K, V> where
K: Eq + Hash, type Item = (K, V);
K: Eq + Hash, type Item = (K, V);
Drain entries that have timed out.
To cap the number of elements in the LruMap and also remove stale
elements in one call, use LruMap::drain_old().
Notes
- The timestamp used to check if nodes have expired is generated and
stored in the iterator when it is created. This implies that
expiration candidacy is effectively determined when
drain_timeout()is called, and not when the iterator’snext()method is called.
sourcepub fn drain_overflow(&mut self, lim: usize) -> DrainOverflow<'_, K, V>ⓘNotable traits for DrainOverflow<'_, K, V>impl<K, V> Iterator for DrainOverflow<'_, K, V> where
K: Eq + Hash, type Item = (K, V);
pub fn drain_overflow(&mut self, lim: usize) -> DrainOverflow<'_, K, V>ⓘNotable traits for DrainOverflow<'_, K, V>impl<K, V> Iterator for DrainOverflow<'_, K, V> where
K: Eq + Hash, type Item = (K, V);
K: Eq + Hash, type Item = (K, V);
Cap the number of entries in the map.
Creates a draining iterator which will return the oldest entries until
there are lim remaining entries in the map.
To cap the number of elements in the LruMap and also remove stale
elements in one call, use LruMap::drain_old().
use qlrumap::LruMap;
let mut map = LruMap::new();
map.insert(1, 1);
map.insert(2, 2);
let mut drain = map.drain_overflow(2);
assert_eq!(drain.next(), None);
drop(drain);
map.insert(3, 3);
let mut drain = map.drain_overflow(2);
assert_eq!(drain.next(), Some((1, 1)));
assert_eq!(drain.next(), None);sourcepub fn drain_old(&mut self, cap: Option<usize>) -> DrainOld<'_, K, V>ⓘNotable traits for DrainOld<'_, K, V>impl<K, V> Iterator for DrainOld<'_, K, V> where
K: Eq + Hash, type Item = (K, V);
pub fn drain_old(&mut self, cap: Option<usize>) -> DrainOld<'_, K, V>ⓘNotable traits for DrainOld<'_, K, V>impl<K, V> Iterator for DrainOld<'_, K, V> where
K: Eq + Hash, type Item = (K, V);
K: Eq + Hash, type Item = (K, V);
Remove old entries and return them in an iterator.
If the cap parameter is set then the oldest node will be removed
until the number of elements is equal to or lower than this value.
use std::time::Duration;
use qlrumap::LruMap;
let mut map = LruMap::with_ttl(Duration::from_secs(10));
map.insert(1, "elena");
map.insert(2, "chloe");
map.insert(3, "drake");
map.touch(&1);
map.touch(&2);
// Limit map to 2 entries, use current time as expiry
let mut drain = map.drain_old(Some(2));
assert_eq!(drain.next(), Some((3, "drake")));
assert_eq!(drain.next(), None);Trait Implementations
Auto Trait Implementations
impl<K, V> RefUnwindSafe for LruMap<K, V> where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> !Send for LruMap<K, V>
impl<K, V> !Sync for LruMap<K, V>
impl<K, V> Unpin for LruMap<K, V> where
K: Unpin,
V: Unpin,
impl<K, V> UnwindSafe for LruMap<K, V> where
K: UnwindSafe,
V: UnwindSafe + RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more