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)]
51pub enum Location {
52    /// The default location.
53    ///
54    /// Prefer to store the entry in the in-memory cache with in-memory cache.
55    /// And prefer to store the entry in the hybrid cache with hybrid cache.
56    Default,
57    /// Prefer to store the entry in the in-memory cache.
58    InMem,
59    /// Prefer to store the entry on the disk cache.
60    OnDisk,
61}
62
63impl Default for Location {
64    fn default() -> Self {
65        Self::Default
66    }
67}
68
69/// Entry age in the disk cache. Used by hybrid cache.
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub enum Age {
72    /// THe entry is still young and will be reserved in the disk cache for a while.
73    Young,
74    /// The entry is old any will be eviction from the disk cache soon.
75    Old,
76}
77
78/// Source context for populated entry.
79#[derive(Debug, Clone, Copy, PartialEq, Eq)]
80pub struct Populated {
81    /// The age of the entry.
82    pub age: Age,
83}
84
85/// Entry source used by hybrid cache.
86#[derive(Debug, Clone, Copy, PartialEq, Eq)]
87pub enum Source {
88    /// Comes from outer system of foyer.
89    Outer,
90    /// Populated from the disk cache.
91    Populated(Populated),
92}
93
94impl Default for Source {
95    fn default() -> Self {
96        Self::Outer
97    }
98}
99
100/// Entry level properties trait.
101///
102/// The in-memory only cache and the hybrid cache may have different properties implementations to minimize the overhead
103/// of necessary properties in different scenarios.
104pub trait Properties: Send + Sync + 'static + Clone + Default + Debug {
105    /// Set entry ephemeral.
106    fn with_ephemeral(self, ephemeral: bool) -> Self;
107
108    /// Entry ephemeral.
109    fn ephemeral(&self) -> Option<bool>;
110
111    /// Set entry hint.
112    fn with_hint(self, hint: Hint) -> Self;
113
114    /// Entry hint.
115    fn hint(&self) -> Option<Hint>;
116
117    /// Set entry location.
118    fn with_location(self, location: Location) -> Self;
119
120    /// Entry location.
121    fn location(&self) -> Option<Location>;
122
123    /// Set entry source.
124    fn with_source(self, source: Source) -> Self;
125
126    /// Entry source.
127    fn source(&self) -> Option<Source>;
128}