Trait generational_cache::cache::Cache
source · 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§
Required Methods§
sourcefn insert(&mut self, key: K, value: V) -> Result<Eviction<K, V>, Self::Error>
fn insert(&mut self, key: K, value: V) -> Result<Eviction<K, V>, Self::Error>
Inserts the given key/value pair into this cache.
sourcefn remove(&mut self, key: &K) -> Result<Lookup<V>, Self::Error>
fn remove(&mut self, key: &K) -> Result<Lookup<V>, Self::Error>
Removes the key/value pair associated with the given key from this cache.
sourcefn shrink(&mut self, new_capacity: usize) -> Result<(), Self::Error>
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.
sourcefn reserve(&mut self, additional: usize) -> Result<(), Self::Error>
fn reserve(&mut self, additional: usize) -> Result<(), Self::Error>
Reserves additional memory to accomodate the given number of additional cache blocks.