pub struct LruMap<K, V> where
    K: Eq + Hash
{ /* private fields */ }
Expand description

A HashMap which maintains an internal last recently used node list.

Implementations

Create a new LRU HashMap, without node expiry TTL.

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.

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);

Return the number of elements in the container.

Remove all nodes from the LruMap.

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));

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.

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());

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)));

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());

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)));

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)));

Return an iterator that will yield all the elements of the LruMap.

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());

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)));

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’s next() method is called.

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);

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

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.