priority_lfu/traits.rs
1use std::hash::Hash;
2
3use priority_lfu_derive::DeepSizeOf;
4
5use crate::deepsize::DeepSizeOf;
6
7pub(crate) const NUM_POLICY_BUCKETS: usize = 3;
8
9/// Cache eviction policy determining retention priority.
10///
11/// Lower discriminant values = higher priority = evicted last.
12#[repr(u8)]
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, DeepSizeOf)]
14pub enum CachePolicy {
15 /// Critical metadata that should persist (catalog, schemas, indexes).
16 /// Last to be evicted, highest retention.
17 Critical = 0,
18
19 /// Standard cacheable data (recent queries, lookup results).
20 /// Normal eviction behavior.
21 #[default]
22 Standard = 1,
23
24 /// Temporary or easily recomputed data (intermediate results, aggregations).
25 /// First to be evicted.
26 Volatile = 2,
27}
28
29/// Marker trait for cache keys. Associates a key type with its value type.
30///
31/// # Example
32///
33/// ```
34/// use priority_lfu::{DeepSizeOf, CacheKey, CachePolicy};
35///
36/// #[derive(Hash, Eq, PartialEq, Clone)]
37/// struct UserId(u64);
38///
39/// #[derive(Clone, Debug, PartialEq, DeepSizeOf)]
40/// struct UserData {
41/// name: String,
42/// }
43///
44/// impl CacheKey for UserId {
45/// type Value = UserData;
46///
47/// fn policy(&self) -> CachePolicy {
48/// CachePolicy::Standard
49/// }
50/// }
51/// ```
52pub trait CacheKey: Hash + Eq + Clone + Send + Sync + 'static {
53 /// The value type associated with this key.
54 type Value: DeepSizeOf + Send + Sync;
55
56 /// Eviction policy determining retention priority.
57 ///
58 /// This method is called on the key, allowing different keys to have
59 /// different policies for the same value type.
60 fn policy(&self) -> CachePolicy {
61 CachePolicy::Standard
62 }
63}