cache.cache_metrics
On this page
CacheMetrics
@dataclass
class CacheMetrics:
l1_hits: int = 0
l2_hits: int = 0
l3_hits: int = 0
l1_misses: int = 0
l2_misses: int = 0
l3_misses: int = 0
sets: int = 0
evictions: int = 0
promotions: int = 0
expirations: int = 0
Overview
A simple, per-level hit/miss counter dataclass used exclusively by
MultiLevelCache (self.metrics). This is distinct from — and much
simpler than — CacheMetricsCollector, which serves the
semantic-cache pipeline and adds latency histograms, cost tracking, and RL reward
statistics.
Methods
get_hit_rate() details
def get_hit_rate(self, level: Optional[int] = None) -> float:
if level == 1:
total = self.l1_hits + self.l1_misses
return self.l1_hits / total if total > 0 else 0.0
elif level == 2: ...
elif level == 3: ...
else:
total_hits = self.l1_hits + self.l2_hits + self.l3_hits
total_gets = total_hits + self.l1_misses + self.l2_misses + self.l3_misses
return total_hits / total_gets if total_gets > 0 else 0.0
Passing any level value other than 1, 2, 3, or None falls through to the else
branch (the overall rate) — there is no validation or ValueError for out-of-range levels.
Examples
from zerda.cache import CacheMetrics
m = CacheMetrics()
m.record_get(1, hit=False)
m.record_get(1, hit=True)
m.record_set()
print(m.get_hit_rate(1)) # 0.5
print(m.get_stats()["sets"]) # 1
m.reset()
print(m.get_hit_rate()) # 0.0 (no data)
Complexity
Every method is O(1).
Thread Safety
CacheMetrics has no internal locking of its own. It is safe only because its sole
consumer, MultiLevelCache, always calls its methods while holding
MultiLevelCache.lock. Using a CacheMetrics instance directly from multiple threads
without external synchronization is not safe.
Related APIs
MultiLevelCache— the sole owner/consumerCacheMetricsCollector— the analogous, more feature-rich metrics component for the semantic-cache pipeline