[][src]Struct elaru::LRUCache

pub struct LRUCache<T> { /* fields omitted */ }

A LRU cache builds on top of the HashMap from standard library.

LRUCache uses std::collections::HashMap for storage. It provides O(1) performance on insert, get, remove_lru and many other APIs.

All entries are linked inlined within the LRUCache without raw pointer manipulation, so it is complete memory safe and doesn't suffer any undefined behavior. 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 elaru::{LRUCache, Entry};

// Create an empty cache, then insert some items.
let mut cache = LRUCache::new(3);
cache.insert(1, "Mercury");
cache.insert(2, "Venus");
cache.insert(3, "Earth");

// Use the `get` method to retrieve the value from the cache with given key.
// This also "touches" the entry, marking it most-recently-used.
let item = cache.get(&1).unwrap();
assert_eq!(item, &"Mercury");

// If the cache is full, inserting a new item evicts the least-recently-used item:
cache.insert(4, "Mars");
assert!(cache.get(&2).is_none());

Implementations

impl<T> LRUCache<T>[src]

pub fn new(capacity: usize) -> Self[src]

Create a new LRU cache that can hold capacity of entries.

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

Returns the number of elements in the cache.

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

Returns the capacity of the cache.

pub fn get(&mut self, key: &u16) -> Option<&T>[src]

Returns the entry in the list with given key.

pub fn get_mut(&mut self, key: &u16) -> Option<&mut T>[src]

Returns a mutable reference to the entry in the list with given key.

pub fn insert(&mut self, key: u16, val: T) -> Option<T>[src]

Insert a given key in the cache. Return old value if the key is present.

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 remove_lru(&mut self) -> Option<(u16, T)>[src]

Remove an entry from the linked list.

pub fn clear(&mut self)[src]

Clear all elements from the cache.

pub fn iter(&self) -> Iter<'_, T>

Notable traits for Iter<'a, T>

impl<'a, T> Iterator for Iter<'a, T> where
    T: 'a, 
type Item = (u16, &'a T);
[src]

Iterate over the contents of this cache.

Trait Implementations

impl<T: Clone> Clone for LRUCache<T>[src]

impl<T: Debug> Debug for LRUCache<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for LRUCache<T> where
    T: RefUnwindSafe
[src]

impl<T> Send for LRUCache<T> where
    T: Send
[src]

impl<T> Sync for LRUCache<T> where
    T: Sync
[src]

impl<T> Unpin for LRUCache<T> where
    T: Unpin
[src]

impl<T> UnwindSafe for LRUCache<T> where
    T: UnwindSafe
[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> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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.