pub struct RawLRU<K, V, E = DefaultEvictCallback, S = DefaultHashBuilder> { /* private fields */ }Expand description
A fixed size RawLRU Cache
Example
- Use
RawLRUwith default noop callback.
use caches::{RawLRU, PutResult, Cache};
let mut cache = RawLRU::new(2).unwrap();
// fill the cache
assert_eq!(cache.put(1, 1), PutResult::Put);
assert_eq!(cache.put(2, 2), PutResult::Put);
// put 3, should evict the entry (1, 1)
assert_eq!(cache.put(3, 3), PutResult::Evicted {key: 1,value: 1});
// put 4, should evict the entry (2, 2)
assert_eq!(cache.put(4, 4), PutResult::Evicted {key: 2,value: 2});
// get 3, should update the recent-ness
assert_eq!(cache.get(&3), Some(&3));
// put 5, should evict the entry (4, 4)
assert_eq!(cache.put(5, 5), PutResult::Evicted {key: 4,value: 4});- Use
RawLRUwith a custom callback.
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use caches::{OnEvictCallback, RawLRU, PutResult, Cache};
// EvictedCounter is a callback which is used to record the number of evicted entries.
struct EvictedCounter {
ctr: Arc<AtomicU64>,
}
impl EvictedCounter {
pub fn new(ctr: Arc<AtomicU64>) -> Self {
Self {
ctr,
}
}
}
impl OnEvictCallback for EvictedCounter {
fn on_evict<K, V>(&self, _: &K, _: &V) {
self.ctr.fetch_add(1, Ordering::SeqCst);
}
}
let counter = Arc::new(AtomicU64::new(0));
let mut cache: RawLRU<u64, u64, EvictedCounter> = RawLRU::with_on_evict_cb(2, EvictedCounter::new(counter.clone())).unwrap();
// fill the cache
assert_eq!(cache.put(1, 1), PutResult::Put);
assert_eq!(cache.put(2, 2), PutResult::Put);
// put 3, should evict the entry (1, 1)
assert_eq!(cache.put(3, 3), PutResult::Evicted {key: 1,value: 1});
// put 4, should evict the entry (2, 2)
assert_eq!(cache.put(4, 4), PutResult::Evicted {key: 2,value: 2});
// get 3, should update the recent-ness
assert_eq!(cache.get(&3), Some(&3));
// put 5, should evict the entry (4, 4)
assert_eq!(cache.put(5, 5), PutResult::Evicted {key: 4,value: 4});
assert_eq!(counter.load(Ordering::SeqCst), 3);Implementations
sourceimpl<K: Hash + Eq, V, S: BuildHasher> RawLRU<K, V, DefaultEvictCallback, S>
impl<K: Hash + Eq, V, S: BuildHasher> RawLRU<K, V, DefaultEvictCallback, S>
sourcepub fn with_hasher(cap: usize, hash_builder: S) -> Result<Self, CacheError>
pub fn with_hasher(cap: usize, hash_builder: S) -> Result<Self, CacheError>
Creates a new LRU Cache that holds at most cap items and
uses the provided hash builder to hash keys.
Example
use caches::{RawLRU, DefaultHashBuilder};
let s = DefaultHashBuilder::default();
let mut cache: RawLRU<isize, &str> = RawLRU::with_hasher(10, s).unwrap();sourceimpl<K: Hash + Eq, V, E: OnEvictCallback> RawLRU<K, V, E, DefaultHashBuilder>
impl<K: Hash + Eq, V, E: OnEvictCallback> RawLRU<K, V, E, DefaultHashBuilder>
sourcepub fn with_on_evict_cb(cap: usize, cb: E) -> Result<Self, CacheError>
pub fn with_on_evict_cb(cap: usize, cb: E) -> Result<Self, CacheError>
Creates a new LRU Cache that holds at most cap items and
uses the provided evict callback.
Example
use caches::{RawLRU, OnEvictCallback};
use std::sync::atomic::{AtomicU64, Ordering};
struct EvictedCounter {
ctr: AtomicU64,
}
impl Default for EvictedCounter {
fn default() -> Self {
Self {
ctr: AtomicU64::new(0),
}
}
}
impl OnEvictCallback for EvictedCounter {
fn on_evict<K, V>(&self, _: &K, _: &V) {
self.ctr.fetch_add(1, Ordering::SeqCst);
}
}
let mut cache: RawLRU<isize, &str, EvictedCounter> = RawLRU::with_on_evict_cb(10, EvictedCounter::default()).unwrap();sourceimpl<K: Hash + Eq, V, E: OnEvictCallback, S: BuildHasher> RawLRU<K, V, E, S>
impl<K: Hash + Eq, V, E: OnEvictCallback, S: BuildHasher> RawLRU<K, V, E, S>
sourcepub fn with_on_evict_cb_and_hasher(
cap: usize,
cb: E,
hasher: S
) -> Result<Self, CacheError>
pub fn with_on_evict_cb_and_hasher(
cap: usize,
cb: E,
hasher: S
) -> Result<Self, CacheError>
Creates a new LRU Cache that holds at most cap items and
uses the provided evict callback and the provided hash builder to hash keys.
Example
use caches::{RawLRU, OnEvictCallback, DefaultHashBuilder};
use std::sync::atomic::{AtomicU64, Ordering};
struct EvictedCounter {
ctr: AtomicU64,
}
impl Default for EvictedCounter {
fn default() -> Self {
Self {
ctr: AtomicU64::new(0),
}
}
}
impl OnEvictCallback for EvictedCounter {
fn on_evict<K, V>(&self, _: &K, _: &V) {
self.ctr.fetch_add(1, Ordering::SeqCst);
}
}
let cache: RawLRU<isize, &str, EvictedCounter, DefaultHashBuilder> = RawLRU::with_on_evict_cb_and_hasher(10, EvictedCounter::default(), DefaultHashBuilder::default()).unwrap();sourcepub fn get_lru(&mut self) -> Option<(&K, &V)>
pub fn get_lru(&mut self) -> Option<(&K, &V)>
Returns the least recent used entry(&K, &V) in the cache or None if the cache is empty.
Example
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
cache.put("apple", 8);
cache.put("banana", 4);
cache.put("banana", 6);
cache.put("pear", 2);
assert_eq!(cache.get_lru(), Some((&"banana", &6)));sourcepub fn get_mru(&self) -> Option<(&K, &V)>
pub fn get_mru(&self) -> Option<(&K, &V)>
Returns the most recent used entry(&K, &V) in the cache or None if the cache is empty.
Example
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
cache.put("apple", 8);
cache.put("banana", 4);
cache.put("banana", 6);
cache.put("pear", 2);
assert_eq!(cache.get_mru(), Some((&"pear", &2)));sourcepub fn get_lru_mut(&mut self) -> Option<(&K, &mut V)>
pub fn get_lru_mut(&mut self) -> Option<(&K, &mut V)>
Returns the least recent used entry(&K, &mut V) in the cache or None if the cache is empty.
Example
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
cache.put("apple", 8);
cache.put("banana", 4);
cache.put("banana", 6);
cache.put("pear", 2);
assert_eq!(cache.get_lru_mut(), Some((&"banana", &mut 6)));sourcepub fn get_mru_mut(&mut self) -> Option<(&K, &mut V)>
pub fn get_mru_mut(&mut self) -> Option<(&K, &mut V)>
Returns the most recent used entry (&K, &mut V) in the cache or None if the cache is empty.
Example
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
cache.put("apple", 8);
cache.put("banana", 4);
cache.put("banana", 6);
cache.put("pear", 2);
assert_eq!(cache.get_mru_mut(), Some((&"pear", &mut 2)));sourcepub fn peek_or_put(&mut self, k: K, v: V) -> (Option<&V>, Option<PutResult<K, V>>)
pub fn peek_or_put(&mut self, k: K, v: V) -> (Option<&V>, Option<PutResult<K, V>>)
peek_or_put peeks if a key is in the cache without updating the
recent-ness or deleting it for being stale, and if not, adds the value.
Returns whether found and whether a PutResult.
Example
use caches::{RawLRU, PutResult, Cache};
let mut cache = RawLRU::new(2).unwrap();
cache.put(1, "a");
cache.put(2, "b");
cache.put(3, "c");
assert_eq!(cache.peek_or_put(2, "B"), (Some(&"b"), None));
assert_eq!(cache.peek_or_put(1, "A"), (None, Some(PutResult::Evicted { key: 2, value: "b",})));sourcepub fn peek_mut_or_put(
&mut self,
k: K,
v: V
) -> (Option<&mut V>, Option<PutResult<K, V>>)
pub fn peek_mut_or_put(
&mut self,
k: K,
v: V
) -> (Option<&mut V>, Option<PutResult<K, V>>)
peek_mut_or_put peeks if a key is in the cache without updating the
recent-ness or deleting it for being stale, and if not, adds the value.
Returns whether found and whether a PutResult.
Example
use caches::{RawLRU, PutResult, Cache};
let mut cache = RawLRU::new(2).unwrap();
cache.put(1, "a");
cache.put(2, "b");
cache.put(3, "c");
assert_eq!(cache.peek_mut_or_put(2, "B"), (Some(&mut "b"), None));
assert_eq!(cache.peek_mut_or_put(1, "A"), (None, Some(PutResult::Evicted { key: 2, value: "b",})));sourcepub fn peek_lru(&self) -> Option<(&K, &V)>
pub fn peek_lru(&self) -> Option<(&K, &V)>
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 caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
cache.put(1, "a");
cache.put(2, "b");
assert_eq!(cache.peek_lru(), Some((&1, &"a")));sourcepub fn peek_lru_mut<'a>(&mut self) -> Option<(&'a K, &'a mut V)>
pub fn peek_lru_mut<'a>(&mut self) -> Option<(&'a K, &'a mut V)>
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 caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
cache.put(1, "a");
cache.put(2, "b");
assert_eq!(cache.peek_lru_mut(), Some((&1, &mut "a")));sourcepub fn peek_mru(&self) -> Option<(&K, &V)>
pub fn peek_mru(&self) -> Option<(&K, &V)>
Returns the value corresponding to the most 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 caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
cache.put(1, "a");
cache.put(2, "b");
assert_eq!(cache.peek_mru(), Some((&2, &"b")));sourcepub fn peek_mru_mut(&mut self) -> Option<(&K, &mut V)>
pub fn peek_mru_mut(&mut self) -> Option<(&K, &mut V)>
Returns the mutable value corresponding to the most 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 caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
cache.put(1, "a");
cache.put(2, "b");
assert_eq!(cache.peek_mru_mut(), Some((&2, &mut "b")));sourcepub fn contains_or_put(&mut self, k: K, v: V) -> (bool, Option<PutResult<K, V>>)
pub fn contains_or_put(&mut self, k: K, v: V) -> (bool, Option<PutResult<K, V>>)
contains_or_put checks if a key is in the cache without updating the
recent-ness or deleting it for being stale, and if not, adds the value.
Returns whether found and whether a PutResult.
Example
use caches::{RawLRU, PutResult, Cache};
let mut cache = RawLRU::new(2).unwrap();
cache.put(1, "a");
cache.put(2, "b");
cache.put(3, "c");
assert_eq!(cache.contains_or_put(2, "B"), (true, None));
assert_eq!(cache.contains_or_put(1, "A"), (false, Some(PutResult::Evicted { key: 2, value: "b",})));sourcepub fn remove_lru(&mut self) -> Option<(K, V)>
pub fn remove_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 caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
cache.put(2, "a");
cache.put(3, "b");
cache.put(4, "c");
cache.get(&3);
assert_eq!(cache.remove_lru(), Some((4, "c")));
assert_eq!(cache.remove_lru(), Some((3, "b")));
assert_eq!(cache.remove_lru(), None);
assert_eq!(cache.len(), 0);sourcepub fn keys(&self) -> KeysMRUIter<'_, K, V>ⓘ
pub fn keys(&self) -> KeysMRUIter<'_, K, V>ⓘ
An iterator visiting all keys in most-recently used order. The iterator element type is
&'a K.
Examples
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(3).unwrap();
cache.put("a", 1);
cache.put("b", 2);
cache.put("c", 3);
for key in cache.keys() {
println!("key: {}", key);
}sourcepub fn keys_lru(&self) -> KeysLRUIter<'_, K, V>ⓘ
pub fn keys_lru(&self) -> KeysLRUIter<'_, K, V>ⓘ
An iterator visiting all keys in less-recently used order. The iterator element type is
&'a K.
Examples
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(3).unwrap();
cache.put("a", 1);
cache.put("b", 2);
cache.put("c", 3);
for key in cache.keys_lru() {
println!("key: {}", key);
}sourcepub fn values(&self) -> ValuesMRUIter<'_, K, V>ⓘ
pub fn values(&self) -> ValuesMRUIter<'_, K, V>ⓘ
An iterator visiting all values in most-recently used order. The iterator element type is
&'a V.
Examples
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(3).unwrap();
cache.put("a", 1);
cache.put("b", 2);
cache.put("c", 3);
for val in cache.values() {
println!("val: {}", val);
}sourcepub fn values_lru<'a>(&self) -> ValuesLRUIter<'a, K, V>ⓘ
pub fn values_lru<'a>(&self) -> ValuesLRUIter<'a, K, V>ⓘ
An iterator visiting all values in less-recently used order. The iterator element type is
&'a V.
Examples
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(3).unwrap();
cache.put("a", 1);
cache.put("b", 2);
cache.put("c", 3);
for val in cache.values_lru() {
println!("val: {}", val);
}sourcepub fn values_mut<'a>(&mut self) -> ValuesMRUIterMut<'a, K, V>ⓘ
pub fn values_mut<'a>(&mut self) -> ValuesMRUIterMut<'a, K, V>ⓘ
An iterator visiting all values in most-recently used order. The iterator element type is
&'a mut V.
Examples
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(3).unwrap();
cache.put("a", 1);
cache.put("b", 2);
cache.put("c", 3);
for val in cache.values_mut() {
println!("val: {}", val);
}sourcepub fn values_lru_mut<'a>(&mut self) -> ValuesLRUIterMut<'a, K, V>ⓘ
pub fn values_lru_mut<'a>(&mut self) -> ValuesLRUIterMut<'a, K, V>ⓘ
An iterator visiting all values in less-recently used order. The iterator element type is
&'a mut V.
Examples
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(3).unwrap();
cache.put("a", 1);
cache.put("b", 2);
cache.put("c", 3);
for val in cache.values_lru_mut() {
println!("val: {}", val);
}sourcepub fn iter<'a>(&self) -> MRUIter<'a, K, V>ⓘ
pub fn iter<'a>(&self) -> MRUIter<'a, K, V>ⓘ
An iterator visiting all entries in most-recently used order. The iterator element type is
(&'a K, &'a V).
Examples
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(3).unwrap();
cache.put("a", 1);
cache.put("b", 2);
cache.put("c", 3);
for (key, val) in cache.iter() {
println!("key: {} val: {}", key, val);
}sourcepub fn iter_lru<'a>(&self) -> LRUIter<'a, K, V>ⓘ
pub fn iter_lru<'a>(&self) -> LRUIter<'a, K, V>ⓘ
An iterator visiting all entries in less-recently used order. The iterator element type is
(&'a K, &'a V).
Examples
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(3).unwrap();
cache.put("a", 1);
cache.put("b", 2);
cache.put("c", 3);
for (key, val) in cache.iter_lru() {
println!("key: {} val: {}", key, val);
}sourcepub fn iter_mut<'a>(&mut self) -> MRUIterMut<'a, K, V>ⓘ
pub fn iter_mut<'a>(&mut self) -> MRUIterMut<'a, K, V>ⓘ
An iterator visiting all entries in most-recently-used order, giving a mutable reference on
V. The iterator element type is (&'a K, &'a mut V).
Examples
use caches::{Cache, RawLRU};
struct HddBlock {
dirty: bool,
data: [u8; 512]
}
let mut cache = RawLRU::new(3).unwrap();
cache.put(0, HddBlock { dirty: false, data: [0x00; 512]});
cache.put(1, HddBlock { dirty: true, data: [0x55; 512]});
cache.put(2, HddBlock { dirty: true, data: [0x77; 512]});
// write dirty blocks to disk.
for (block_id, block) in cache.iter_mut() {
if block.dirty {
// write block to disk
block.dirty = false
}
}sourcepub fn iter_lru_mut<'a>(&mut self) -> LRUIterMut<'a, K, V>ⓘ
pub fn iter_lru_mut<'a>(&mut self) -> LRUIterMut<'a, K, V>ⓘ
An iterator visiting all entries in less-recently-used order, giving a mutable reference on
V. The iterator element type is (&'a K, &'a mut V).
Examples
use caches::{Cache, RawLRU};
struct HddBlock {
dirty: bool,
data: [u8; 512]
}
let mut cache = RawLRU::new(3).unwrap();
cache.put(0, HddBlock { dirty: false, data: [0x00; 512]});
cache.put(1, HddBlock { dirty: true, data: [0x55; 512]});
cache.put(2, HddBlock { dirty: true, data: [0x77; 512]});
// write dirty blocks to disk.
for (block_id, block) in cache.iter_lru_mut() {
if block.dirty {
// write block to disk
block.dirty = false
}
}Trait Implementations
sourceimpl<K: Hash + Eq, V, E: OnEvictCallback, S: BuildHasher> Cache<K, V> for RawLRU<K, V, E, S>
impl<K: Hash + Eq, V, E: OnEvictCallback, S: BuildHasher> Cache<K, V> for RawLRU<K, V, E, S>
sourcefn put(&mut self, k: K, v: V) -> PutResult<K, V>
fn put(&mut self, k: K, v: V) -> PutResult<K, V>
Puts a key-value pair into cache, returns a PutResult.
Example
use caches::{RawLRU, PutResult, Cache};
let mut cache = RawLRU::new(2).unwrap();
assert_eq!(PutResult::Put, cache.put(1, "a"));
assert_eq!(PutResult::Put, cache.put(2, "b"));
assert_eq!(PutResult::Update("b"), cache.put(2, "beta"));
assert_eq!(PutResult::Evicted{ key: 1, value: "a"}, cache.put(3, "c"));
assert_eq!(cache.get(&1), None);
assert_eq!(cache.get(&2), Some(&"beta"));sourcefn get<'a, Q>(&mut self, k: &'a Q) -> Option<&'a V>where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
fn get<'a, Q>(&mut self, k: &'a Q) -> Option<&'a V>where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
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 caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
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"));sourcefn get_mut<'a, Q>(&mut self, k: &'a Q) -> Option<&'a mut V>where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
fn get_mut<'a, Q>(&mut self, k: &'a Q) -> Option<&'a mut V>where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
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 caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
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));sourcefn peek<'a, Q>(&self, k: &'a Q) -> Option<&'a V>where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
fn peek<'a, Q>(&self, k: &'a Q) -> Option<&'a V>where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
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 caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
cache.put(1, "a");
cache.put(2, "b");
assert_eq!(cache.peek(&1), Some(&"a"));
assert_eq!(cache.peek(&2), Some(&"b"));sourcefn peek_mut<'a, Q>(&mut self, k: &'a Q) -> Option<&'a mut V>where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
fn peek_mut<'a, Q>(&mut self, k: &'a Q) -> Option<&'a mut V>where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
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 caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
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"));sourcefn contains<Q>(&self, k: &Q) -> boolwhere
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
fn contains<Q>(&self, k: &Q) -> boolwhere
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Returns a bool indicating whether the given key is in the cache. Does not update the LRU list.
Example
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
cache.put(1, "a");
cache.put(2, "b");
cache.put(3, "c");
assert!(!cache.contains(&1));
assert!(cache.contains(&2));
assert!(cache.contains(&3));sourcefn remove<Q>(&mut self, k: &Q) -> Option<V>where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
fn remove<Q>(&mut self, k: &Q) -> Option<V>where
KeyRef<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Removes and returns the value corresponding to the key from the cache or
None if it does not exist.
Example
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
cache.put(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);sourcefn purge(&mut self)
fn purge(&mut self)
Clears the contents of the cache.
Example
use caches::{Cache, RawLRU};
let mut cache: RawLRU<isize, &str> = RawLRU::new(2).unwrap();
assert_eq!(cache.len(), 0);
cache.put(1, "a");
assert_eq!(cache.len(), 1);
cache.put(2, "b");
assert_eq!(cache.len(), 2);
cache.purge();
assert_eq!(cache.len(), 0);sourcefn len(&self) -> usize
fn len(&self) -> usize
Returns the number of key-value pairs that are currently in the the cache.
Example
use caches::{Cache, RawLRU};
let mut cache = RawLRU::new(2).unwrap();
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);sourceimpl<K: Hash + Eq, V, E: OnEvictCallback, S: BuildHasher> Debug for RawLRU<K, V, E, S>
impl<K: Hash + Eq, V, E: OnEvictCallback, S: BuildHasher> Debug for RawLRU<K, V, E, S>
sourceimpl<K: Hash + Eq, V> From<BinaryHeap<(K, V)>> for RawLRU<K, V>
impl<K: Hash + Eq, V> From<BinaryHeap<(K, V)>> for RawLRU<K, V>
sourcefn from(vals: BinaryHeap<(K, V)>) -> Self
fn from(vals: BinaryHeap<(K, V)>) -> Self
sourceimpl<K: Hash + Eq, V> From<LinkedList<(K, V)>> for RawLRU<K, V>
impl<K: Hash + Eq, V> From<LinkedList<(K, V)>> for RawLRU<K, V>
sourcefn from(vals: LinkedList<(K, V)>) -> Self
fn from(vals: LinkedList<(K, V)>) -> Self
sourceimpl<K: Hash + Eq, V> FromIterator<(K, V)> for RawLRU<K, V>
impl<K: Hash + Eq, V> FromIterator<(K, V)> for RawLRU<K, V>
sourcefn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self
sourceimpl<'a, K: Hash + Eq, V, E: OnEvictCallback, S: BuildHasher> IntoIterator for &'a RawLRU<K, V, E, S>
impl<'a, K: Hash + Eq, V, E: OnEvictCallback, S: BuildHasher> IntoIterator for &'a RawLRU<K, V, E, S>
sourceimpl<'a, K: Hash + Eq, V, E: OnEvictCallback, S: BuildHasher> IntoIterator for &'a mut RawLRU<K, V, E, S>
impl<'a, K: Hash + Eq, V, E: OnEvictCallback, S: BuildHasher> IntoIterator for &'a mut RawLRU<K, V, E, S>
type IntoIter = MRUIterMut<'a, K, V>
type IntoIter = MRUIterMut<'a, K, V>
sourcefn into_iter(self) -> MRUIterMut<'a, K, V>ⓘ
fn into_iter(self) -> MRUIterMut<'a, K, V>ⓘ
sourceimpl<K: Hash + Eq, V, E: OnEvictCallback, S: BuildHasher> ResizableCache for RawLRU<K, V, E, S>
impl<K: Hash + Eq, V, E: OnEvictCallback, S: BuildHasher> ResizableCache for RawLRU<K, V, E, S>
sourcefn resize(&mut self, cap: usize) -> u64
fn resize(&mut self, cap: usize) -> u64
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 caches::{Cache, RawLRU, ResizableCache};
let mut cache: RawLRU<isize, &str> = RawLRU::new(2).unwrap();
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"));