Skip to main content

MutableCache

Trait MutableCache 

Source
pub trait MutableCache<K, V>: CoreCache<K, V> {
    // Required method
    fn remove(&mut self, key: &K) -> Option<V>;

    // Provided method
    fn remove_batch(&mut self, keys: &[K]) -> Vec<Option<V>> { ... }
}
Expand description

Caches that support arbitrary key-based removal.

This trait extends CoreCache with the ability to remove entries by key. Appropriate for LRU, LFU, and general hash-map style caches where arbitrary removal doesn’t violate policy semantics.

Note: FIFO caches intentionally do NOT implement this trait because arbitrary removal would violate FIFO semantics. Use FifoCacheTrait instead.

§Example

use cachekit::traits::{CoreCache, MutableCache, ReadOnlyCache};
use cachekit::policy::lru_k::LrukCache;

fn invalidate_keys<C: MutableCache<u64, String>>(cache: &mut C, keys: &[u64]) {
    for key in keys {
        cache.remove(key);
    }
}

let mut cache = LrukCache::new(100);
cache.insert(1, "one".to_string());
cache.insert(2, "two".to_string());
cache.insert(3, "three".to_string());

invalidate_keys(&mut cache, &[1, 3]);
assert!(!cache.contains(&1));
assert!(cache.contains(&2));
assert!(!cache.contains(&3));

Required Methods§

Source

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

Removes a specific key-value pair.

Returns the removed value if the key existed, or None if it didn’t.

§Example
use cachekit::traits::{CoreCache, MutableCache, ReadOnlyCache};
use cachekit::policy::lru_k::LrukCache;

let mut cache = LrukCache::new(10);
cache.insert(1, "value");

assert_eq!(cache.remove(&1), Some("value"));
assert_eq!(cache.remove(&1), None);  // Already removed

Provided Methods§

Source

fn remove_batch(&mut self, keys: &[K]) -> Vec<Option<V>>

Removes multiple keys efficiently.

Returns a vector of Option<V> in the same order as the input keys. The default implementation loops over remove.

§Example
use cachekit::traits::{CoreCache, MutableCache, ReadOnlyCache};
use cachekit::policy::lru_k::LrukCache;

let mut cache = LrukCache::new(10);
cache.insert(1, "one");
cache.insert(2, "two");
cache.insert(3, "three");

let removed = cache.remove_batch(&[1, 99, 3]);
assert_eq!(removed, vec![Some("one"), None, Some("three")]);
assert_eq!(cache.len(), 1);

Implementors§

Source§

impl<H, V> MutableCache<H, Arc<V>> for LFUHandleCache<H, V>
where H: Eq + Hash + Copy,

Mutable cache operations for handle-based LFU.

§Example

use cachekit::policy::lfu::LFUHandleCache;
use cachekit::traits::{CoreCache, MutableCache};
use std::sync::Arc;

let mut cache: LFUHandleCache<u64, i32> = LFUHandleCache::new(10);
cache.insert(1u64, Arc::new(42));

let removed = cache.remove(&1u64);
assert_eq!(*removed.unwrap(), 42);
Source§

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

Mutable cache operations for heap-based LFU.

§Example

use cachekit::policy::heap_lfu::HeapLfuCache;
use cachekit::traits::{CoreCache, MutableCache};
use std::sync::Arc;

let mut cache: HeapLfuCache<&str, i32> = HeapLfuCache::new(10);
cache.insert("key", Arc::new(42));

let removed = cache.remove(&"key");
assert_eq!(*removed.unwrap(), 42);
assert!(!cache.contains(&"key"));
Source§

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

Mutable cache operations for LFU.

§Example

use cachekit::policy::lfu::LfuCache;
use cachekit::traits::{CoreCache, MutableCache, ReadOnlyCache};
use std::sync::Arc;

let mut cache: LfuCache<&str, i32> = LfuCache::new(10);
cache.insert("key", Arc::new(42));

let removed = cache.remove(&"key");
assert_eq!(*removed.unwrap(), 42);
assert!(!cache.contains(&"key"));
Source§

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

Source§

impl<K, V> MutableCache<K, V> for ARCCore<K, V>
where K: Clone + Eq + Hash,

Source§

impl<K, V> MutableCache<K, V> for CARCore<K, V>
where K: Clone + Eq + Hash,

Source§

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

Source§

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

Source§

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

Mutable cache operations for LRU-K.

§Example

use cachekit::policy::lru_k::LrukCache;
use cachekit::traits::{CoreCache, MutableCache, ReadOnlyCache};

let mut cache: LrukCache<u32, String> = LrukCache::new(10);
cache.insert(1, "value".to_string());

// Remove an entry
let removed = cache.remove(&1);
assert_eq!(removed.as_deref(), Some("value"));
assert!(!cache.contains(&1));

// Remove non-existent key
assert!(cache.remove(&999).is_none());
Source§

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

Source§

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