-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_metrics.py
40 lines (34 loc) · 1.12 KB
/
cache_metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from threading import Lock
class CacheMetrics:
def __init__(self):
self.hits = 0
self.misses = 0
self.total_requests = 0
self.evictions = 0
self.expirations = 0
self.lock = Lock()
def record_hit(self):
with self.lock:
self.hits += 1
self.total_requests += 1
def record_miss(self):
with self.lock:
self.misses += 1
self.total_requests += 1
def record_eviction(self):
with self.lock:
self.evictions += 1
def record_expiration(self):
with self.lock:
self.expirations += 1
def get_metrics(self):
with self.lock:
return {
"hits": self.hits,
"misses": self.misses,
"total_requests": self.total_requests,
"evictions": self.evictions,
"expirations": self.expirations,
"hit_ratio": self.hits / self.total_requests if self.total_requests > 0 else 0,
"miss_ratio": self.misses / self.total_requests if self.total_requests > 0 else 0
}