Files
mytheclipse/crates/mytheclipse-cache/src/metrics.rs
T
asepharyana 8106f8943e feat: add 4 new crates (queue, tracing, http, cli) + enhancements to existing crates
New crates:
- mytheclipse-queue: unified job queue with WorkerPool, retry/backoff, DLQ
  Backends: in-memory (default), Redis, NATS, PostgreSQL
- mytheclipse-tracing: tracing subscriber layers with env filter + OTLP/Jaeger export
- mytheclipse-http: HTTP client/server with timeout + tracing, axum server
- mytheclipse-cli: CLI framework with clap derive, built-in subcommands

Enhancements to existing crates:
- mytheclipse-core: SemaphorePool, LeaderElection, HealthRegistry/HealthCheck
- mytheclipse-cache: AutoRefreshCache (bg refresh on miss), CacheMetrics
- mytheclipse-config: ConfigSchema for JSON Schema generation
- mytheclipse-storage: MultipartUploadDriver trait
- mytheclipse-crypto: PASETO v4.local token support

All features compile with --all-features; tests + clippy pass clean.
2026-08-29 16:05:02 +07:00

60 lines
1.4 KiB
Rust

//! Cache instrumentation metrics (hit/miss/eviction counters).
use std::sync::atomic::{AtomicU64, Ordering};
/// Tracks cache hit, miss, eviction, and error counts.
#[derive(Default)]
pub struct CacheMetrics {
hits: AtomicU64,
misses: AtomicU64,
evictions: AtomicU64,
errors: AtomicU64,
}
impl CacheMetrics {
pub fn new() -> Self {
Self::default()
}
pub fn hit(&self) {
self.hits.fetch_add(1, Ordering::Relaxed);
}
pub fn miss(&self) {
self.misses.fetch_add(1, Ordering::Relaxed);
}
pub fn eviction(&self) {
self.evictions.fetch_add(1, Ordering::Relaxed);
}
pub fn error(&self) {
self.errors.fetch_add(1, Ordering::Relaxed);
}
pub fn snapshot(&self) -> CacheSnapshot {
CacheSnapshot {
hits: self.hits.load(Ordering::Relaxed),
misses: self.misses.load(Ordering::Relaxed),
evictions: self.evictions.load(Ordering::Relaxed),
errors: self.errors.load(Ordering::Relaxed),
}
}
}
/// A point-in-time read of cache metrics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CacheSnapshot {
pub hits: u64,
pub misses: u64,
pub evictions: u64,
pub errors: u64,
}
impl CacheSnapshot {
pub fn hit_rate(&self) -> f64 {
let total = self.hits + self.misses;
if total == 0 { 0.0 } else { self.hits as f64 / total as f64 }
}
}