Skip to main content

linkrs_query/cache/
config.rs

1//! Unified Cache Configuration Module
2//!
3//! Provides centralized configuration for all cache types.
4
5use serde::{Deserialize, Serialize};
6
7/// Cache priority levels
8#[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/// TTL configuration for cache entries
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct TtlConfig {
20    /// Base TTL in seconds
21    pub base_ttl_seconds: u64,
22    /// Enable adaptive TTL based on access patterns
23    pub adaptive: bool,
24    /// Minimum TTL in seconds
25    pub min_ttl_seconds: u64,
26    /// Maximum TTL in seconds
27    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/// Priority configuration for cache entries
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct PriorityConfig {
44    /// Enable priority-based eviction
45    pub enable_priority: bool,
46    /// Track execution time for priority calculation
47    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/// Query Plan Cache Configuration
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct PlanCacheConfig {
62    /// Maximum number of cache entries
63    pub max_entries: usize,
64    /// Memory budget (bytes)
65    pub memory_budget: usize,
66    /// Maximum weight (bytes), takes precedence over max_entries
67    /// If None, use memory_budget
68    pub max_weight: Option<u64>,
69    /// Whether to enable parameterized query support
70    pub enable_parameterized: bool,
71    /// TTL configuration
72    pub ttl_config: TtlConfig,
73    /// Priority configuration
74    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    /// Create a minimal configuration for embedded environments
92    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    /// Create a high-performance configuration for server environments
109    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    /// Get effective max weight
126    pub fn effective_max_weight(&self) -> u64 {
127        self.max_weight.unwrap_or(self.memory_budget as u64)
128    }
129}
130
131/// CTE Cache Configuration
132#[derive(Debug, Clone, Serialize, Deserialize)]
133pub struct CteCacheConfig {
134    /// Maximum cache size (bytes)
135    pub max_size: usize,
136    /// Maximum number of entries (optional)
137    pub max_entries: Option<usize>,
138    /// Maximum size of a single entry (bytes)
139    pub max_entry_size: usize,
140    /// Minimum number of rows to cache (less than this value is not cached)
141    pub min_row_count: u64,
142    /// Maximum number of rows to cache (greater than this value is not cached)
143    pub max_row_count: u64,
144    /// Entry expiration time (seconds)
145    pub entry_ttl_seconds: u64,
146    /// Enable caching
147    pub enabled: bool,
148    /// Whether to enable adaptive caching
149    pub adaptive: bool,
150    /// Whether to enable priority
151    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    /// Create a small memory configuration
172    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    /// Create a large memory configuration
187    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    /// Disable caching
202    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}