pub trait Cache<K, V> {
// Required methods
fn contains(&self, key: &K) -> bool;
fn len(&self) -> usize;
fn capacity(&self) -> usize;
fn peek(&self, key: &K) -> Option<&V>;
fn get(&mut self, key: &K) -> Option<&V>;
fn insert(&mut self, key: K, value: V) -> Option<V>;
fn remove(&mut self, key: &K) -> Option<V>;
fn clear(&mut self);
// Provided method
fn is_empty(&self) -> bool { ... }
}Expand description
Universal cache operations that all policies implement.
This is the primary user-facing trait. Code written against Cache<K, V> can
swap eviction policies without changing call sites.
The trait is intentionally object-safe to support Box<dyn Cache<K, V>>.
§Type Parameters
K: Key type (implementations typically requireEq + Hash)V: Value type
§Example
use cachekit::traits::Cache;
use cachekit::policy::lru_k::LrukCache;
fn use_any_cache<C: Cache<u64, String>>(cache: &mut C) {
cache.insert(1, "hello".to_string());
assert_eq!(cache.peek(&1), Some(&"hello".to_string()));
assert_eq!(cache.get(&1), Some(&"hello".to_string()));
assert_eq!(cache.remove(&1), Some("hello".to_string()));
}
let mut cache = LrukCache::new(100);
use_any_cache(&mut cache);Required Methods§
Sourcefn peek(&self, key: &K) -> Option<&V>
fn peek(&self, key: &K) -> Option<&V>
Side-effect-free lookup by key.
Does not update access patterns, eviction order, or any internal state.
Sourcefn get(&mut self, key: &K) -> Option<&V>
fn get(&mut self, key: &K) -> Option<&V>
Policy-tracked lookup by key.
May update internal state (access time, frequency, reference bits)
depending on the eviction policy. Use peek if you
need a read without side effects.
Sourcefn insert(&mut self, key: K, value: V) -> Option<V>
fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair, returning the previous value if it existed.
If the cache is at capacity, an entry may be evicted according to the cache’s eviction policy before the new entry is inserted.
Provided Methods§
Implementors§
impl<H, V> Cache<H, Arc<V>> for LfuHandleCache<H, V>
Cache implementation for handle-based LFU.
§Example
use cachekit::policy::lfu::LfuHandleCache;
use cachekit::traits::Cache;
use std::sync::Arc;
let mut cache: LfuHandleCache<u64, i32> = LfuHandleCache::new(3);
cache.insert(1u64, Arc::new(100));
cache.insert(2u64, Arc::new(200));
assert_eq!(**cache.get(&1u64).unwrap(), 100);
assert!(cache.contains(&1u64));
assert_eq!(cache.len(), 2);impl<K, V> Cache<K, Arc<V>> for HeapLfuCache<K, V>
Cache implementation for heap-based LFU.
§Example
use cachekit::policy::heap_lfu::HeapLfuCache;
use cachekit::traits::Cache;
use std::sync::Arc;
let mut cache: HeapLfuCache<&str, i32> = HeapLfuCache::new(3);
cache.insert("a", Arc::new(1));
cache.insert("b", Arc::new(2));
assert_eq!(**cache.get(&"a").unwrap(), 1);
assert!(cache.contains(&"a"));
assert!(!cache.contains(&"z"));
assert_eq!(cache.len(), 2);
assert_eq!(cache.capacity(), 3);impl<K, V> Cache<K, Arc<V>> for LfuCache<K, V>
Cache implementation for LFU.
§Example
use cachekit::policy::lfu::LfuCache;
use cachekit::traits::Cache;
use std::sync::Arc;
let mut cache: LfuCache<&str, i32> = LfuCache::new(3);
cache.insert("a", Arc::new(1));
cache.insert("b", Arc::new(2));
assert_eq!(**cache.get(&"a").unwrap(), 1);
assert!(cache.contains(&"a"));
assert!(!cache.contains(&"z"));
assert_eq!(cache.len(), 2);
assert_eq!(cache.capacity(), 3);
cache.clear();
assert_eq!(cache.len(), 0);impl<K, V> Cache<K, Arc<V>> for LruCore<K, V>
impl<K, V> Cache<K, V> for ArcCore<K, V>
impl<K, V> Cache<K, V> for CarCore<K, V>
impl<K, V> Cache<K, V> for ClockCache<K, V>
impl<K, V> Cache<K, V> for ClockProCache<K, V>
impl<K, V> Cache<K, V> for FastLru<K, V>
impl<K, V> Cache<K, V> for FifoCache<K, V>
impl<K, V> Cache<K, V> for LifoCore<K, V>
impl<K, V> Cache<K, V> for LrukCache<K, V>
Cache implementation for LRU-K.
§Example
use cachekit::policy::lru_k::LrukCache;
use cachekit::traits::Cache;
let mut cache: LrukCache<u32, String> = LrukCache::new(3);
cache.insert(1, "one".to_string());
cache.insert(2, "two".to_string());
assert_eq!(cache.get(&1).map(|s| s.as_str()), Some("one"));
assert!(cache.contains(&1));
assert!(!cache.contains(&999));
assert_eq!(cache.len(), 2);
assert_eq!(cache.capacity(), 3);
cache.clear();
assert_eq!(cache.len(), 0);impl<K, V> Cache<K, V> for MfuCore<K, V>
impl<K, V> Cache<K, V> for MruCore<K, V>
impl<K, V> Cache<K, V> for NruCache<K, V>
impl<K, V> Cache<K, V> for RandomCore<K, V>
impl<K, V> Cache<K, V> for S3FifoCache<K, V>
impl<K, V> Cache<K, V> for SlruCore<K, V>
Implementation of the Cache trait for SLRU.
Allows SlruCore to be used through the unified cache interface.
§Example
use cachekit::traits::Cache;
use cachekit::policy::slru::SlruCore;
let mut cache: SlruCore<&str, i32> = SlruCore::new(100, 0.25);
// Use via Cache trait
cache.insert("key", 42);
assert_eq!(cache.get(&"key"), Some(&42));
assert!(cache.contains(&"key"));impl<K, V> Cache<K, V> for TwoQCore<K, V>
Implementation of the Cache trait for 2Q.
Allows TwoQCore to be used through the unified cache interface.
§Example
use cachekit::traits::Cache;
use cachekit::policy::two_q::TwoQCore;
let mut cache: TwoQCore<&str, i32> = TwoQCore::new(100, 0.25);
// Use via Cache trait
cache.insert("key", 42);
assert_eq!(cache.get(&"key"), Some(&42));
assert!(cache.contains(&"key"));