1use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Serialize, Deserialize)]
9pub enum CachePriority {
10 Low = 0,
11 #[default]
12 Normal = 1,
13 High = 2,
14 Critical = 3,
15}
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct TtlConfig {
20 pub base_ttl_seconds: u64,
22 pub adaptive: bool,
24 pub min_ttl_seconds: u64,
26 pub max_ttl_seconds: u64,
28}
29
30impl Default for TtlConfig {
31 fn default() -> Self {
32 Self {
33 base_ttl_seconds: 3600,
34 adaptive: true,
35 min_ttl_seconds: 300,
36 max_ttl_seconds: 86400,
37 }
38 }
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct PriorityConfig {
44 pub enable_priority: bool,
46 pub track_execution_time: bool,
48}
49
50impl Default for PriorityConfig {
51 fn default() -> Self {
52 Self {
53 enable_priority: true,
54 track_execution_time: true,
55 }
56 }
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct PlanCacheConfig {
62 pub max_entries: usize,
64 pub memory_budget: usize,
66 pub max_weight: Option<u64>,
69 pub enable_parameterized: bool,
71 pub ttl_config: TtlConfig,
73 pub priority_config: PriorityConfig,
75}
76
77impl Default for PlanCacheConfig {
78 fn default() -> Self {
79 Self {
80 max_entries: 1000,
81 memory_budget: 50 * 1024 * 1024,
82 max_weight: None,
83 enable_parameterized: true,
84 ttl_config: TtlConfig::default(),
85 priority_config: PriorityConfig::default(),
86 }
87 }
88}
89
90impl PlanCacheConfig {
91 pub fn minimal() -> Self {
93 Self {
94 max_entries: 200,
95 memory_budget: 10 * 1024 * 1024,
96 max_weight: None,
97 enable_parameterized: true,
98 ttl_config: TtlConfig {
99 base_ttl_seconds: 1800,
100 adaptive: true,
101 min_ttl_seconds: 60,
102 max_ttl_seconds: 3600,
103 },
104 priority_config: PriorityConfig::default(),
105 }
106 }
107
108 pub fn high_performance() -> Self {
110 Self {
111 max_entries: 5000,
112 memory_budget: 200 * 1024 * 1024,
113 max_weight: None,
114 enable_parameterized: true,
115 ttl_config: TtlConfig {
116 base_ttl_seconds: 7200,
117 adaptive: true,
118 min_ttl_seconds: 600,
119 max_ttl_seconds: 86400,
120 },
121 priority_config: PriorityConfig::default(),
122 }
123 }
124
125 pub fn effective_max_weight(&self) -> u64 {
127 self.max_weight.unwrap_or(self.memory_budget as u64)
128 }
129}
130
131#[derive(Debug, Clone, Serialize, Deserialize)]
133pub struct CteCacheConfig {
134 pub max_size: usize,
136 pub max_entries: Option<usize>,
138 pub max_entry_size: usize,
140 pub min_row_count: u64,
142 pub max_row_count: u64,
144 pub entry_ttl_seconds: u64,
146 pub enabled: bool,
148 pub adaptive: bool,
150 pub enable_priority: bool,
152}
153
154impl Default for CteCacheConfig {
155 fn default() -> Self {
156 Self {
157 max_size: 64 * 1024 * 1024,
158 max_entries: Some(10000),
159 max_entry_size: 10 * 1024 * 1024,
160 min_row_count: 100,
161 max_row_count: 100_000,
162 entry_ttl_seconds: 3600,
163 enabled: true,
164 adaptive: true,
165 enable_priority: true,
166 }
167 }
168}
169
170impl CteCacheConfig {
171 pub fn low_memory() -> Self {
173 Self {
174 max_size: 16 * 1024 * 1024,
175 max_entries: Some(5000),
176 max_entry_size: 5 * 1024 * 1024,
177 min_row_count: 50,
178 max_row_count: 50_000,
179 entry_ttl_seconds: 1800,
180 enabled: true,
181 adaptive: true,
182 enable_priority: true,
183 }
184 }
185
186 pub fn high_memory() -> Self {
188 Self {
189 max_size: 256 * 1024 * 1024,
190 max_entries: Some(20000),
191 max_entry_size: 50 * 1024 * 1024,
192 min_row_count: 100,
193 max_row_count: 500_000,
194 entry_ttl_seconds: 7200,
195 enabled: true,
196 adaptive: true,
197 enable_priority: true,
198 }
199 }
200
201 pub fn disabled() -> Self {
203 Self {
204 enabled: false,
205 ..Default::default()
206 }
207 }
208}
209
210#[cfg(test)]
211mod tests {
212 use super::*;
213
214 #[test]
215 fn test_ttl_config_default() {
216 let config = TtlConfig::default();
217 assert_eq!(config.base_ttl_seconds, 3600);
218 assert!(config.adaptive);
219 }
220
221 #[test]
222 fn test_plan_cache_config_default() {
223 let config = PlanCacheConfig::default();
224 assert_eq!(config.max_entries, 1000);
225 assert_eq!(config.memory_budget, 50 * 1024 * 1024);
226 }
227
228 #[test]
229 fn test_cte_cache_config_default() {
230 let config = CteCacheConfig::default();
231 assert_eq!(config.max_size, 64 * 1024 * 1024);
232 assert!(config.enabled);
233 }
234}