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§
Sourcefn remove(&mut self, key: &K) -> Option<V>
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 removedProvided Methods§
Sourcefn remove_batch(&mut self, keys: &[K]) -> Vec<Option<V>>
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§
impl<H, V> MutableCache<H, Arc<V>> for LFUHandleCache<H, V>
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);impl<K, V> MutableCache<K, Arc<V>> for HeapLfuCache<K, V>
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"));impl<K, V> MutableCache<K, Arc<V>> for LfuCache<K, V>
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"));impl<K, V> MutableCache<K, Arc<V>> for LruCore<K, V>
impl<K, V> MutableCache<K, V> for ARCCore<K, V>
impl<K, V> MutableCache<K, V> for CARCore<K, V>
impl<K, V> MutableCache<K, V> for ClockCache<K, V>
impl<K, V> MutableCache<K, V> for ClockProCache<K, V>
impl<K, V> MutableCache<K, V> for LrukCache<K, V>
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());