pub struct LruCache<K: Eq + Hash, V, S: BuildHasher = RandomState, M: CountableMeter<K, V> = Count> { /* private fields */ }
Expand description

An LRU cache.

Implementations

Creates an empty cache that can hold at most capacity items.

Examples
use lru_cache::LruCache;
let mut cache: LruCache<i32, &str> = LruCache::new(10);

Creates an empty cache that can hold at most capacity as measured by meter.

You can implement the Meter trait to allow custom metrics.

Examples
use lru_cache::{LruCache, Meter};
use std::borrow::Borrow;

/// Measure Vec items by their length
struct VecLen;

impl<K, T> Meter<K, Vec<T>> for VecLen {
    // Use `Measure = usize` or implement `CountableMeter` as well.
    type Measure = usize;
    fn measure<Q: ?Sized>(&self, _: &Q, v: &Vec<T>) -> usize
        where K: Borrow<Q>
    {
        v.len()
    }
}

let mut cache = LruCache::with_meter(5, VecLen);
cache.insert(1, vec![1, 2]);
assert_eq!(cache.size(), 2);
cache.insert(2, vec![3, 4]);
cache.insert(3, vec![5, 6]);
assert_eq!(cache.size(), 4);
assert_eq!(cache.len(), 2);

Creates an empty cache that can hold at most capacity items with the given hash builder.

Returns a mutable reference to the value corresponding to the given key in the cache, if any.

Note that this method is not available for cache objects using Meter implementations other than Count.

Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(1, "a");
cache.insert(2, "b");
cache.insert(2, "c");
cache.insert(3, "d");

assert_eq!(cache.get_mut(&1), None);
assert_eq!(cache.get_mut(&2), Some(&mut "c"));

Returns an iterator over the cache’s key-value pairs in least- to most-recently-used order, with mutable references to the values.

Accessing the cache through the iterator does not affect the cache’s LRU state. Note that this method is not available for cache objects using Meter implementations. other than Count.

Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(1, 10);
cache.insert(2, 20);
cache.insert(3, 30);

let mut n = 2;

for (k, v) in cache.iter_mut() {
    assert_eq!(*k, n);
    assert_eq!(*v, n * 10);
    *v *= 10;
    n += 1;
}

assert_eq!(n, 4);
assert_eq!(cache.get_mut(&2), Some(&mut 200));
assert_eq!(cache.get_mut(&3), Some(&mut 300));

Creates an empty cache that can hold at most capacity as measured by meter with the given hash builder.

Returns the maximum size of the key-value pairs the cache can hold, as measured by the Meter used by the cache.

Examples
use lru_cache::LruCache;
let mut cache: LruCache<i32, &str> = LruCache::new(2);
assert_eq!(cache.capacity(), 2);

Checks if the map contains the given key.

Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(1);

cache.insert(1, "a");
assert!(cache.contains_key(&1));

Inserts a key-value pair into the cache. If the key already existed, the old value is returned.

Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(1, "a");
cache.insert(2, "b");
assert_eq!(cache.get_mut(&1), Some(&mut "a"));
assert_eq!(cache.get_mut(&2), Some(&mut "b"));

Removes the given key from the cache and returns its corresponding value.

Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(2, "a");

assert_eq!(cache.remove(&1), None);
assert_eq!(cache.remove(&2), Some("a"));
assert_eq!(cache.remove(&2), None);
assert_eq!(cache.len(), 0);

Sets the size of the key-value pairs the cache can hold, as measured by the Meter used by the cache.

Removes least-recently-used key-value pairs if necessary.

Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(1, "a");
cache.insert(2, "b");
cache.insert(3, "c");

assert_eq!(cache.get_mut(&1), None);
assert_eq!(cache.get_mut(&2), Some(&mut "b"));
assert_eq!(cache.get_mut(&3), Some(&mut "c"));

cache.set_capacity(3);
cache.insert(1, "a");
cache.insert(2, "b");

assert_eq!(cache.get_mut(&1), Some(&mut "a"));
assert_eq!(cache.get_mut(&2), Some(&mut "b"));
assert_eq!(cache.get_mut(&3), Some(&mut "c"));

cache.set_capacity(1);

assert_eq!(cache.get_mut(&1), None);
assert_eq!(cache.get_mut(&2), None);
assert_eq!(cache.get_mut(&3), Some(&mut "c"));

Removes and returns the least recently used key-value pair as a tuple.

Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(1, "a");
cache.insert(2, "b");

assert_eq!(cache.remove_lru(), Some((1, "a")));
assert_eq!(cache.len(), 1);

Returns the number of key-value pairs in the cache.

Returns the size of all the key-value pairs in the cache, as measured by the Meter used by the cache.

Returns true if the cache contains no key-value pairs.

Removes all key-value pairs from the cache.

Returns an iterator over the cache’s key-value pairs in least- to most-recently-used order.

Accessing the cache through the iterator does not affect the cache’s LRU state.

Examples
use lru_cache::LruCache;

let mut cache = LruCache::new(2);

cache.insert(1, 10);
cache.insert(2, 20);
cache.insert(3, 30);

let kvs: Vec<_> = cache.iter().collect();
assert_eq!(kvs, [(&2, &20), (&3, &30)]);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. 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.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

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.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more