CacheStats

Struct CacheStats 

Source
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

Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);

Trait Implementations§

Source§

impl Clone for CacheStats

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CacheStats

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CacheStats

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.