[−][src]Struct elaru::LRUCache
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>ⓘ
[src]
Iterate over the contents of this cache.
Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for LRUCache<T> where
T: RefUnwindSafe,
[src]
T: RefUnwindSafe,
impl<T> Send for LRUCache<T> where
T: Send,
[src]
T: Send,
impl<T> Sync for LRUCache<T> where
T: Sync,
[src]
T: Sync,
impl<T> Unpin for LRUCache<T> where
T: Unpin,
[src]
T: Unpin,
impl<T> UnwindSafe for LRUCache<T> where
T: UnwindSafe,
[src]
T: UnwindSafe,
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> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
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>,