pub struct LruCache<K: Eq + Hash, V, S: BuildHasher = RandomState, M: CountableMeter<K, V> = Count> { /* private fields */ }
Expand description
An LRU cache.
Implementations
sourceimpl<K: Eq + Hash, V, M: CountableMeter<K, V>> LruCache<K, V, RandomState, M>
impl<K: Eq + Hash, V, M: CountableMeter<K, V>> LruCache<K, V, RandomState, M>
sourcepub fn with_meter(capacity: u64, meter: M) -> LruCache<K, V, RandomState, M>
pub fn with_meter(capacity: u64, meter: M) -> LruCache<K, V, RandomState, M>
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);
sourceimpl<K: Eq + Hash, V, S: BuildHasher> LruCache<K, V, S, Count>
impl<K: Eq + Hash, V, S: BuildHasher> LruCache<K, V, S, Count>
sourcepub fn with_hasher(capacity: u64, hash_builder: S) -> LruCache<K, V, S, Count>
pub fn with_hasher(capacity: u64, hash_builder: S) -> LruCache<K, V, S, Count>
Creates an empty cache that can hold at most capacity
items with the given hash builder.
sourcepub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Hash + Eq,
pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Hash + Eq,
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"));
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V>ⓘNotable traits for IterMut<'a, K, V>impl<'a, K, V> Iterator for IterMut<'a, K, V> type Item = (&'a K, &'a mut V);
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>ⓘNotable traits for IterMut<'a, K, V>impl<'a, K, V> Iterator for IterMut<'a, K, V> type Item = (&'a K, &'a mut V);
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));
sourceimpl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> LruCache<K, V, S, M>
impl<K: Eq + Hash, V, S: BuildHasher, M: CountableMeter<K, V>> LruCache<K, V, S, M>
sourcepub fn with_meter_and_hasher(capacity: u64, meter: M, hash_builder: S) -> Self
pub fn with_meter_and_hasher(capacity: u64, meter: M, hash_builder: S) -> Self
Creates an empty cache that can hold at most capacity
as measured by meter
with the
given hash builder.
sourcepub fn capacity(&self) -> u64
pub fn capacity(&self) -> u64
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);
sourcepub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where
K: Borrow<Q>,
Q: Hash + Eq,
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where
K: Borrow<Q>,
Q: Hash + Eq,
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));
pub fn get<Q: ?Sized>(&mut self, k: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Hash + Eq,
sourcepub fn insert(&mut self, k: K, v: V) -> Option<V>
pub fn insert(&mut self, k: K, v: V) -> Option<V>
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"));
sourcepub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Hash + Eq,
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Hash + Eq,
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);
sourcepub fn set_capacity(&mut self, capacity: u64)
pub fn set_capacity(&mut self, capacity: u64)
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"));
sourcepub fn remove_lru(&mut self) -> Option<(K, V)>
pub fn remove_lru(&mut self) -> Option<(K, V)>
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);
sourcepub fn size(&self) -> u64
pub fn size(&self) -> u64
Returns the size of all the key-value pairs in the cache, as measured by the Meter
used
by the cache.
sourcepub fn iter(&self) -> Iter<'_, K, V>ⓘNotable traits for Iter<'a, K, V>impl<'a, K, V> Iterator for Iter<'a, K, V> type Item = (&'a K, &'a V);
pub fn iter(&self) -> Iter<'_, K, V>ⓘNotable traits for Iter<'a, K, V>impl<'a, K, V> Iterator for Iter<'a, K, V> type Item = (&'a K, &'a V);
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
sourceimpl<K: Clone + Eq + Hash, V: Clone, S: Clone + BuildHasher, M: Clone + CountableMeter<K, V>> Clone for LruCache<K, V, S, M> where
M::Measure: Clone,
impl<K: Clone + Eq + Hash, V: Clone, S: Clone + BuildHasher, M: Clone + CountableMeter<K, V>> Clone for LruCache<K, V, S, M> where
M::Measure: Clone,
sourceimpl<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>
sourceimpl<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>
sourcefn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<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>
sourceimpl<'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>
sourceimpl<'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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more