CacheToolsUtils 10.3__py3-none-any.whl → 10.4__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.
CacheToolsUtils.py CHANGED
@@ -2,6 +2,9 @@
2
2
  CacheTools Utilities. This code is public domain.
3
3
  """
4
4
 
5
+ # FIXME 3.13t checks without Cipher package
6
+ # pyright: reportMissingImports=false
7
+
5
8
  from typing import Any, Callable, MutableMapping
6
9
  import abc
7
10
 
@@ -286,19 +289,25 @@ class StatsCache(_MutMapMix, MutableMapping):
286
289
 
287
290
  def reset(self):
288
291
  """Reset internal stats data."""
289
- self._reads, self._writes, self._dels, self._hits = 0, 0, 0, 0
292
+ self._tests, self._reads, self._writes, self._dels, self._hits = 0, 0, 0, 0, 0
290
293
 
291
294
  def stats(self) -> dict[str, Any]:
292
295
  """Return available stats data as dict."""
293
296
  return {
294
297
  "type": 1,
298
+ "tests": self._tests,
295
299
  "reads": self._reads,
296
300
  "writes": self._writes,
297
301
  "dels": self._dels,
298
- "hits": self.hits(),
302
+ "hits": self._hits,
303
+ "hit_rate": self.hits(),
299
304
  "size": self._cache.__len__()
300
305
  }
301
306
 
307
+ def __contains__(self, key):
308
+ self._tests += 1
309
+ return self._cache.__contains__(key)
310
+
302
311
  def __getitem__(self, key):
303
312
  self._reads += 1
304
313
  res = self._cache.__getitem__(key)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: CacheToolsUtils
3
- Version: 10.3
3
+ Version: 10.4
4
4
  Summary: Cachetools Utilities
5
5
  Author-email: Fabien Coelho <cachetools.utils@coelho.net>
6
6
  License: CC0
@@ -26,8 +26,6 @@ Requires-Dist: ruff; extra == "dev"
26
26
  Requires-Dist: pytest; extra == "dev"
27
27
  Requires-Dist: coverage; extra == "dev"
28
28
  Requires-Dist: pymarkdownlnt; extra == "dev"
29
- Requires-Dist: types-redis; extra == "dev"
30
- Requires-Dist: types-cachetools; extra == "dev"
31
29
  Provides-Extra: doc
32
30
  Requires-Dist: sphinx; extra == "doc"
33
31
  Requires-Dist: sphinx_rtd_theme; extra == "doc"
@@ -46,14 +44,12 @@ Requires-Dist: twine; extra == "pub"
46
44
 
47
45
  # CacheToolsUtils
48
46
 
49
- Classes to add key prefix and stats to
50
- [cachetools](https://pypi.org/project/cachetools/) classes and use
51
- [redis](https://redis.io/) and
52
- [memcached](https://memcached.org/) as storage backends,
53
- and other cache-related utils.
47
+ Stackable cache classes for sharing, encryption, statistics _and more_
48
+ on top of [cachetools](https://pypi.org/project/cachetools/),
49
+ [redis](https://redis.io/) and [memcached](https://memcached.org/).
54
50
 
55
- ![Status](https://github.com/zx80/cachetools-utils/actions/workflows/cachetools-utils.yml/badge.svg?branch=main&style=flat)
56
- ![Tests](https://img.shields.io/badge/tests-23%20✓-success)
51
+ ![Status](https://github.com/zx80/cachetools-utils/actions/workflows/ctu.yml/badge.svg?branch=main&style=flat)
52
+ ![Tests](https://img.shields.io/badge/tests-25%20✓-success)
57
53
  ![Coverage](https://img.shields.io/badge/coverage-100%25-success)
58
54
  ![Issues](https://img.shields.io/github/issues/zx80/cachetools-utils?style=flat)
59
55
  ![Python](https://img.shields.io/badge/python-3-informational)
@@ -66,7 +62,37 @@ and other cache-related utils.
66
62
  For our purpose, a cache is a key-value store, aka a dictionary, possibly with
67
63
  some constraints on keys (type, size) and values (size, serialization).
68
64
  This module provides new caches, wrappers and other utilities suitable to use
69
- with `cachetools`.
65
+ with `cachetools`. Example:
66
+
67
+ ```python
68
+ import redis
69
+ import CacheToolsUtils as ctu
70
+ import config
71
+
72
+ redis_storage = redis.Redis(**config.REDIS_SERVER)
73
+ storage = ctu.RedisCache(redis_storage, raw=True, ttl=120)
74
+ crypto = ctu.EncryptedCache(storage, config.CACHE_KEY)
75
+ cache = ctu.ToBytesCache(crypto)
76
+
77
+ @ctu.cached(ctu.AutoPrefixedCache(cache), key=ctu.json_key)
78
+ def repeat(s: str, n: int) -> str:
79
+ return s * n
80
+
81
+ @ctu.cached(ctu.AutoPrefixedCache(cache), key=ctu.json_key)
82
+ def banged(s: str, n: int) -> str:
83
+ return repeat(s, n) + "!"
84
+
85
+ @ctu.cached(ctu.AutoPrefixedCache(cache), key=ctu.json_key)
86
+ def question(s: str, n: int) -> str:
87
+ return repeat(s, n) + "?"
88
+
89
+ print(banged("aa", 3)) # add 2 cache entries
90
+ print(question("aa", 3)) # add 1 entry, 1 hit
91
+ print(repeat("aa", 3)) # cache hit!
92
+ print(banged("aa", 3)) # cache hit!
93
+
94
+ assert cache.hits() > 0
95
+ ```
70
96
 
71
97
  ### Cache classes
72
98
 
@@ -0,0 +1,6 @@
1
+ CacheToolsUtils.py,sha256=uOSsktLdbzvWfBNTmMPNcPQxOHgoQclmxIYtMCFptH8,27657
2
+ cachetoolsutils-10.4.dist-info/LICENSE,sha256=y_yFW1bmBAUFCF-ZDyDiq10Ykns_19If5pFnZUzYgeU,33
3
+ cachetoolsutils-10.4.dist-info/METADATA,sha256=YKZ44SQN-vim2aYm4GrKhYuchKNtIxm9hCPgYzMJ2XU,5527
4
+ cachetoolsutils-10.4.dist-info/WHEEL,sha256=jB7zZ3N9hIM9adW7qlTAyycLYW9npaWKLRzaoVcLKcM,91
5
+ cachetoolsutils-10.4.dist-info/top_level.txt,sha256=qCyE4jGot5mOmvIQrZueCnPtbYtRe2hqke4sGojm3zI,16
6
+ cachetoolsutils-10.4.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- CacheToolsUtils.py,sha256=Dc6S3Rc6MUgcepInpojzxIwyQg55tQTrLIPfaP8qgEo,27384
2
- cachetoolsutils-10.3.dist-info/LICENSE,sha256=y_yFW1bmBAUFCF-ZDyDiq10Ykns_19If5pFnZUzYgeU,33
3
- cachetoolsutils-10.3.dist-info/METADATA,sha256=BNt4H36KrXvRJoXGCmYJCDHZtZMJjZcujPoLhUrqWB4,4809
4
- cachetoolsutils-10.3.dist-info/WHEEL,sha256=jB7zZ3N9hIM9adW7qlTAyycLYW9npaWKLRzaoVcLKcM,91
5
- cachetoolsutils-10.3.dist-info/top_level.txt,sha256=qCyE4jGot5mOmvIQrZueCnPtbYtRe2hqke4sGojm3zI,16
6
- cachetoolsutils-10.3.dist-info/RECORD,,