Struct lru::LruCache
[−]
[src]
pub struct LruCache<K, V> { /* fields omitted */ }
An LRU Cache
Methods
impl<K: Hash + Eq, V> LruCache<K, V>
[src]
fn new(cap: usize) -> LruCache<K, V>
[src]
Creates a new LRU Cache that holds at most cap
items.
Example
use lru::LruCache; let mut cache: LruCache<isize, &str> = LruCache::new(10);
fn put(&mut self, k: K, v: V)
[src]
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"));
fn get<'a>(&'a mut self, k: &K) -> Option<&'a V>
[src]
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"));
fn get_mut<'a>(&'a mut self, k: &K) -> Option<&'a mut V>
[src]
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));
fn peek<'a>(&'a self, k: &K) -> Option<&'a V>
[src]
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"));
fn contains(&self, k: &K) -> bool
[src]
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));
fn pop(&mut self, k: &K) -> Option<V>
[src]
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);
fn len(&self) -> usize
[src]
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);
fn cap(&self) -> usize
[src]
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);
fn clear(&mut self)
[src]
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);