plain_cache/cache/
stats.rs

1use std::sync::atomic::{AtomicU64, Ordering};
2
3/// Cache performance statistics.
4#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
5pub struct Stats {
6    pub miss_count: u64,
7    pub hit_count: u64,
8    pub eviction_count: u64,
9    pub millis_elapsed: u128,
10}
11
12#[derive(Debug, Default)]
13pub(crate) struct Counters {
14    hit_count: AtomicU64,
15    miss_count: AtomicU64,
16    eviction_count: AtomicU64,
17}
18
19impl Counters {
20    pub(crate) fn hit_count(&self) -> u64 {
21        self.hit_count.load(Ordering::Acquire)
22    }
23
24    pub(crate) fn miss_count(&self) -> u64 {
25        self.miss_count.load(Ordering::Acquire)
26    }
27
28    pub(crate) fn eviction_count(&self) -> u64 {
29        self.eviction_count.load(Ordering::Acquire)
30    }
31
32    pub(crate) fn increment_hit_count(&self) {
33        self.hit_count.fetch_add(1, Ordering::AcqRel);
34    }
35
36    pub(crate) fn increment_miss_count(&self) {
37        self.miss_count.fetch_add(1, Ordering::AcqRel);
38    }
39
40    pub(crate) fn increment_eviction_count(&self) {
41        self.eviction_count.fetch_add(1, Ordering::AcqRel);
42    }
43
44    pub(crate) fn reset(&self) {
45        self.hit_count.store(0, Ordering::Release);
46        self.miss_count.store(0, Ordering::Release);
47        self.eviction_count.store(0, Ordering::Release);
48    }
49}