Struct uluru::LRUCache[][src]

pub struct LRUCache<A: Array> { /* fields omitted */ }

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]

pub fn len(&self) -> usize[src]

Returns the number of elements in the cache.

pub fn num_entries(&self) -> usize[src]

👎 Deprecated:

Use the 'len' method instead.

Returns the number of elements in the cache.

pub fn front(&self) -> Option<&T>[src]

👎 Deprecated:

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]

👎 Deprecated:

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]

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]

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]

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]

👎 Deprecated:

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]

impl<T, A> Debug for LRUCache<A> where
    A: Array<Item = Entry<T>>,
    T: Debug
[src]

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]

impl<A> Sync for LRUCache<A> where
    A: Sync,
    <A as Array>::Index: Sync
[src]

impl<A> Unpin for LRUCache<A> where
    A: Unpin,
    <A as Array>::Index: Unpin
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.