foyer_common/properties.rs
1// Copyright 2025 foyer Project Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::Debug;
16
17/// Hint for the cache eviction algorithm to decide the priority of the specific entry if needed.
18///
19/// The meaning of the hint differs in each cache eviction algorithm, and some of them can be ignore by specific
20/// algorithm.
21///
22/// If the given cache hint does not suitable for the cache eviction algorithm that is active, the algorithm may modify
23/// it to a proper one.
24///
25/// For more details, please refer to the document of each enum options.
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28pub enum Hint {
29 /// The default hint shared by all cache eviction algorithms.
30 Normal,
31 /// Suggest the priority of the entry is low.
32 ///
33 /// Used by LRU.
34 Low,
35}
36
37impl Default for Hint {
38 fn default() -> Self {
39 Self::Normal
40 }
41}
42
43// TODO(MrCroxx): Is it necessary to make popluated entry still follow the cache location advice?
44/// Advice cache location for the cache entry.
45///
46/// Useful when using hybrid cache.
47///
48/// NOTE: `CacheLocation` only affects the first time the entry is handle.
49/// After it is populated, the entry may not follow the given advice.
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
51#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
52pub enum Location {
53 /// The default location.
54 ///
55 /// Prefer to store the entry in the in-memory cache with in-memory cache.
56 /// And prefer to store the entry in the hybrid cache with hybrid cache.
57 #[default]
58 Default,
59 /// Prefer to store the entry in the in-memory cache.
60 InMem,
61 /// Prefer to store the entry on the disk cache.
62 OnDisk,
63}
64
65/// Entry age in the disk cache. Used by hybrid cache.
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
68pub enum Age {
69 /// THe entry is still young and will be reserved in the disk cache for a while.
70 Young,
71 /// The entry is old any will be eviction from the disk cache soon.
72 Old,
73}
74
75/// Source context for populated entry.
76#[derive(Debug, Clone, Copy, PartialEq, Eq)]
77#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
78pub struct Populated {
79 /// The age of the entry.
80 pub age: Age,
81}
82
83/// Entry source used by hybrid cache.
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
85#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
86pub enum Source {
87 /// Comes from outer system of foyer.
88 Outer,
89 /// Populated from the disk cache.
90 Populated(Populated),
91}
92
93impl Default for Source {
94 fn default() -> Self {
95 Self::Outer
96 }
97}
98
99/// Entry level properties trait.
100///
101/// The in-memory only cache and the hybrid cache may have different properties implementations to minimize the overhead
102/// of necessary properties in different scenarios.
103pub trait Properties: Send + Sync + 'static + Clone + Default + Debug {
104 /// Set entry as a phantom entry.
105 ///
106 /// A phantom entry will not be actually inserted into the in-memory cache.
107 /// It is only used to keep the APIs consistent.
108 ///
109 /// NOTE: This API is for internal usage only. It MUST NOT be exported publicly.
110 fn with_phantom(self, phantom: bool) -> Self;
111
112 /// If the entry is a phantom entry.
113 fn phantom(&self) -> Option<bool>;
114
115 /// Set entry hint.
116 fn with_hint(self, hint: Hint) -> Self;
117
118 /// Entry hint.
119 fn hint(&self) -> Option<Hint>;
120
121 /// Set entry location.
122 fn with_location(self, location: Location) -> Self;
123
124 /// Entry location.
125 fn location(&self) -> Option<Location>;
126
127 /// Set entry source.
128 fn with_source(self, source: Source) -> Self;
129
130 /// Entry source.
131 fn source(&self) -> Option<Source>;
132}