@@ -69,10 +69,10 @@ def __delitem__(self, key: Any):
69
69
class _StatsMix :
70
70
"""Convenient Mixin to forward stats methods to _cache."""
71
71
72
- def hits (self ):
72
+ def hits (self ) -> float | None :
73
73
return self ._cache .hits () # type: ignore
74
74
75
- def stats (self ):
75
+ def stats (self ) -> dict [ str , Any ] :
76
76
return self ._cache .stats () # type: ignore
77
77
78
78
def reset (self ):
@@ -244,15 +244,15 @@ def __init__(self, cache: MutableMapping):
244
244
self ._cache = cache
245
245
self .reset ()
246
246
247
- def hits (self ):
247
+ def hits (self ) -> float :
248
248
"""Return the cache hit ratio."""
249
249
return float (self ._hits ) / max (self ._reads , 1 )
250
250
251
251
def reset (self ):
252
252
"""Reset internal stats data."""
253
253
self ._reads , self ._writes , self ._dels , self ._hits = 0 , 0 , 0 , 0
254
254
255
- def stats (self ):
255
+ def stats (self ) -> dict [ str , Any ] :
256
256
"""Return available stats data as dict."""
257
257
return {
258
258
"type" : 1 ,
@@ -338,17 +338,23 @@ def clear(self):
338
338
# NOTE not passed on cache2…
339
339
return self ._cache .clear ()
340
340
341
- def stats (self ):
342
- return {
343
- "type" : 2 ,
344
- "cache1" : self ._cache .stats (), # type: ignore
345
- "cache2" : self ._cache2 .stats () # type: ignore
346
- }
341
+ def stats (self ) -> dict [str , Any ]:
342
+ data = { "type" : 2 }
343
+ try :
344
+ data ["cache1" ] = self ._cache .stats () # type: ignore
345
+ except Exception :
346
+ data ["cache1" ] = {} # type: ignore
347
+ try :
348
+ data ["cache2" ] = self ._cache2 .stats () # type: ignore
349
+ except Exception :
350
+ data ["cache2" ] = {} # type: ignore
351
+ return data
347
352
348
- def hits (self ):
353
+ def hits (self ) -> float | None :
349
354
data = self .stats ()
350
355
c1 , c2 = data ["cache1" ], data ["cache2" ]
351
- if c1 ["type" ] == 1 and c2 ["type" ] == 1 :
356
+ if (c1 and "type" in c1 and c1 ["type" ] == 1 and
357
+ c2 and "type" in c2 and c2 ["type" ] == 1 ):
352
358
return float (c1 ["hits" ] + c2 ["hits" ]) / max (c1 ["reads" ] + c2 ["reads" ], 1 )
353
359
# else None
354
360
@@ -512,7 +518,7 @@ def clear(self): # pragma: no cover
512
518
"""Flush MemCached contents."""
513
519
return self ._cache .flush_all () # type: ignore
514
520
515
- def hits (self ):
521
+ def hits (self ) -> float :
516
522
"""Return overall cache hit ratio."""
517
523
stats = self ._cache .stats () # type: ignore
518
524
return float (stats [b"get_hits" ]) / max (stats [b"cmd_get" ], 1 )
@@ -614,7 +620,7 @@ def info(self, *args, **kwargs):
614
620
"""Return redis informations."""
615
621
return self ._cache .info (* args , ** kwargs )
616
622
617
- def stats (self ):
623
+ def stats (self ) -> dict [ str , Any ] :
618
624
return self .info (section = "stats" )
619
625
620
626
def dbsize (self , * args , ** kwargs ):
@@ -637,7 +643,7 @@ def delete(self, index):
637
643
del self [index ]
638
644
639
645
# stats
640
- def hits (self ):
646
+ def hits (self ) -> float :
641
647
"""Return cache hits."""
642
648
stats = self .stats ()
643
649
return float (stats ["keyspace_hits" ]) / (
0 commit comments