Skip to main content

Cache

Trait Cache 

Source
pub trait Cache<K, V>
where K: Eq + Hash, V: Clone,
{ // Required methods fn get(&self, key: &K) -> Option<V>; fn insert(&self, key: K, value: V) -> Option<V>; fn remove(&self, key: &K) -> Option<V>; fn contains_key(&self, key: &K) -> bool; fn len(&self) -> usize; fn clear(&self); fn capacity(&self) -> usize; // Provided method fn is_empty(&self) -> bool { ... } }
Expand description

The common read / write / evict contract every cache type in this crate implements.

All methods take &self (not &mut self) so a cache instance can be shared across threads and across .await points without external locking. Implementations use interior mutability.

§Access semantics

  • get is an access: it may update the eviction order (e.g. promoting the entry to most-recently-used).
  • contains_key is a query only: it must not update the eviction order.
  • insert is an access on the inserted key.
  • remove is destructive and does not update order.

§Example

use cache_mod::{Cache, LruCache};

let cache: LruCache<&'static str, u32> = LruCache::new(4).expect("capacity > 0");

assert_eq!(cache.insert("a", 1), None);
assert_eq!(cache.get(&"a"), Some(1));
assert!(cache.contains_key(&"a"));
assert_eq!(cache.len(), 1);

assert_eq!(cache.remove(&"a"), Some(1));
assert!(cache.is_empty());

Required Methods§

Source

fn get(&self, key: &K) -> Option<V>

Returns the value associated with key, if any, and counts as an access for the purposes of the eviction policy.

Source

fn insert(&self, key: K, value: V) -> Option<V>

Inserts value under key. Returns the previously-stored value if key was already present.

May evict one or more existing entries to make room, according to the cache’s eviction policy.

Source

fn remove(&self, key: &K) -> Option<V>

Removes the entry for key and returns the value if present.

Source

fn contains_key(&self, key: &K) -> bool

Returns true if the cache currently holds an entry for key.

Unlike get, this method does not count as an access — eviction order is left unchanged.

Source

fn len(&self) -> usize

Number of entries currently stored.

Source

fn clear(&self)

Removes every entry. Capacity is preserved.

Source

fn capacity(&self) -> usize

Configured capacity bound.

The unit depends on the implementation:

  • LruCache, LfuCache, TtlCache, TinyLfuCache — maximum number of entries.
  • SizedCache — maximum total byte-weight across entries.

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns true when the cache holds no entries.

Implementors§

Source§

impl<K, V> Cache<K, V> for LfuCache<K, V>
where K: Eq + Hash + Clone, V: Clone,

Available on crate feature std only.
Source§

impl<K, V> Cache<K, V> for LruCache<K, V>
where K: Eq + Hash + Clone, V: Clone,

Available on crate feature std only.
Source§

impl<K, V> Cache<K, V> for SizedCache<K, V>
where K: Eq + Hash + Clone, V: Clone,

Available on crate feature std only.
Source§

impl<K, V> Cache<K, V> for TinyLfuCache<K, V>
where K: Eq + Hash + Clone, V: Clone,

Available on crate feature std only.
Source§

impl<K, V> Cache<K, V> for TtlCache<K, V>
where K: Eq + Hash + Clone, V: Clone,

Available on crate feature std only.