use std::fmt::Debug;
use std::hash::Hash;
use std::time::Instant;
pub struct EvictionCandidate<K, V> {
pub key: K,
pub value: V,
pub last_access: Instant,
}
pub trait EvictionPolicy<K, V>: Send + Sync + Debug
where
K: Clone,
V: Clone,
{
fn select_victim(&self, candidates: &[EvictionCandidate<K, V>]) -> Option<K>;
fn should_evict(&self, current_entries: usize, current_memory_bytes: usize) -> bool;
fn tracks_memory(&self) -> bool {
false
}
fn estimate_entry_size(&self, _key: &K, _value: &V) -> usize {
0
}
}
pub trait Clock: Send + Sync + Debug {
fn now(&self) -> Instant;
}
pub trait Storage<K, V>: Send + Sync + Debug
where
K: Hash + Eq + Clone + Send + Sync,
V: Send + Sync,
{
fn with_entry_mut<F, R>(&self, key: K, factory: impl FnOnce() -> V, accessor: F) -> R
where
F: FnOnce(&mut V) -> R;
fn len(&self) -> usize;
fn is_empty(&self) -> bool;
fn clear(&self);
fn for_each<F>(&self, f: F)
where
F: FnMut(&K, &V);
fn retain<F>(&self, f: F)
where
F: FnMut(&K, &mut V) -> bool;
}