2
2
CacheTools Utilities. This code is public domain.
3
3
"""
4
4
5
- from typing import Any , Callable , MutableMapping as MutMap
5
+ from typing import Any , Callable , MutableMapping
6
6
import abc
7
7
8
8
import cachetools
25
25
class _MutMapMix :
26
26
"""Convenient MutableMapping Mixin, forward to _cache."""
27
27
28
- _cache : MutMap
28
+ _cache : MutableMapping
29
29
30
30
def __contains__ (self , key ):
31
31
return self ._cache .__contains__ (key )
@@ -102,15 +102,15 @@ def dbsize(self, *args, **kwargs):
102
102
# CACHETOOLS EXTENSIONS
103
103
#
104
104
105
- class DebugCache (MutMap ):
105
+ class DebugCache (MutableMapping ):
106
106
"""Debug class.
107
107
108
108
:param cache: actual cache
109
109
:param log: logging instance
110
110
:param name: name of instance for output
111
111
"""
112
112
113
- def __init__ (self , cache : MutMap , log : logging .Logger , name = "cache" ):
113
+ def __init__ (self , cache : MutableMapping , log : logging .Logger , name = "cache" ):
114
114
self ._cache = cache
115
115
self ._log = log
116
116
self ._name = name
@@ -152,7 +152,7 @@ def reset(self): # pragma: no cover
152
152
return self ._cache .reset () # type: ignore
153
153
154
154
155
- class DictCache (_MutMapMix , MutMap ):
155
+ class DictCache (_MutMapMix , MutableMapping ):
156
156
"""Cache class based on dict."""
157
157
158
158
def __init__ (self ):
@@ -162,7 +162,7 @@ def clear(self):
162
162
self ._cache .clear ()
163
163
164
164
165
- class LockedCache (_MutMapMix , _StatsMix , _RedisMix , MutMap ):
165
+ class LockedCache (_MutMapMix , _StatsMix , _RedisMix , MutableMapping ):
166
166
"""Cache class with a lock.
167
167
168
168
:param cache: actual cache.
@@ -178,7 +178,7 @@ class LockedCache(_MutMapMix, _StatsMix, _RedisMix, MutMap):
178
178
cache = ctu.LockedCache(ct.LFUCache(), threading.RLock())
179
179
"""
180
180
181
- def __init__ (self , cache : MutMap , lock ):
181
+ def __init__ (self , cache : MutableMapping , lock ):
182
182
self ._cache = cache
183
183
self ._lock = lock
184
184
@@ -199,14 +199,14 @@ def __delitem__(self, key):
199
199
return self ._cache .__delitem__ (key )
200
200
201
201
202
- class PrefixedCache (_KeyMutMapMix , _StatsMix , MutMap ):
202
+ class PrefixedCache (_KeyMutMapMix , _StatsMix , MutableMapping ):
203
203
"""Cache class to add a key prefix.
204
204
205
205
:param cache: actual cache.
206
206
:param prefix: prefix to prepend to keys.
207
207
"""
208
208
209
- def __init__ (self , cache : MutMap , prefix : str | bytes = "" ):
209
+ def __init__ (self , cache : MutableMapping , prefix : str | bytes = "" ):
210
210
self ._prefix = prefix
211
211
self ._cache = cache
212
212
# dynamic cast
@@ -220,7 +220,7 @@ def _key(self, key: Any) -> Any:
220
220
return self ._prefix + self ._cast (key ) # type: ignore
221
221
222
222
223
- class StatsCache (_MutMapMix , MutMap ):
223
+ class StatsCache (_MutMapMix , MutableMapping ):
224
224
"""Cache class to keep stats.
225
225
226
226
:param cache: actual cache.
@@ -237,7 +237,7 @@ class StatsCache(_MutMapMix, MutMap):
237
237
However, this only works for its own classes.
238
238
"""
239
239
240
- def __init__ (self , cache : MutMap ):
240
+ def __init__ (self , cache : MutableMapping ):
241
241
self ._cache = cache
242
242
self .reset ()
243
243
@@ -267,15 +267,15 @@ def clear(self):
267
267
return self ._cache .clear ()
268
268
269
269
270
- class TwoLevelCache (_MutMapMix , MutMap ):
270
+ class TwoLevelCache (_MutMapMix , MutableMapping ):
271
271
"""Two-Level Cache class for CacheTools.
272
272
273
273
:param cache: first (smaller, shorter TTL) cache
274
274
:param cache2: second (larger, longer TTL) cache
275
275
:param resilient: whether to ignore cache2 failures
276
276
"""
277
277
278
- def __init__ (self , cache : MutMap , cache2 : MutMap , resilient = False ):
278
+ def __init__ (self , cache : MutableMapping , cache2 : MutableMapping , resilient = False ):
279
279
self ._resilient = resilient
280
280
self ._cache = cache
281
281
self ._cache2 = cache2
@@ -370,10 +370,10 @@ def cache_del(*args, **kwargs):
370
370
371
371
372
372
# short generator type name
373
- MapGen = Callable [[MutMap , str ], MutMap ]
373
+ MapGen = Callable [[MutableMapping , str ], MutableMapping ]
374
374
375
375
376
- def cacheMethods (cache : MutMap , obj : Any , gen : MapGen = PrefixedCache , ** funs ):
376
+ def cacheMethods (cache : MutableMapping , obj : Any , gen : MapGen = PrefixedCache , ** funs ):
377
377
"""Cache some object methods.
378
378
379
379
:param cache: cache to use.
@@ -394,7 +394,7 @@ def cacheMethods(cache: MutMap, obj: Any, gen: MapGen = PrefixedCache, **funs):
394
394
395
395
396
396
def cacheFunctions (
397
- cache : MutMap , globs : MutMap [str , Any ], gen : MapGen = PrefixedCache , ** funs
397
+ cache : MutableMapping , globs : MutableMapping [str , Any ], gen : MapGen = PrefixedCache , ** funs
398
398
):
399
399
"""Cache some global functions, with a prefix.
400
400
@@ -445,7 +445,7 @@ def deserialize(self, key, value, flag):
445
445
raise Exception ("Unknown serialization format" )
446
446
447
447
448
- class MemCached (_KeyMutMapMix , MutMap ):
448
+ class MemCached (_KeyMutMapMix , MutableMapping ):
449
449
"""MemCached-compatible wrapper class for cachetools with key encoding.
450
450
451
451
:param cache: actual memcached cache.
@@ -528,7 +528,7 @@ class StatsMemCached(MemCached):
528
528
#
529
529
530
530
531
- class RedisCache (MutMap ):
531
+ class RedisCache (MutableMapping ):
532
532
"""Redis TTL-ed wrapper for cachetools (``redis``).
533
533
534
534
:param cache: actual redis cache.
0 commit comments