cachetools 7.3.4__tar.gz → 7.3.5__tar.gz
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.
- {cachetools-7.3.4 → cachetools-7.3.5}/CHANGELOG.rst +12 -0
- {cachetools-7.3.4/src/cachetools.egg-info → cachetools-7.3.5}/PKG-INFO +1 -1
- {cachetools-7.3.4 → cachetools-7.3.5}/docs/index.rst +55 -12
- {cachetools-7.3.4 → cachetools-7.3.5}/src/cachetools/__init__.py +1 -1
- {cachetools-7.3.4 → cachetools-7.3.5}/src/cachetools/_cached.py +39 -16
- {cachetools-7.3.4 → cachetools-7.3.5}/src/cachetools/_cachedmethod.py +40 -16
- {cachetools-7.3.4 → cachetools-7.3.5/src/cachetools.egg-info}/PKG-INFO +1 -1
- {cachetools-7.3.4 → cachetools-7.3.5}/tests/__init__.py +400 -35
- cachetools-7.3.5/tests/test_cache.py +31 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/tests/test_cachedmethod.py +10 -3
- {cachetools-7.3.4 → cachetools-7.3.5}/tests/test_fifo.py +1 -23
- {cachetools-7.3.4 → cachetools-7.3.5}/tests/test_func.py +8 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/tests/test_lfu.py +1 -23
- {cachetools-7.3.4 → cachetools-7.3.5}/tests/test_lru.py +1 -23
- {cachetools-7.3.4 → cachetools-7.3.5}/tests/test_rr.py +1 -1
- {cachetools-7.3.4 → cachetools-7.3.5}/tests/test_threading.py +98 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/tests/test_tlru.py +33 -248
- cachetools-7.3.5/tests/test_ttl.py +46 -0
- cachetools-7.3.4/tests/test_cache.py +0 -9
- cachetools-7.3.4/tests/test_ttl.py +0 -358
- {cachetools-7.3.4 → cachetools-7.3.5}/LICENSE +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/MANIFEST.in +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/README.rst +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/docs/conf.py +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/pyproject.toml +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/setup.cfg +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/src/cachetools/__init__.pyi +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/src/cachetools/_descriptor.py +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/src/cachetools/func.py +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/src/cachetools/func.pyi +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/src/cachetools/keys.py +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/src/cachetools/keys.pyi +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/src/cachetools/py.typed +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/src/cachetools.egg-info/SOURCES.txt +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/src/cachetools.egg-info/dependency_links.txt +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/src/cachetools.egg-info/top_level.txt +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/tests/test_cached.py +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/tests/test_classmethod.py +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/tests/test_keys.py +0 -0
- {cachetools-7.3.4 → cachetools-7.3.5}/tox.ini +0 -0
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
v7.3.5 (2026-11-29)
|
|
2
|
+
===================
|
|
3
|
+
|
|
4
|
+
- Fix recursive calls with the same cache key waiting forever in
|
|
5
|
+
``@cached`` and ``@cachedmethod`` with ``condition``, and in the
|
|
6
|
+
``cachetools.func`` decorators (broken since v6.0.0).
|
|
7
|
+
|
|
8
|
+
- Various documentation improvements.
|
|
9
|
+
|
|
10
|
+
- Various unit test improvements.
|
|
11
|
+
|
|
12
|
+
|
|
1
13
|
v7.3.4 (2026-11-25)
|
|
2
14
|
===================
|
|
3
15
|
|
|
@@ -96,7 +96,7 @@ inserted into the cache.
|
|
|
96
96
|
tie, the least recently used of these items is discarded.
|
|
97
97
|
|
|
98
98
|
Inserting or updating a cache item via :meth:`LFUCache.__setitem__`
|
|
99
|
-
also counts as a use of the item
|
|
99
|
+
also counts as a use of the item. An update will increment the
|
|
100
100
|
item's use count.
|
|
101
101
|
|
|
102
102
|
.. versionchanged:: 7.3
|
|
@@ -156,6 +156,10 @@ inserted into the cache.
|
|
|
156
156
|
|
|
157
157
|
cache = TTLCache(maxsize=10, ttl=timedelta(hours=12), timer=datetime.now)
|
|
158
158
|
|
|
159
|
+
To copy the items of a :class:`TTLCache`, use
|
|
160
|
+
``dict(cache.items())``. Calling ``dict(cache)`` may raise
|
|
161
|
+
:exc:`KeyError` if an item expires while it is being copied.
|
|
162
|
+
|
|
159
163
|
.. method:: expire(self, time=None)
|
|
160
164
|
|
|
161
165
|
Expired items will be removed from a cache lazily, e.g. when
|
|
@@ -208,6 +212,9 @@ inserted into the cache.
|
|
|
208
212
|
expired items are there to remove, the least recently used items
|
|
209
213
|
will be discarded first to make space when necessary.
|
|
210
214
|
|
|
215
|
+
Copying the items of a :class:`TLRUCache` behaves as described for
|
|
216
|
+
:class:`TTLCache`.
|
|
217
|
+
|
|
211
218
|
.. method:: expire(self, time=None)
|
|
212
219
|
|
|
213
220
|
Expired items will be removed from a cache lazily, e.g. when
|
|
@@ -295,6 +302,36 @@ any more, and will lead to surprising results when used with any of
|
|
|
295
302
|
the memoizing decorators described below. However, it may be useful
|
|
296
303
|
in its own right.
|
|
297
304
|
|
|
305
|
+
A shallow copy of a cache, e.g. one made with :func:`copy.copy`,
|
|
306
|
+
shares any attributes added by a subclass with the original. A
|
|
307
|
+
subclass with mutable state of its own should therefore extend
|
|
308
|
+
:meth:`__copy__`:
|
|
309
|
+
|
|
310
|
+
.. doctest::
|
|
311
|
+
|
|
312
|
+
>>> class EvictionLog(LRUCache):
|
|
313
|
+
... def __init__(self, maxsize):
|
|
314
|
+
... super().__init__(maxsize)
|
|
315
|
+
... self.evicted = []
|
|
316
|
+
...
|
|
317
|
+
... def popitem(self):
|
|
318
|
+
... key, value = super().popitem()
|
|
319
|
+
... self.evicted.append(key)
|
|
320
|
+
... return key, value
|
|
321
|
+
...
|
|
322
|
+
... def __copy__(self):
|
|
323
|
+
... new = super().__copy__()
|
|
324
|
+
... new.evicted = self.evicted.copy()
|
|
325
|
+
... return new
|
|
326
|
+
|
|
327
|
+
>>> import copy
|
|
328
|
+
>>> c = EvictionLog(maxsize=1)
|
|
329
|
+
>>> c['a'] = 1
|
|
330
|
+
>>> d = copy.copy(c)
|
|
331
|
+
>>> d['b'] = 2
|
|
332
|
+
>>> c.evicted, d.evicted
|
|
333
|
+
([], ['a'])
|
|
334
|
+
|
|
298
335
|
|
|
299
336
|
Memoizing decorators
|
|
300
337
|
====================
|
|
@@ -348,14 +385,13 @@ often called with the same arguments:
|
|
|
348
385
|
If `condition` is not :const:`None`, it must specify a `condition
|
|
349
386
|
variable`_, i.e. an object providing :meth:`wait()`,
|
|
350
387
|
:meth:`wait_for()`, :meth:`notify()` and :meth:`notify_all()`
|
|
351
|
-
methods as defined by :class:`threading.Condition`.
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
function result.
|
|
388
|
+
methods as defined by :class:`threading.Condition`. When one
|
|
389
|
+
thread is computing a result for a cache key, other threads
|
|
390
|
+
calling the same wrapper with that key wait, then check the cache
|
|
391
|
+
again. If the computing thread calls the wrapper recursively with
|
|
392
|
+
the same key, the recursive call runs uncached and counts as a
|
|
393
|
+
miss when `info=True`. Only the outermost call can store a result
|
|
394
|
+
for that key.
|
|
359
395
|
|
|
360
396
|
.. note::
|
|
361
397
|
|
|
@@ -376,7 +412,7 @@ often called with the same arguments:
|
|
|
376
412
|
guards the cache. In practice, when using
|
|
377
413
|
:class:`threading.Lock` and :class:`threading.Condition`, the
|
|
378
414
|
condition variable must be created with the corresponding lock,
|
|
379
|
-
e.g. ``condition=threading.Condition(lock)
|
|
415
|
+
e.g. ``condition=threading.Condition(lock)``. An unrelated
|
|
380
416
|
:class:`threading.Condition` will raise :exc:`RuntimeError`.
|
|
381
417
|
On current CPython versions, passing the lock separately this
|
|
382
418
|
way is also slightly faster than providing `condition` alone,
|
|
@@ -386,8 +422,8 @@ often called with the same arguments:
|
|
|
386
422
|
The decorator's `cache`, `key`, `lock` and `condition` parameters
|
|
387
423
|
are also available as :attr:`cache`, :attr:`cache_key`,
|
|
388
424
|
:attr:`cache_lock` and :attr:`cache_condition` attributes of the
|
|
389
|
-
memoizing
|
|
390
|
-
|
|
425
|
+
memoizing callable. These can be used for clearing the cache or
|
|
426
|
+
invalidating individual cache items, for example.
|
|
391
427
|
|
|
392
428
|
.. testcode::
|
|
393
429
|
|
|
@@ -441,6 +477,10 @@ often called with the same arguments:
|
|
|
441
477
|
:attr:`__wrapped__` attribute. This can be used for introspection
|
|
442
478
|
or for bypassing the cache.
|
|
443
479
|
|
|
480
|
+
Decorating instance methods with :func:`cached` is not
|
|
481
|
+
recommended, since the cache is then shared by all instances of
|
|
482
|
+
the class. Use :func:`cachedmethod` for instance methods instead.
|
|
483
|
+
|
|
444
484
|
It is also possible to use a single shared cache object with
|
|
445
485
|
multiple functions. However, care must be taken that different
|
|
446
486
|
cache keys are generated for each function, even for identical
|
|
@@ -828,6 +868,9 @@ performance and clear the cache. Please see the
|
|
|
828
868
|
:func:`functools.lru_cache` documentation for details. Also note that
|
|
829
869
|
all the decorators in this module are thread-safe.
|
|
830
870
|
|
|
871
|
+
As with :func:`functools.lru_cache`, decorating a method shares the
|
|
872
|
+
cache among all instances of its class.
|
|
873
|
+
|
|
831
874
|
|
|
832
875
|
.. decorator:: fifo_cache(user_function)
|
|
833
876
|
fifo_cache(maxsize=128, typed=False)
|
|
@@ -18,8 +18,10 @@ import warnings
|
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
def _condition_info(func, cache, key, lock, cond, info):
|
|
21
|
+
from threading import get_ident
|
|
22
|
+
|
|
21
23
|
hits = misses = 0
|
|
22
|
-
pending =
|
|
24
|
+
pending = {} # maps keys to the calling thread
|
|
23
25
|
|
|
24
26
|
def wrapper(*args, **kwargs):
|
|
25
27
|
nonlocal hits, misses
|
|
@@ -27,14 +29,24 @@ def _condition_info(func, cache, key, lock, cond, info):
|
|
|
27
29
|
with lock:
|
|
28
30
|
# apparently significantly faster than cond.wait_for(...)
|
|
29
31
|
while k in pending:
|
|
32
|
+
if pending[k] == get_ident():
|
|
33
|
+
# A recursive call would wait for its own pending
|
|
34
|
+
# call forever, so it calls the function again.
|
|
35
|
+
misses += 1
|
|
36
|
+
recursive = True
|
|
37
|
+
break
|
|
30
38
|
cond.wait()
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
else:
|
|
40
|
+
try:
|
|
41
|
+
result = cache[k]
|
|
42
|
+
hits += 1
|
|
43
|
+
return result
|
|
44
|
+
except KeyError:
|
|
45
|
+
pending[k] = get_ident()
|
|
46
|
+
misses += 1
|
|
47
|
+
recursive = False
|
|
48
|
+
if recursive:
|
|
49
|
+
return func(*args, **kwargs)
|
|
38
50
|
try:
|
|
39
51
|
v = func(*args, **kwargs)
|
|
40
52
|
with lock:
|
|
@@ -45,7 +57,7 @@ def _condition_info(func, cache, key, lock, cond, info):
|
|
|
45
57
|
return v
|
|
46
58
|
finally:
|
|
47
59
|
with lock:
|
|
48
|
-
pending
|
|
60
|
+
del pending[k]
|
|
49
61
|
cond.notify_all()
|
|
50
62
|
|
|
51
63
|
def cache_clear():
|
|
@@ -155,19 +167,30 @@ def _uncached_info(func, info):
|
|
|
155
167
|
|
|
156
168
|
|
|
157
169
|
def _condition(func, cache, key, lock, cond):
|
|
158
|
-
|
|
170
|
+
from threading import get_ident
|
|
171
|
+
|
|
172
|
+
pending = {} # maps keys to the calling thread
|
|
159
173
|
|
|
160
174
|
def wrapper(*args, **kwargs):
|
|
161
175
|
k = key(*args, **kwargs)
|
|
162
176
|
with lock:
|
|
163
177
|
# apparently significantly faster than cond.wait_for(...)
|
|
164
178
|
while k in pending:
|
|
179
|
+
if pending[k] == get_ident():
|
|
180
|
+
# A recursive call would wait for its own pending
|
|
181
|
+
# call forever, so it calls the function again.
|
|
182
|
+
recursive = True
|
|
183
|
+
break
|
|
165
184
|
cond.wait()
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
185
|
+
else:
|
|
186
|
+
try:
|
|
187
|
+
result = cache[k]
|
|
188
|
+
return result
|
|
189
|
+
except KeyError:
|
|
190
|
+
pending[k] = get_ident()
|
|
191
|
+
recursive = False
|
|
192
|
+
if recursive:
|
|
193
|
+
return func(*args, **kwargs)
|
|
171
194
|
try:
|
|
172
195
|
v = func(*args, **kwargs)
|
|
173
196
|
with lock:
|
|
@@ -178,7 +201,7 @@ def _condition(func, cache, key, lock, cond):
|
|
|
178
201
|
return v
|
|
179
202
|
finally:
|
|
180
203
|
with lock:
|
|
181
|
-
pending
|
|
204
|
+
del pending[k]
|
|
182
205
|
cond.notify_all()
|
|
183
206
|
|
|
184
207
|
def cache_clear():
|
|
@@ -65,12 +65,14 @@ class _WrapperBase:
|
|
|
65
65
|
|
|
66
66
|
|
|
67
67
|
def _condition_info(method, cache, key, lock, cond, info):
|
|
68
|
+
from threading import get_ident
|
|
69
|
+
|
|
68
70
|
class Descriptor(_MethodDescriptor):
|
|
69
71
|
class Wrapper(_WrapperBase):
|
|
70
72
|
def __init__(self, obj, descriptor):
|
|
71
73
|
super().__init__(obj, descriptor, method, cache, key, lock, cond)
|
|
72
74
|
self.__hits = self.__misses = 0
|
|
73
|
-
self.__pending =
|
|
75
|
+
self.__pending = {} # maps keys to the calling thread
|
|
74
76
|
|
|
75
77
|
def __call__(self, /, *args, **kwargs):
|
|
76
78
|
cache = self.cache
|
|
@@ -81,14 +83,25 @@ def _condition_info(method, cache, key, lock, cond, info):
|
|
|
81
83
|
with lock:
|
|
82
84
|
# apparently significantly faster than cond.wait_for(...)
|
|
83
85
|
while key in self.__pending:
|
|
86
|
+
if self.__pending[key] == get_ident():
|
|
87
|
+
# A recursive call would wait for its own
|
|
88
|
+
# pending call forever, so it calls the
|
|
89
|
+
# method again.
|
|
90
|
+
self.__misses += 1
|
|
91
|
+
recursive = True
|
|
92
|
+
break
|
|
84
93
|
cond.wait()
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
94
|
+
else:
|
|
95
|
+
try:
|
|
96
|
+
result = cache[key]
|
|
97
|
+
self.__hits += 1
|
|
98
|
+
return result
|
|
99
|
+
except KeyError:
|
|
100
|
+
self.__pending[key] = get_ident()
|
|
101
|
+
self.__misses += 1
|
|
102
|
+
recursive = False
|
|
103
|
+
if recursive:
|
|
104
|
+
return method(self._obj, *args, **kwargs)
|
|
92
105
|
try:
|
|
93
106
|
val = method(self._obj, *args, **kwargs)
|
|
94
107
|
with lock:
|
|
@@ -99,7 +112,7 @@ def _condition_info(method, cache, key, lock, cond, info):
|
|
|
99
112
|
return val
|
|
100
113
|
finally:
|
|
101
114
|
with lock:
|
|
102
|
-
self.__pending
|
|
115
|
+
del self.__pending[key]
|
|
103
116
|
cond.notify_all()
|
|
104
117
|
|
|
105
118
|
def cache_clear(self):
|
|
@@ -188,6 +201,8 @@ def _unlocked_info(method, cache, key, info):
|
|
|
188
201
|
|
|
189
202
|
|
|
190
203
|
def _condition(method, cache, key, lock, cond):
|
|
204
|
+
from threading import get_ident
|
|
205
|
+
|
|
191
206
|
# backward-compatible weakref dictionary for Python >= 3.13
|
|
192
207
|
pending = weakref.WeakKeyDictionary()
|
|
193
208
|
|
|
@@ -197,11 +212,20 @@ def _condition(method, cache, key, lock, cond):
|
|
|
197
212
|
with lock(self):
|
|
198
213
|
# apparently significantly faster than cond.wait_for(...)
|
|
199
214
|
while k in pending:
|
|
215
|
+
if pending[k] == get_ident():
|
|
216
|
+
# A recursive call would wait for its own pending
|
|
217
|
+
# call forever, so it calls the method again.
|
|
218
|
+
recursive = True
|
|
219
|
+
break
|
|
200
220
|
cond(self).wait()
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
221
|
+
else:
|
|
222
|
+
try:
|
|
223
|
+
return c[k]
|
|
224
|
+
except KeyError:
|
|
225
|
+
pending[k] = get_ident()
|
|
226
|
+
recursive = False
|
|
227
|
+
if recursive:
|
|
228
|
+
return method(self, *args, **kwargs)
|
|
205
229
|
try:
|
|
206
230
|
v = method(self, *args, **kwargs)
|
|
207
231
|
with lock(self):
|
|
@@ -212,7 +236,7 @@ def _condition(method, cache, key, lock, cond):
|
|
|
212
236
|
return v
|
|
213
237
|
finally:
|
|
214
238
|
with lock(self):
|
|
215
|
-
pending
|
|
239
|
+
del pending[k]
|
|
216
240
|
cond(self).notify_all()
|
|
217
241
|
|
|
218
242
|
def cache_clear(self):
|
|
@@ -221,14 +245,14 @@ def _condition(method, cache, key, lock, cond):
|
|
|
221
245
|
c.clear()
|
|
222
246
|
|
|
223
247
|
def classmethod_wrapper(self, /, *args, **kwargs):
|
|
224
|
-
p = pending.setdefault(self,
|
|
248
|
+
p = pending.setdefault(self, {})
|
|
225
249
|
return wrapper(self, p, *args, **kwargs)
|
|
226
250
|
|
|
227
251
|
class Descriptor(_ClassMethodDescriptor):
|
|
228
252
|
class Wrapper(_WrapperBase):
|
|
229
253
|
def __init__(self, obj, descriptor):
|
|
230
254
|
super().__init__(obj, descriptor, method, cache, key, lock, cond)
|
|
231
|
-
self.__pending =
|
|
255
|
+
self.__pending = {} # maps keys to the calling thread
|
|
232
256
|
|
|
233
257
|
def __call__(self, /, *args, **kwargs):
|
|
234
258
|
return wrapper(self._obj, self.__pending, *args, **kwargs)
|