Skip to main content

Cache

Trait Cache 

Source
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 require Eq + 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§

Source

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

Checks if a key exists without updating access state.

Source

fn len(&self) -> usize

Returns the current number of entries.

Source

fn capacity(&self) -> usize

Returns the maximum capacity of the cache.

Source

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

Side-effect-free lookup by key.

Does not update access patterns, eviction order, or any internal state.

Source

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.

Source

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.

Source

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

Removes a specific key-value pair, returning the value if it existed.

Source

fn clear(&mut self)

Removes all entries from the cache.

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns true if the cache contains no entries.

Implementors§

Source§

impl<H, V> Cache<H, Arc<V>> for LfuHandleCache<H, V>
where H: Eq + Hash + Copy,

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);
Source§

impl<K, V> Cache<K, Arc<V>> for HeapLfuCache<K, V>
where K: Eq + Hash + Clone + Ord,

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);
Source§

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

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);
Source§

impl<K, V> Cache<K, Arc<V>> for LruCore<K, V>
where K: Copy + Eq + Hash,

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

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);
Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

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"));
Source§

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

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"));