Struct uluru::LRUCache [−][src]
A LRU cache using a statically-sized array for storage.
LRUCache
uses a fixed-capacity array for storage. It provides O(1)
insertion, and O(n)
lookup.
All items are stored inline within the LRUCache
, so it does not impose any heap allocation or
indirection. A linked list is used to record the cache order, so the items themselves do not
need to be moved when the order changes. (This is important for speed if the items are large.)
Example
use uluru::{LRUCache, Entry}; struct MyValue { id: u32, name: &'static str, } // A cache with a capacity of three. type MyCache = LRUCache<[Entry<MyValue>; 3]>; // Create an empty cache, then insert some items. let mut cache = MyCache::default(); cache.insert(MyValue { id: 1, name: "Mercury" }); cache.insert(MyValue { id: 2, name: "Venus" }); cache.insert(MyValue { id: 3, name: "Earth" }); { // Use the `find` method to retrieve an item from the cache. // This also "touches" the item, marking it most-recently-used. let item = cache.find(|x| x.id == 1); assert_eq!(item.unwrap().name, "Mercury"); } // If the cache is full, inserting a new item evicts the least-recently-used item: cache.insert(MyValue { id: 4, name: "Mars" }); assert!(cache.find(|x| x.id == 2).is_none());
Implementations
impl<T, A> LRUCache<A> where
A: Array<Item = Entry<T>>,
[src]
A: Array<Item = Entry<T>>,
pub fn len(&self) -> usize
[src]
Returns the number of elements in the cache.
pub fn num_entries(&self) -> usize
[src]
Use the 'len' method instead.
Returns the number of elements in the cache.
pub fn front(&self) -> Option<&T>
[src]
Private implementation detail. Will be removed in a future release.
Returns the front entry in the list (most recently used).
pub fn front_mut(&mut self) -> Option<&mut T>
[src]
Private implementation detail. Will be removed in a future release.
Returns a mutable reference to the front entry in the list (most recently used).
pub fn lookup<F, R>(&mut self, mut test_one: F) -> Option<R> where
F: FnMut(&mut T) -> Option<R>,
[src]
F: FnMut(&mut T) -> Option<R>,
Performs a lookup on the cache with the given test routine. Touches the result on a hit.
pub fn touch<F>(&mut self, mut pred: F) -> bool where
F: FnMut(&T) -> bool,
[src]
F: FnMut(&T) -> bool,
Touches the first item in the cache that matches the given predicate.
Returns true
on a hit, false
if no matches.
pub fn find<F>(&mut self, pred: F) -> Option<&mut T> where
F: FnMut(&T) -> bool,
[src]
F: FnMut(&T) -> bool,
Returns the first item in the cache that matches the given predicate. Touches the result on a hit.
pub fn insert(&mut self, val: T)
[src]
Insert a given key in the cache.
This item becomes the front (most-recently-used) item in the cache. If the cache is full, the back (least-recently-used) item will be removed.
pub fn clear(&mut self)
[src]
Evict all elements from the cache.
pub fn evict_all(&mut self)
[src]
Use the 'clear' method instead.
Evict all elements from the cache.
Trait Implementations
impl<T, A> Clone for LRUCache<A> where
A: Array<Item = Entry<T>>,
T: Clone,
[src]
A: Array<Item = Entry<T>>,
T: Clone,
fn clone(&self) -> Self
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<T, A> Debug for LRUCache<A> where
A: Array<Item = Entry<T>>,
T: Debug,
[src]
A: Array<Item = Entry<T>>,
T: Debug,
impl<A: Array> Default for LRUCache<A>
[src]
Auto Trait Implementations
impl<A> Send for LRUCache<A> where
A: Send,
<A as Array>::Index: Send,
[src]
A: Send,
<A as Array>::Index: Send,
impl<A> Sync for LRUCache<A> where
A: Sync,
<A as Array>::Index: Sync,
[src]
A: Sync,
<A as Array>::Index: Sync,
impl<A> Unpin for LRUCache<A> where
A: Unpin,
<A as Array>::Index: Unpin,
[src]
A: Unpin,
<A as Array>::Index: Unpin,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,