CacheToolsUtils 10.2__py3-none-any.whl → 10.3__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
@@ -226,6 +226,39 @@ class PrefixedCache(_KeyMutMapMix, _StatsMix, MutableMapping):
226
226
  return self._prefix + self._cast(key) # type: ignore
227
227
 
228
228
 
229
+ class AutoPrefixedCache(PrefixedCache):
230
+ """Cache class with an automatic counter-based string prefix.
231
+
232
+ :param cache: actual cache.
233
+ :param sep: prefix separator, default is ".".
234
+ :param method: encoding method in "b64", "b64u", "b32", "b32x", "b16",
235
+ "a85" and "b85". Default is "b64".
236
+ """
237
+
238
+ _COUNTER: int = 0
239
+
240
+ _METHODS: dict[str, Callable[[Any], bytes]] = {
241
+ "b64": base64.b64encode,
242
+ "b64u": base64.urlsafe_b64encode,
243
+ "b32": base64.b32encode,
244
+ "b32x": base64.b32hexencode,
245
+ "b16": base64.b16encode,
246
+ "a85": base64.a85encode,
247
+ "b85": base64.b85encode,
248
+ # "z85": base64.z85encode, # Python 3.13
249
+ }
250
+
251
+ def __init__(self, cache: MutableMapping, sep: str = ".", method: str = "b64"):
252
+ if method not in AutoPrefixedCache._METHODS:
253
+ raise Exception(f"invalid conversion method: {method}")
254
+ encoder = AutoPrefixedCache._METHODS[method]
255
+ length = max(1, (AutoPrefixedCache._COUNTER.bit_length() + 7) // 8)
256
+ # NOTE byteorder default added to 3.11
257
+ prefix = encoder(AutoPrefixedCache._COUNTER.to_bytes(length, byteorder="big")).decode("ASCII")
258
+ AutoPrefixedCache._COUNTER += 1
259
+ super().__init__(cache, prefix + sep)
260
+
261
+
229
262
  class StatsCache(_MutMapMix, MutableMapping):
230
263
  """Cache class to keep stats.
231
264
 
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: CacheToolsUtils
3
- Version: 10.2
3
+ Version: 10.3
4
4
  Summary: Cachetools Utilities
5
5
  Author-email: Fabien Coelho <cachetools.utils@coelho.net>
6
6
  License: CC0
@@ -53,7 +53,7 @@ Classes to add key prefix and stats to
53
53
  and other cache-related utils.
54
54
 
55
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-22%20✓-success)
56
+ ![Tests](https://img.shields.io/badge/tests-23%20✓-success)
57
57
  ![Coverage](https://img.shields.io/badge/coverage-100%25-success)
58
58
  ![Issues](https://img.shields.io/github/issues/zx80/cachetools-utils?style=flat)
59
59
  ![Python](https://img.shields.io/badge/python-3-informational)
@@ -81,6 +81,7 @@ with `cachetools`.
81
81
 
82
82
  - `PrefixedCache`, `PrefixedMemCached` and `PrefixedRedisCache` add a prefix to
83
83
  distinguish sources on a shared cache.
84
+ - `AutoPrefixedCache` add a counter-based prefix.
84
85
  - `StatsCache`, `MemCached` and `RedisCache` add a `hits` method
85
86
  to report the cache hit rate, `stats` to report statistics and
86
87
  `reset` to reset statistics.
@@ -0,0 +1,6 @@
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,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.6.0)
2
+ Generator: setuptools (75.8.2)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,6 +0,0 @@
1
- CacheToolsUtils.py,sha256=etJrt34OkZtSMXK1wGJIg9VcDYxH3pXzpJy6OowAMnY,26120
2
- CacheToolsUtils-10.2.dist-info/LICENSE,sha256=y_yFW1bmBAUFCF-ZDyDiq10Ykns_19If5pFnZUzYgeU,33
3
- CacheToolsUtils-10.2.dist-info/METADATA,sha256=rEY3PJdfjL-E-QVqAt36vb7voJqxZYaeqze-Lk7YuQ0,4759
4
- CacheToolsUtils-10.2.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
5
- CacheToolsUtils-10.2.dist-info/top_level.txt,sha256=qCyE4jGot5mOmvIQrZueCnPtbYtRe2hqke4sGojm3zI,16
6
- CacheToolsUtils-10.2.dist-info/RECORD,,