Struct ritecache::LruCache [−][src]
pub struct LruCache<K: Eq + Hash, V, S: BuildHasher = DefaultHashBuilder, M: CountableMeter<K, V> = Count> { /* fields omitted */ }
Expand description
An LRU cache.
Implementations
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 ritecache::{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.put(1, vec![1, 2]);
assert_eq!(cache.size(), 2);
cache.put(2, vec![3, 4]);
cache.put(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 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 ritecache::{Cache, LruCache};
let mut cache = LruCache::new(2);
cache.put(1, 10);
cache.put(2, 20);
cache.put(3, 30);
let kvs: Vec<_> = cache.iter().collect();
assert_eq!(kvs, [(&2, &20), (&3, &30)]);
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 ritecache::{Cache, LruCache};
let mut cache = LruCache::new(2);
cache.put(1, 10);
cache.put(2, 20);
cache.put(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));
Trait Implementations
impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> Cache<K, V, S, M> for LruCache<K, V, S, M>
impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> Cache<K, V, S, M> for LruCache<K, V, S, M>
Creates an empty cache that can hold at most capacity
as measured by meter
with the
given hash builder.
Returns a 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 ritecache::{Cache, LruCache};
let mut cache = LruCache::new(2);
cache.put(1, "a");
cache.put(2, "b");
cache.put(2, "c");
cache.put(3, "d");
assert_eq!(cache.get(&1), None);
assert_eq!(cache.get(&2), Some(&"c"));
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 ritecache::{Cache, LruCache};
let mut cache = LruCache::new(2);
cache.put(1, "a");
cache.put(2, "b");
cache.put(2, "c");
cache.put(3, "d");
assert_eq!(cache.get_mut(&1), None);
assert_eq!(cache.get_mut(&2), Some(&mut "c"));
Returns a reference to the value corresponding to the key in the cache or None
if it is
not present in the cache. Unlike get
, peek
does not update the LRU list so the key’s
position will be unchanged.
Example
use ritecache::{Cache, LruCache};
let mut cache = LruCache::new(2);
cache.put(1, "a");
cache.put(2, "b");
assert_eq!(cache.peek(&1), Some(&"a"));
assert_eq!(cache.peek(&2), Some(&"b"));
Returns a mutable reference to the value corresponding to the key in the cache or None
if it is not present in the cache. Unlike get_mut
, peek_mut
does not update the LRU
list so the key’s position will be unchanged.
Example
use ritecache::{Cache, LruCache};
let mut cache = LruCache::new(2);
cache.put(1, "a");
cache.put(2, "b");
assert_eq!(cache.peek_mut(&1), Some(&mut "a"));
assert_eq!(cache.peek_mut(&2), Some(&mut "b"));
Returns the value corresponding to the least recently used item or None
if the
cache is empty. Like peek
, peek_lru
does not update the LRU list so the item’s
position will be unchanged.
Example
use ritecache::{Cache, LruCache};
let mut cache = LruCache::new(2);
cache.put(1, "a");
cache.put(2, "b");
assert_eq!(cache.peek_by_policy(), Some((&1, &"a")));
Checks if the map contains the given key.
Examples
use ritecache::{Cache, LruCache};
let mut cache = LruCache::new(1);
cache.put(1, "a");
assert!(cache.contains(&1));
Inserts a key-value pair into the cache. If the key already existed, the old value is returned.
Examples
use ritecache::{Cache, LruCache};
let mut cache = LruCache::new(2);
cache.put(1, "a");
cache.put(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 ritecache::{Cache, LruCache};
let mut cache = LruCache::new(2);
cache.put(2, "a");
assert_eq!(cache.pop(&1), None);
assert_eq!(cache.pop(&2), Some("a"));
assert_eq!(cache.pop(&2), None);
assert_eq!(cache.len(), 0);
Removes and returns the least recently used key-value pair as a tuple.
Examples
use ritecache::{Cache, LruCache};
let mut cache = LruCache::new(2);
cache.put(1, "a");
cache.put(2, "b");
assert_eq!(cache.pop_by_policy(), Some((1, "a")));
assert_eq!(cache.len(), 1);
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 ritecache::{Cache, LruCache};
let mut cache = LruCache::new(2);
cache.put(1, "a");
cache.put(2, "b");
cache.put(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.put(1, "a");
cache.put(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"));
Returns the size of all the key-value pairs in the cache, as measured by the Meter
used
by the cache.
Returns the maximum size of the key-value pairs the cache can hold, as measured by the
Meter
used by the cache.
Examples
use ritecache::{Cache, LruCache};
let mut cache: LruCache<i32, &str> = LruCache::new(2);
assert_eq!(cache.capacity(), 2);
impl<K: Debug + Eq + Hash, V: Debug, S: BuildHasher, M: CountableMeter<K, V>> Debug for LruCache<K, V, S, M>
impl<K: Debug + Eq + Hash, V: Debug, S: BuildHasher, M: CountableMeter<K, V>> Debug for LruCache<K, V, S, M>
impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> Extend<(K, V)> for LruCache<K, V, S, M>
impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> Extend<(K, V)> for LruCache<K, V, S, M>
Extends a collection with the contents of an iterator. Read more
extend_one
)Extends a collection with exactly one element.
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator for LruCache<K, V, S, M>
impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator for LruCache<K, V, S, M>
impl<'a, K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator for &'a LruCache<K, V, S, M>
impl<'a, K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator for &'a LruCache<K, V, S, M>
impl<'a, K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator for &'a mut LruCache<K, V, S, M>
impl<'a, K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> IntoIterator for &'a mut LruCache<K, V, S, M>
Auto Trait Implementations
impl<K, V, S, M> RefUnwindSafe for LruCache<K, V, S, M> where
K: RefUnwindSafe,
M: RefUnwindSafe,
S: RefUnwindSafe,
V: RefUnwindSafe,
<M as Meter<K, V>>::Measure: RefUnwindSafe,
impl<K, V, S, M> Send for LruCache<K, V, S, M> where
K: Send,
M: Send,
S: Send,
V: Send,
<M as Meter<K, V>>::Measure: Send,
impl<K, V, S, M> Sync for LruCache<K, V, S, M> where
K: Sync,
M: Sync,
S: Sync,
V: Sync,
<M as Meter<K, V>>::Measure: Sync,
impl<K, V, S, M> Unpin for LruCache<K, V, S, M> where
M: Unpin,
S: Unpin,
<M as Meter<K, V>>::Measure: Unpin,
impl<K, V, S, M> UnwindSafe for LruCache<K, V, S, M> where
K: RefUnwindSafe,
M: UnwindSafe,
S: UnwindSafe,
V: RefUnwindSafe,
<M as Meter<K, V>>::Measure: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more