pub struct LruCache<K, V, S = RandomState> { /* private fields */ }
Expand description
An LRU Cache
Implementations
sourceimpl<K: Hash + Eq, V> LruCache<K, V>
impl<K: Hash + Eq, V> LruCache<K, V>
sourceimpl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S>
impl<K: Hash + Eq, V, S: BuildHasher> LruCache<K, V, S>
sourcepub fn with_hasher(cap: usize, hash_builder: S) -> LruCache<K, V, S>
pub fn with_hasher(cap: usize, hash_builder: S) -> LruCache<K, V, S>
Creates a new LRU Cache that holds at most cap
items and
uses the providedash builder to hash keys.
Example
use std::collections::HashMap;
use std::collections::hash_map::RandomState;
use lru::LruCache;
let s = RandomState::new();
let mut cache: LruCache<isize, &str> = LruCache::with_hasher(10, s);
sourcepub fn put(&mut self, k: K, v: V)
pub fn put(&mut self, k: K, v: V)
Puts a key-value pair into cache. If the key already exists it updates its value.
Example
use lru::LruCache;
let mut cache = LruCache::new(2);
cache.put(1, "a");
cache.put(2, "b");
assert_eq!(cache.get(&1), Some(&"a"));
assert_eq!(cache.get(&2), Some(&"b"));
sourcepub fn get<'a>(&'a mut self, k: &K) -> Option<&'a V>
pub fn get<'a>(&'a mut self, k: &K) -> Option<&'a V>
Returns a reference to the value of the key in the cache or None
if it is not
present in the cache. Moves the key to the head of the LRU list if it exists.
Example
use lru::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"));
assert_eq!(cache.get(&3), Some(&"d"));
sourcepub fn get_mut<'a>(&'a mut self, k: &K) -> Option<&'a mut V>
pub fn get_mut<'a>(&'a mut self, k: &K) -> Option<&'a mut V>
Returns a mutable reference to the value of the key in the cache or None
if it
is not present in the cache. Moves the key to the head of the LRU list if it exists.
Example
use lru::LruCache;
let mut cache = LruCache::new(2);
cache.put("apple", 8);
cache.put("banana", 4);
cache.put("banana", 6);
cache.put("pear", 2);
assert_eq!(cache.get_mut(&"apple"), None);
assert_eq!(cache.get_mut(&"banana"), Some(&mut 6));
assert_eq!(cache.get_mut(&"pear"), Some(&mut 2));
sourcepub fn peek<'a>(&'a self, k: &K) -> Option<&'a V>
pub fn peek<'a>(&'a self, k: &K) -> Option<&'a V>
Returns 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 lru::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"));
sourcepub fn contains(&self, k: &K) -> bool
pub fn contains(&self, k: &K) -> bool
Returns a bool indicating whether the given key is in the cache. Does not update the LRU list.
Example
use lru::LruCache;
let mut cache = LruCache::new(2);
cache.put(1, "a");
cache.put(2, "b");
cache.put(3, "c");
assert!(!cache.contains(&1));
assert!(cache.contains(&2));
assert!(cache.contains(&3));
sourcepub fn pop(&mut self, k: &K) -> Option<V>
pub fn pop(&mut self, k: &K) -> Option<V>
Removes and returns the value corresponding to the key from the cache or
None
if it does not exist.
Example
use lru::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);
sourcepub fn pop_lru(&mut self) -> Option<(K, V)>
pub fn pop_lru(&mut self) -> Option<(K, V)>
Removes and returns the key and value corresponding to the least recently
used item or None
if the cache is empty.
Example
use lru::LruCache;
let mut cache = LruCache::new(2);
cache.put(2, "a");
cache.put(3, "b");
cache.put(4, "c");
cache.get(&3);
assert_eq!(cache.pop_lru(), Some((4, "c")));
assert_eq!(cache.pop_lru(), Some((3, "b")));
assert_eq!(cache.pop_lru(), None);
assert_eq!(cache.len(), 0);
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of key-value pairs that are currently in the the cache.
Example
use lru::LruCache;
let mut cache = LruCache::new(2);
assert_eq!(cache.len(), 0);
cache.put(1, "a");
assert_eq!(cache.len(), 1);
cache.put(2, "b");
assert_eq!(cache.len(), 2);
cache.put(3, "c");
assert_eq!(cache.len(), 2);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns a bool indicating whether the cache is empty or not.
Example
use lru::LruCache;
let mut cache = LruCache::new(2);
assert!(cache.is_empty());
cache.put(1, "a");
assert!(!cache.is_empty());
sourcepub fn cap(&self) -> usize
pub fn cap(&self) -> usize
Returns the maximum number of key-value pairs the cache can hold.
Example
use lru::LruCache;
let mut cache: LruCache<isize, &str> = LruCache::new(2);
assert_eq!(cache.cap(), 2);
sourcepub fn resize(&mut self, cap: usize)
pub fn resize(&mut self, cap: usize)
Resizes the cache. If the new capacity is smaller than the size of the current cache any entries past the new capacity are discarded.
Example
use lru::LruCache;
let mut cache: LruCache<isize, &str> = LruCache::new(2);
cache.put(1, "a");
cache.put(2, "b");
cache.resize(4);
cache.put(3, "c");
cache.put(4, "d");
assert_eq!(cache.len(), 4);
assert_eq!(cache.get(&1), Some(&"a"));
assert_eq!(cache.get(&2), Some(&"b"));
assert_eq!(cache.get(&3), Some(&"c"));
assert_eq!(cache.get(&4), Some(&"d"));
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the contents of the cache.
Example
use lru::LruCache;
let mut cache: LruCache<isize, &str> = LruCache::new(2);
assert_eq!(cache.len(), 0);
cache.put(1, "a");
assert_eq!(cache.len(), 1);
cache.put(2, "b");
assert_eq!(cache.len(), 2);
cache.clear();
assert_eq!(cache.len(), 0);