Skip to content

Commit 505cd10

Browse files
author
Fabien Coelho
committed
type declarations
1 parent b1e14fc commit 505cd10

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

CacheToolsUtils.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ def __delitem__(self, key: Any):
6969
class _StatsMix:
7070
"""Convenient Mixin to forward stats methods to _cache."""
7171

72-
def hits(self):
72+
def hits(self) -> float|None:
7373
return self._cache.hits() # type: ignore
7474

75-
def stats(self):
75+
def stats(self) -> dict[str, Any]:
7676
return self._cache.stats() # type: ignore
7777

7878
def reset(self):
@@ -244,15 +244,15 @@ def __init__(self, cache: MutableMapping):
244244
self._cache = cache
245245
self.reset()
246246

247-
def hits(self):
247+
def hits(self) -> float:
248248
"""Return the cache hit ratio."""
249249
return float(self._hits) / max(self._reads, 1)
250250

251251
def reset(self):
252252
"""Reset internal stats data."""
253253
self._reads, self._writes, self._dels, self._hits = 0, 0, 0, 0
254254

255-
def stats(self):
255+
def stats(self) -> dict[str, Any]:
256256
"""Return available stats data as dict."""
257257
return {
258258
"type": 1,
@@ -338,17 +338,23 @@ def clear(self):
338338
# NOTE not passed on cache2…
339339
return self._cache.clear()
340340

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
347352

348-
def hits(self):
353+
def hits(self) -> float|None:
349354
data = self.stats()
350355
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):
352358
return float(c1["hits"] + c2["hits"]) / max(c1["reads"] + c2["reads"], 1)
353359
# else None
354360

@@ -512,7 +518,7 @@ def clear(self): # pragma: no cover
512518
"""Flush MemCached contents."""
513519
return self._cache.flush_all() # type: ignore
514520

515-
def hits(self):
521+
def hits(self) -> float:
516522
"""Return overall cache hit ratio."""
517523
stats = self._cache.stats() # type: ignore
518524
return float(stats[b"get_hits"]) / max(stats[b"cmd_get"], 1)
@@ -614,7 +620,7 @@ def info(self, *args, **kwargs):
614620
"""Return redis informations."""
615621
return self._cache.info(*args, **kwargs)
616622

617-
def stats(self):
623+
def stats(self) -> dict[str, Any]:
618624
return self.info(section="stats")
619625

620626
def dbsize(self, *args, **kwargs):
@@ -637,7 +643,7 @@ def delete(self, index):
637643
del self[index]
638644

639645
# stats
640-
def hits(self):
646+
def hits(self) -> float:
641647
"""Return cache hits."""
642648
stats = self.stats()
643649
return float(stats["keyspace_hits"]) / (

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Classes to add key prefix and stats to
77
and other cache-related utils.
88

99
![Status](https://github.com/zx80/cachetools-utils/actions/workflows/cachetools-utils.yml/badge.svg?branch=main&style=flat)
10-
![Tests](https://img.shields.io/badge/tests-17%20✓-success)
10+
![Tests](https://img.shields.io/badge/tests-19%20✓-success)
1111
![Coverage](https://img.shields.io/badge/coverage-100%25-success)
1212
![Issues](https://img.shields.io/github/issues/zx80/cachetools-utils?style=flat)
1313
![Python](https://img.shields.io/badge/python-3-informational)

docs/VERSIONS.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ Install [package](https://pypi.org/project/CacheToolsUtils/) from
1919
Maybe the existing client can do that with appropriate options?
2020
- `cached`: add `contains` and `delete` parameters to change names?
2121

22-
## ? on ?
22+
## 9.0 on ?
2323

2424
Add `stats` method to return a convenient `dict` of statistics.
25+
Improve type declarations.
26+
Add some tests.
27+
Test with (future) Python 3.14.
2528
Fix doc typo.
2629

2730
## 8.6 on 2024-08-03

test.py

+8
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ def test_two_level_ok():
329329
del c2[KEY]
330330

331331

332+
def test_twolevel_bad_stats():
333+
c0 = ctu.DictCache()
334+
c1 = ctu.DictCache()
335+
c2 = ctu.TwoLevelCache(c0, c1)
336+
assert isinstance(c2.stats(), dict)
337+
assert c2.hits() is None
338+
339+
332340
class Stuff:
333341
"""Test class with cacheable methods."""
334342

0 commit comments

Comments
 (0)