1use std::hash::BuildHasher;
2use parking_lot::MutexGuard;
3
4use crate::LightCache;
5
6pub mod lru;
7pub mod noop;
8pub mod ttl;
9
10mod linked_arena;
11
12pub use noop::NoopPolicy;
13pub use ttl::TtlPolicy;
14pub use lru::LruPolicy;
15
16pub trait Policy<K, V>: Sized {
20 type Inner: Prune<K, V, Self>;
22
23 fn lock_inner(&self) -> MutexGuard<'_, Self::Inner>;
26
27 fn get<S: BuildHasher>(&self, key: &K, cache: &LightCache<K, V, S, Self>) -> Option<V>;
28
29 fn insert<S: BuildHasher>(&self, key: K, value: V, cache: &LightCache<K, V, S, Self>) -> Option<V>;
30
31 fn remove<S: BuildHasher>(&self, key: &K, cache: &LightCache<K, V, S, Self>) -> Option<V>;
32
33 fn lock_and_prune<S: BuildHasher>(&self, cache: &LightCache<K, V, S, Self>) -> MutexGuard<'_, Self::Inner> {
34 let mut lock = self.lock_inner();
35 lock.prune(cache);
36
37 lock
38 }
39}
40
41pub trait Prune<K, V, P> {
43 fn prune<S: BuildHasher>(&mut self, cache: &LightCache<K, V, S, P>);
45}