pub struct CacheStats {
pub hits: u64,
pub misses: u64,
pub evictions: u64,
pub invalidations: u64,
}Expand description
Cache statistics for monitoring and debugging.
Fields§
§hits: u64Total cache hits
misses: u64Total cache misses
evictions: u64Total evictions due to size limit
invalidations: u64Total invalidations due to file changes
Implementations§
Source§impl CacheStats
impl CacheStats
Sourcepub fn hit_ratio(&self) -> f64
pub fn hit_ratio(&self) -> f64
Calculate cache hit ratio.
Examples found in repository?
examples/caching_performance.rs (line 167)
130fn demonstrate_loader_caching() -> Result<(), Box<dyn std::error::Error>> {
131 println!("4. Advanced Loader with Caching:");
132
133 // Configure loader with caching enabled
134 let loader_config = LoaderConfig {
135 enable_caching: true,
136 cache_config: CacheConfig {
137 max_size: 100,
138 max_age: Some(Duration::from_secs(600)), // 10 minutes
139 track_file_changes: true,
140 enable_stats: true,
141 },
142 ..LoaderConfig::default()
143 };
144
145 let mut loader = TemplateLoader::with_config(loader_config);
146
147 // Load templates with caching
148 println!(" - Loading templates from directory (with caching)...");
149 let start = Instant::now();
150 let templates = loader.load_from_directory("templates")?;
151 let first_load_time = start.elapsed();
152
153 println!(" - First load: {} templates in {:?}", templates.len(), first_load_time);
154 println!(" - Cache size: {}", loader.cache_size());
155
156 // Load again to test cache performance
157 let start = Instant::now();
158 let _templates = loader.load_from_directory("templates")?;
159 let cached_load_time = start.elapsed();
160
161 println!(" - Cached load: {:?} ({}x faster)",
162 cached_load_time,
163 first_load_time.as_nanos() / cached_load_time.as_nanos().max(1));
164
165 // Show cache statistics
166 if let Some(stats) = loader.cache_stats() {
167 println!(" - Cache hit ratio: {:.2}%", stats.hit_ratio() * 100.0);
168 println!(" - Total hits: {}, misses: {}", stats.hits, stats.misses);
169 }
170
171 println!();
172 Ok(())
173}
174
175fn demonstrate_cache_monitoring() -> Result<(), Box<dyn std::error::Error>> {
176 println!("5. Cache Statistics and Monitoring:");
177
178 let cache = TemplateCache::with_config(CacheConfig {
179 enable_stats: true,
180 max_size: 10,
181 ..CacheConfig::default()
182 });
183
184 // Generate some cache activity
185 let templates = vec![
186 "Template A: @[content]@",
187 "Template B: @[data]@",
188 "Template C: @[info]@",
189 "Template D: @[value]@",
190 "Template E: @[result]@",
191 ];
192
193 // Insert templates
194 for template_content in &templates {
195 let template = TronTemplate::new(template_content)?;
196 cache.insert_template(template)?;
197 }
198
199 // Create cache hits and misses
200 for _ in 0..10 {
201 // Cache hits
202 let _hit = cache.get_by_content(templates[0]);
203 let _hit = cache.get_by_content(templates[1]);
204
205 // Cache misses
206 let _miss = cache.get_by_content("Nonexistent template @[x]@");
207 }
208
209 // Add more templates to trigger eviction
210 for i in 6..15 {
211 let template = TronTemplate::new(&format!("Template {}: @[data]@", i))?;
212 cache.insert_template(template)?;
213 }
214
215 // Display comprehensive statistics
216 if let Some(stats) = cache.stats() {
217 println!(" - Cache Performance Metrics:");
218 println!(" * Total requests: {}", stats.hits + stats.misses);
219 println!(" * Cache hits: {}", stats.hits);
220 println!(" * Cache misses: {}", stats.misses);
221 println!(" * Hit ratio: {:.2}%", stats.hit_ratio() * 100.0);
222 println!(" * Evictions: {}", stats.evictions);
223 println!(" * Invalidations: {}", stats.invalidations);
224 }
225
226 println!(" - Current cache size: {}", cache.size());
227 println!(" - Cache is empty: {}", cache.is_empty());
228
229 // Clean up expired entries
230 cache.cleanup_expired();
231 println!(" - After cleanup: {} templates", cache.size());
232
233 println!();
234 Ok(())
235}Trait Implementations§
Source§impl Clone for CacheStats
impl Clone for CacheStats
Source§fn clone(&self) -> CacheStats
fn clone(&self) -> CacheStats
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for CacheStats
impl Debug for CacheStats
Source§impl Default for CacheStats
impl Default for CacheStats
Source§fn default() -> CacheStats
fn default() -> CacheStats
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for CacheStats
impl RefUnwindSafe for CacheStats
impl Send for CacheStats
impl Sync for CacheStats
impl Unpin for CacheStats
impl UnwindSafe for CacheStats
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more