pub struct CacheStats { /* private fields */ }Expand description
Cache statistics for monitoring hit/miss rates and performance.
This structure tracks cache access patterns using atomic operations for thread-safe statistics collection with minimal overhead.
§Thread Safety
All operations are thread-safe using atomic operations with Relaxed ordering,
which provides the best performance while still maintaining consistency.
§Examples
use cachelito_core::CacheStats;
let stats = CacheStats::new();
// Simulate cache operations
stats.record_hit();
stats.record_hit();
stats.record_miss();
assert_eq!(stats.hits(), 2);
assert_eq!(stats.misses(), 1);
assert_eq!(stats.total_accesses(), 3);
assert!((stats.hit_rate() - 0.6666).abs() < 0.001);Implementations§
Source§impl CacheStats
impl CacheStats
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new CacheStats instance with zero counters.
§Examples
use cachelito_core::CacheStats;
let stats = CacheStats::new();
assert_eq!(stats.hits(), 0);
assert_eq!(stats.misses(), 0);Sourcepub fn record_hit(&self)
pub fn record_hit(&self)
Records a cache hit (successful lookup).
This method is called internally when a cache lookup finds a valid entry. Uses atomic operations for thread-safety with minimal overhead.
§Examples
use cachelito_core::CacheStats;
let stats = CacheStats::new();
stats.record_hit();
assert_eq!(stats.hits(), 1);Sourcepub fn record_miss(&self)
pub fn record_miss(&self)
Records a cache miss (failed lookup).
This method is called internally when a cache lookup doesn’t find an entry or finds an expired entry.
§Examples
use cachelito_core::CacheStats;
let stats = CacheStats::new();
stats.record_miss();
assert_eq!(stats.misses(), 1);Sourcepub fn hits(&self) -> u64
pub fn hits(&self) -> u64
Returns the total number of cache hits.
§Examples
use cachelito_core::CacheStats;
let stats = CacheStats::new();
stats.record_hit();
stats.record_hit();
assert_eq!(stats.hits(), 2);Sourcepub fn misses(&self) -> u64
pub fn misses(&self) -> u64
Returns the total number of cache misses.
§Examples
use cachelito_core::CacheStats;
let stats = CacheStats::new();
stats.record_miss();
stats.record_miss();
stats.record_miss();
assert_eq!(stats.misses(), 3);Sourcepub fn total_accesses(&self) -> u64
pub fn total_accesses(&self) -> u64
Returns the total number of cache accesses (hits + misses).
§Examples
use cachelito_core::CacheStats;
let stats = CacheStats::new();
stats.record_hit();
stats.record_miss();
stats.record_hit();
assert_eq!(stats.total_accesses(), 3);Sourcepub fn hit_rate(&self) -> f64
pub fn hit_rate(&self) -> f64
Calculates and returns the cache hit rate as a fraction (0.0 to 1.0).
The hit rate is the ratio of successful lookups to total lookups. Returns 0.0 if there have been no accesses.
§Examples
use cachelito_core::CacheStats;
let stats = CacheStats::new();
stats.record_hit();
stats.record_hit();
stats.record_miss();
// 2 hits out of 3 total = 0.6666...
assert!((stats.hit_rate() - 0.6666).abs() < 0.001);Sourcepub fn miss_rate(&self) -> f64
pub fn miss_rate(&self) -> f64
Calculates and returns the cache miss rate as a fraction (0.0 to 1.0).
The miss rate is the ratio of failed lookups to total lookups. Returns 0.0 if there have been no accesses.
§Examples
use cachelito_core::CacheStats;
let stats = CacheStats::new();
stats.record_hit();
stats.record_miss();
stats.record_miss();
// 2 misses out of 3 total = 0.6666...
assert!((stats.miss_rate() - 0.6666).abs() < 0.001);Sourcepub fn reset(&self)
pub fn reset(&self)
Resets all statistics counters to zero.
This can be useful for measuring statistics over specific time periods or after configuration changes.
§Examples
use cachelito_core::CacheStats;
let stats = CacheStats::new();
stats.record_hit();
stats.record_miss();
assert_eq!(stats.total_accesses(), 2);
stats.reset();
assert_eq!(stats.total_accesses(), 0);
assert_eq!(stats.hits(), 0);
assert_eq!(stats.misses(), 0);