oxigdal_cloud/cache/
mod.rs1use std::path::PathBuf;
14use std::time::Duration;
15
16pub mod backends;
17pub mod eviction;
18pub mod metadata;
19pub mod multi;
20
21#[cfg(test)]
22mod tests;
23
24pub use metadata::{
26 CacheEntry, CacheKey, CacheStats, DiskCacheMetadata, LevelStats, SpatialInfo, TileCoord,
27};
28
29#[cfg(feature = "cache")]
30pub use eviction::{ArcCache, LfuCache, LruTtlCache};
31
32#[cfg(feature = "cache")]
33pub use backends::{PersistentDiskCache, SpatialCache, TileCache};
34
35#[cfg(feature = "cache")]
36pub use multi::{CacheWarmer, DiskCache, MemoryCache, MultiLevelCache};
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
40pub enum EvictionStrategy {
41 Lru,
43 Lfu,
45 #[default]
47 Adaptive,
48 TimeToLive,
50 LargestFirst,
52 Random,
54}
55
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
58pub enum WarmingStrategy {
59 #[default]
61 None,
62 AccessPattern,
64 SpatialAdjacent,
66 PyramidLevels,
68 Custom,
70}
71
72#[derive(Debug, Clone)]
74pub struct CacheConfig {
75 pub max_memory_size: usize,
77 pub max_disk_size: usize,
79 pub compress: bool,
81 pub compress_threshold: usize,
83 pub eviction_strategy: EvictionStrategy,
85 pub persistent: bool,
87 pub cache_dir: Option<PathBuf>,
89 pub default_ttl: Option<Duration>,
91 pub max_age: Option<Duration>,
93 pub max_entries: usize,
95 pub warming_strategy: WarmingStrategy,
97 pub warm_batch_size: usize,
99 pub spatial_aware: bool,
101 pub arc_adaptation_rate: f64,
103}
104
105impl Default for CacheConfig {
106 fn default() -> Self {
107 Self {
108 max_memory_size: 100 * 1024 * 1024, max_disk_size: 1024 * 1024 * 1024, compress: true,
111 compress_threshold: 4096, eviction_strategy: EvictionStrategy::Adaptive,
113 persistent: true,
114 cache_dir: None,
115 default_ttl: Some(Duration::from_secs(3600)), max_age: Some(Duration::from_secs(3600)), max_entries: 10000,
118 warming_strategy: WarmingStrategy::None,
119 warm_batch_size: 10,
120 spatial_aware: false,
121 arc_adaptation_rate: 0.5,
122 }
123 }
124}
125
126impl CacheConfig {
127 #[must_use]
129 pub fn new() -> Self {
130 Self::default()
131 }
132
133 #[must_use]
135 pub fn with_max_memory_size(mut self, size: usize) -> Self {
136 self.max_memory_size = size;
137 self
138 }
139
140 #[must_use]
142 pub fn with_max_disk_size(mut self, size: usize) -> Self {
143 self.max_disk_size = size;
144 self
145 }
146
147 #[must_use]
149 pub fn with_compress(mut self, compress: bool) -> Self {
150 self.compress = compress;
151 self
152 }
153
154 #[must_use]
156 pub fn with_eviction_strategy(mut self, strategy: EvictionStrategy) -> Self {
157 self.eviction_strategy = strategy;
158 self
159 }
160
161 #[must_use]
163 pub fn with_cache_dir(mut self, dir: impl Into<PathBuf>) -> Self {
164 self.cache_dir = Some(dir.into());
165 self
166 }
167
168 #[must_use]
170 pub fn with_default_ttl(mut self, ttl: Duration) -> Self {
171 self.default_ttl = Some(ttl);
172 self
173 }
174
175 #[must_use]
177 pub fn with_max_age(mut self, duration: Duration) -> Self {
178 self.max_age = Some(duration);
179 self
180 }
181
182 #[must_use]
184 pub fn with_max_entries(mut self, count: usize) -> Self {
185 self.max_entries = count;
186 self
187 }
188
189 #[must_use]
191 pub fn with_warming_strategy(mut self, strategy: WarmingStrategy) -> Self {
192 self.warming_strategy = strategy;
193 self
194 }
195
196 #[must_use]
198 pub fn with_spatial_aware(mut self, enabled: bool) -> Self {
199 self.spatial_aware = enabled;
200 self
201 }
202}