CacheToolsUtils 8.5__py3-none-any.whl → 9.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: CacheToolsUtils
3
- Version: 8.5
3
+ Version: 9.0
4
4
  Summary: Cachetools Utilities
5
5
  Author-email: Fabien Coelho <cachetools.utils@coelho.net>
6
6
  License: CC0
@@ -22,6 +22,7 @@ Requires-Dist: mypy ; extra == 'dev'
22
22
  Requires-Dist: pyright ; extra == 'dev'
23
23
  Requires-Dist: black ; extra == 'dev'
24
24
  Requires-Dist: flake8 ; extra == 'dev'
25
+ Requires-Dist: ruff ; extra == 'dev'
25
26
  Requires-Dist: pytest ; extra == 'dev'
26
27
  Requires-Dist: coverage ; extra == 'dev'
27
28
  Requires-Dist: pymarkdownlnt ; extra == 'dev'
@@ -50,7 +51,7 @@ Classes to add key prefix and stats to
50
51
  and other cache-related utils.
51
52
 
52
53
  ![Status](https://github.com/zx80/cachetools-utils/actions/workflows/cachetools-utils.yml/badge.svg?branch=main&style=flat)
53
- ![Tests](https://img.shields.io/badge/tests-17%20✓-success)
54
+ ![Tests](https://img.shields.io/badge/tests-19%20✓-success)
54
55
  ![Coverage](https://img.shields.io/badge/coverage-100%25-success)
55
56
  ![Issues](https://img.shields.io/github/issues/zx80/cachetools-utils?style=flat)
56
57
  ![Python](https://img.shields.io/badge/python-3-informational)
@@ -74,12 +75,13 @@ with `cachetools`.
74
75
  class for Memcached.
75
76
  - `DictCache` a very simple `dict` cache.
76
77
 
77
- ### Other wrappers to extend cache capabilities
78
+ ### Wrappers to extend cache capabilities
78
79
 
79
80
  - `PrefixedCache`, `PrefixedMemCached` and `PrefixedRedisCache` add a prefix to
80
81
  distinguish sources on a shared cache.
81
- - `StatsCache`, `StatsMemCached` and `StatsRedisCache` add a `hits()` method
82
- to report the cache hit rate.
82
+ - `StatsCache`, `MemCached` and `RedisCache` add a `hits` method
83
+ to report the cache hit rate, `stats` to report statistics and
84
+ `reset` to reset statistics.
83
85
  - `LockedCache` use a (thread) lock to control cache accesses.
84
86
  - `TwoLevelCache` allows to combine two caches.
85
87
  - `DebugCache` to trace cache calls using `logging`.
@@ -90,6 +92,14 @@ with `cachetools`.
90
92
  function result is in cache, and to delete such an entry.
91
93
  - `cacheFunctions` and `cacheMethods`: add caching to functions or methods.
92
94
 
95
+ ## License
96
+
97
+ This code is [Public Domain](https://creativecommons.org/publicdomain/zero/1.0/).
98
+
99
+ All software has bug, this is software, hence… Beware that you may lose your
100
+ hairs or your friends because of it. If you like it, feel free to send a
101
+ postcard to the author.
102
+
93
103
  ## More
94
104
 
95
105
  See the
@@ -98,7 +108,3 @@ See the
98
108
  [issues](https://github.com/zx80/cachetools-utils/issues) on GitHub.
99
109
 
100
110
  See [packages](https://pypi.org/project/CacheToolsUtils/) on PyPI.
101
-
102
- ## License
103
-
104
- This code is public domain.
@@ -0,0 +1,6 @@
1
+ CacheToolsUtils.py,sha256=HSfiQx6NkUa0yWgyyj4j3eAMEA-QyLznrztd4QAKBtQ,19464
2
+ CacheToolsUtils-9.0.dist-info/LICENSE,sha256=y_yFW1bmBAUFCF-ZDyDiq10Ykns_19If5pFnZUzYgeU,33
3
+ CacheToolsUtils-9.0.dist-info/METADATA,sha256=MHZGUt_7_7ATiQn0ZetFecucS5Mjjs-dmqlJj3prots,4424
4
+ CacheToolsUtils-9.0.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
5
+ CacheToolsUtils-9.0.dist-info/top_level.txt,sha256=qCyE4jGot5mOmvIQrZueCnPtbYtRe2hqke4sGojm3zI,16
6
+ CacheToolsUtils-9.0.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.42.0)
2
+ Generator: setuptools (75.2.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
CacheToolsUtils.py CHANGED
@@ -25,7 +25,8 @@ _NO_DEFAULT = object()
25
25
  class _MutMapMix:
26
26
  """Convenient MutableMapping Mixin, forward to _cache."""
27
27
 
28
- _cache: MutableMapping
28
+ # FIXME coverage reports this as missing with Python 3.14
29
+ _cache: MutableMapping # pragma: no cover
29
30
 
30
31
  def __contains__(self, key):
31
32
  return self._cache.__contains__(key)
@@ -69,9 +70,12 @@ class _KeyMutMapMix(_MutMapMix):
69
70
  class _StatsMix:
70
71
  """Convenient Mixin to forward stats methods to _cache."""
71
72
 
72
- def hits(self):
73
+ def hits(self) -> float|None:
73
74
  return self._cache.hits() # type: ignore
74
75
 
76
+ def stats(self) -> dict[str, Any]:
77
+ return self._cache.stats() # type: ignore
78
+
75
79
  def reset(self):
76
80
  return self._cache.reset() # type: ignore
77
81
 
@@ -102,7 +106,7 @@ class _RedisMix: # pragma: no cover
102
106
  # CACHETOOLS EXTENSIONS
103
107
  #
104
108
 
105
- class DebugCache(MutableMapping):
109
+ class DebugCache(_StatsMix, MutableMapping):
106
110
  """Debug class.
107
111
 
108
112
  :param cache: actual cache
@@ -241,14 +245,25 @@ class StatsCache(_MutMapMix, MutableMapping):
241
245
  self._cache = cache
242
246
  self.reset()
243
247
 
244
- def hits(self):
248
+ def hits(self) -> float:
245
249
  """Return the cache hit ratio."""
246
- return float(self._hits) / float(max(self._reads, 1))
250
+ return float(self._hits) / max(self._reads, 1)
247
251
 
248
252
  def reset(self):
249
253
  """Reset internal stats data."""
250
254
  self._reads, self._writes, self._dels, self._hits = 0, 0, 0, 0
251
255
 
256
+ def stats(self) -> dict[str, Any]:
257
+ """Return available stats data as dict."""
258
+ return {
259
+ "type": 1,
260
+ "reads": self._reads,
261
+ "writes": self._writes,
262
+ "dels": self._dels,
263
+ "hits": self.hits(),
264
+ "size": self._cache.__len__()
265
+ }
266
+
252
267
  def __getitem__(self, key):
253
268
  self._reads += 1
254
269
  res = self._cache.__getitem__(key)
@@ -324,6 +339,27 @@ class TwoLevelCache(_MutMapMix, MutableMapping):
324
339
  # NOTE not passed on cache2…
325
340
  return self._cache.clear()
326
341
 
342
+ def stats(self) -> dict[str, Any]:
343
+ data = {"type": 2}
344
+ try:
345
+ data["cache1"] = self._cache.stats() # type: ignore
346
+ except Exception:
347
+ data["cache1"] = {} # type: ignore
348
+ try:
349
+ data["cache2"] = self._cache2.stats() # type: ignore
350
+ except Exception:
351
+ data["cache2"] = {} # type: ignore
352
+ return data
353
+
354
+ def hits(self) -> float|None:
355
+ data = self.stats()
356
+ c1, c2 = data["cache1"], data["cache2"]
357
+ if (c1 and "type" in c1 and c1["type"] == 1 and
358
+ c2 and "type" in c2 and c2["type"] == 1):
359
+ return float(c1["hits"] + c2["hits"]) / max(c1["reads"] + c2["reads"], 1)
360
+ # else
361
+ return None
362
+
327
363
  def reset(self): # pragma: no cover
328
364
  self._cache.reset() # type: ignore
329
365
  self._cache2.reset() # type: ignore
@@ -586,6 +622,9 @@ class RedisCache(MutableMapping):
586
622
  """Return redis informations."""
587
623
  return self._cache.info(*args, **kwargs)
588
624
 
625
+ def stats(self) -> dict[str, Any]:
626
+ return self.info(section="stats")
627
+
589
628
  def dbsize(self, *args, **kwargs):
590
629
  """Return redis database size."""
591
630
  return self._cache.dbsize(*args, **kwargs)
@@ -606,9 +645,9 @@ class RedisCache(MutableMapping):
606
645
  del self[index]
607
646
 
608
647
  # stats
609
- def hits(self):
648
+ def hits(self) -> float:
610
649
  """Return cache hits."""
611
- stats = self.info(section="stats")
650
+ stats = self.stats()
612
651
  return float(stats["keyspace_hits"]) / (
613
652
  stats["keyspace_hits"] + stats["keyspace_misses"]
614
653
  )
@@ -1,6 +0,0 @@
1
- CacheToolsUtils.py,sha256=kqZNczVcgP_F8oioXBDCqEA5Ux9FovbO_VhlJ67pjiE,18135
2
- CacheToolsUtils-8.5.dist-info/LICENSE,sha256=y_yFW1bmBAUFCF-ZDyDiq10Ykns_19If5pFnZUzYgeU,33
3
- CacheToolsUtils-8.5.dist-info/METADATA,sha256=MEAFq8ImCNmiSk49N5yWM3uZCBfHrOLmkQ5b5yVacho,4110
4
- CacheToolsUtils-8.5.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
5
- CacheToolsUtils-8.5.dist-info/top_level.txt,sha256=qCyE4jGot5mOmvIQrZueCnPtbYtRe2hqke4sGojm3zI,16
6
- CacheToolsUtils-8.5.dist-info/RECORD,,