LRUCache

Struct LRUCache 

Source
pub struct LRUCache<V, K, T, M> { /* private fields */ }
Expand description

A generational Arena backed LRU cache implementation.

This Cache implementation always evicts the least-recently-used (LRU) key/value pair. It uses a LinkedList for storing the underlying cache block entries to maintain the order in which they were inserted into the cache.

It uses a generational Arena for allocating the underlying LinkedList which stores the cache blocks. It uses a Map for maintaining the mapping from keys to the nodes storing the respective cache blocks in the LinkedList.

§Type parameters

  • V: Vector<LRUCacheBlockArenaEntry<K, T>> Used as the backing vector for the underlying Arena.
  • K The Key type.
  • V The Value type.
  • M: Map<K, Link> Used to store a mapping from the keys to links in the linked list.

Implementations§

Source§

impl<V, K, T, M> LRUCache<V, K, T, M>
where V: Vector<LRUCacheBlockArenaEntry<K, T>>, M: Map<K, Link>,

Source

pub fn least_recent(&self) -> Option<(&K, &T)>

Returns the least recently used key/value pair.

Source

pub fn most_recent(&self) -> Option<(&K, &T)>

Returns the most recently used key/value pair.

Source§

impl<V, K, T, M> LRUCache<V, K, T, M>
where V: Vector<LRUCacheBlockArenaEntry<K, T>>, M: Map<K, Link>,

Source

pub fn with_backing_vector_and_map(vector: V, map: M) -> Self

Creates an LRUCache instance with the given the backing Vector and Map implementation instances.

Source§

impl<V, K, T, M> LRUCache<V, K, T, M>
where V: Vector<LRUCacheBlockArenaEntry<K, T>>, M: Map<K, Link> + Default,

Source

pub fn with_backing_vector(vector: V) -> Self

Creates an LRUCache instance with the given Vector implementation instance and the default Map implementation value.

Trait Implementations§

Source§

impl<V, K, T, M> Cache<K, T> for LRUCache<V, K, T, M>
where V: Vector<LRUCacheBlockArenaEntry<K, T>>, M: Map<K, Link>, K: Copy,

Source§

type Error = LRUCacheError<<V as Vector<Entry<Node<Block<K, T>>>>>::Error, <M as Map<K, Link>>::Error>

Associated error type.
Source§

fn insert(&mut self, key: K, value: T) -> Result<Eviction<K, T>, Self::Error>

Inserts the given key/value pair into this cache.
Source§

fn remove(&mut self, key: &K) -> Result<Lookup<T>, Self::Error>

Removes the key/value pair associated with the given key from this cache.
Source§

fn shrink(&mut self, new_capacity: usize) -> Result<(), Self::Error>

Removes (self.len() - new_capacity) cache blocks to fit the new capacity. If the difference is non-positive no cache blocks are removed.
Source§

fn reserve(&mut self, additional: usize) -> Result<(), Self::Error>

Reserves additional memory to accomodate the given number of additional cache blocks.
Source§

fn query(&mut self, key: &K) -> Result<Lookup<&T>, Self::Error>

Queries this cache to find the value associated with given key.
Source§

fn capacity(&self) -> usize

Returns the current capacity of this cache.
Source§

fn len(&self) -> usize

Returns the number of key/value pairs stored in this cache.
Source§

fn is_empty(&self) -> bool

Returns whether this cache is empty.
Source§

fn clear(&mut self) -> Result<(), Self::Error>

Remove all items from this cache until it’s empty.
Source§

fn is_maxed(&self) -> bool

Returns whether this cache is maxed out. Read more
Source§

impl<V, K, T, M> Default for LRUCache<V, K, T, M>
where V: Vector<LRUCacheBlockArenaEntry<K, T>> + Default, M: Map<K, Link> + Default,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<V, K, T, M> Freeze for LRUCache<V, K, T, M>
where M: Freeze, V: Freeze,

§

impl<V, K, T, M> RefUnwindSafe for LRUCache<V, K, T, M>

§

impl<V, K, T, M> Send for LRUCache<V, K, T, M>
where M: Send, V: Send, K: Send, T: Send,

§

impl<V, K, T, M> Sync for LRUCache<V, K, T, M>
where M: Sync, V: Sync, K: Sync, T: Sync,

§

impl<V, K, T, M> Unpin for LRUCache<V, K, T, M>
where M: Unpin, V: Unpin, K: Unpin, T: Unpin,

§

impl<V, K, T, M> UnwindSafe for LRUCache<V, K, T, M>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.