pub trait Cache<K, V> {
    type Error;

    // Required methods
    fn insert(
        &mut self,
        key: K,
        value: V
    ) -> Result<Eviction<K, V>, Self::Error>;
    fn remove(&mut self, key: &K) -> Result<Lookup<V>, Self::Error>;
    fn shrink(&mut self, new_capacity: usize) -> Result<(), Self::Error>;
    fn reserve(&mut self, additional: usize) -> Result<(), Self::Error>;
    fn query(&mut self, key: &K) -> Result<Lookup<&V>, Self::Error>;
    fn capacity(&self) -> usize;
    fn len(&self) -> usize;
    fn clear(&mut self) -> Result<(), Self::Error>;

    // Provided methods
    fn is_maxed(&self) -> bool { ... }
    fn is_empty(&self) -> bool { ... }
}
Expand description

A size bounded map, where certain existing entries are evicted to make space for new entries.

Implementations follow a well defined criteria to decide which cache blocks to evict in which order. (e.g an LRU cache implementation would evict the least recently used cache blocks).

Required Associated Types§

source

type Error

Associated error type.

Required Methods§

source

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

Inserts the given key/value pair into this cache.

source

fn remove(&mut self, key: &K) -> Result<Lookup<V>, 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<&V>, 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 clear(&mut self) -> Result<(), Self::Error>

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

Provided Methods§

source

fn is_maxed(&self) -> bool

Returns whether this cache is maxed out.

This method simply checks whether the current length of this cache equals its capacity.

source

fn is_empty(&self) -> bool

Returns whether this cache is empty.

Implementors§

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,

§

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