cachetools 7.3.3__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.
Files changed (40) hide show
  1. {cachetools-7.3.3 → cachetools-7.3.5}/CHANGELOG.rst +20 -0
  2. {cachetools-7.3.3/src/cachetools.egg-info → cachetools-7.3.5}/PKG-INFO +1 -1
  3. {cachetools-7.3.3 → cachetools-7.3.5}/docs/index.rst +65 -12
  4. {cachetools-7.3.3 → cachetools-7.3.5}/src/cachetools/__init__.py +1 -1
  5. {cachetools-7.3.3 → cachetools-7.3.5}/src/cachetools/_cached.py +39 -16
  6. {cachetools-7.3.3 → cachetools-7.3.5}/src/cachetools/_cachedmethod.py +40 -16
  7. {cachetools-7.3.3 → cachetools-7.3.5}/src/cachetools/func.py +9 -5
  8. {cachetools-7.3.3 → cachetools-7.3.5/src/cachetools.egg-info}/PKG-INFO +1 -1
  9. {cachetools-7.3.3 → cachetools-7.3.5}/tests/__init__.py +400 -35
  10. cachetools-7.3.5/tests/test_cache.py +31 -0
  11. {cachetools-7.3.3 → cachetools-7.3.5}/tests/test_cachedmethod.py +10 -3
  12. {cachetools-7.3.3 → cachetools-7.3.5}/tests/test_fifo.py +1 -23
  13. {cachetools-7.3.3 → cachetools-7.3.5}/tests/test_func.py +8 -0
  14. {cachetools-7.3.3 → cachetools-7.3.5}/tests/test_lfu.py +1 -23
  15. {cachetools-7.3.3 → cachetools-7.3.5}/tests/test_lru.py +1 -23
  16. {cachetools-7.3.3 → cachetools-7.3.5}/tests/test_rr.py +1 -1
  17. {cachetools-7.3.3 → cachetools-7.3.5}/tests/test_threading.py +98 -0
  18. {cachetools-7.3.3 → cachetools-7.3.5}/tests/test_tlru.py +33 -248
  19. cachetools-7.3.5/tests/test_ttl.py +46 -0
  20. cachetools-7.3.3/tests/test_cache.py +0 -9
  21. cachetools-7.3.3/tests/test_ttl.py +0 -358
  22. {cachetools-7.3.3 → cachetools-7.3.5}/LICENSE +0 -0
  23. {cachetools-7.3.3 → cachetools-7.3.5}/MANIFEST.in +0 -0
  24. {cachetools-7.3.3 → cachetools-7.3.5}/README.rst +0 -0
  25. {cachetools-7.3.3 → cachetools-7.3.5}/docs/conf.py +0 -0
  26. {cachetools-7.3.3 → cachetools-7.3.5}/pyproject.toml +0 -0
  27. {cachetools-7.3.3 → cachetools-7.3.5}/setup.cfg +0 -0
  28. {cachetools-7.3.3 → cachetools-7.3.5}/src/cachetools/__init__.pyi +0 -0
  29. {cachetools-7.3.3 → cachetools-7.3.5}/src/cachetools/_descriptor.py +0 -0
  30. {cachetools-7.3.3 → cachetools-7.3.5}/src/cachetools/func.pyi +0 -0
  31. {cachetools-7.3.3 → cachetools-7.3.5}/src/cachetools/keys.py +0 -0
  32. {cachetools-7.3.3 → cachetools-7.3.5}/src/cachetools/keys.pyi +0 -0
  33. {cachetools-7.3.3 → cachetools-7.3.5}/src/cachetools/py.typed +0 -0
  34. {cachetools-7.3.3 → cachetools-7.3.5}/src/cachetools.egg-info/SOURCES.txt +0 -0
  35. {cachetools-7.3.3 → cachetools-7.3.5}/src/cachetools.egg-info/dependency_links.txt +0 -0
  36. {cachetools-7.3.3 → cachetools-7.3.5}/src/cachetools.egg-info/top_level.txt +0 -0
  37. {cachetools-7.3.3 → cachetools-7.3.5}/tests/test_cached.py +0 -0
  38. {cachetools-7.3.3 → cachetools-7.3.5}/tests/test_classmethod.py +0 -0
  39. {cachetools-7.3.3 → cachetools-7.3.5}/tests/test_keys.py +0 -0
  40. {cachetools-7.3.3 → cachetools-7.3.5}/tox.ini +0 -0
@@ -1,3 +1,23 @@
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
+
13
+ v7.3.4 (2026-11-25)
14
+ ===================
15
+
16
+ - Minor ``cachetools.func`` performance improvements.
17
+
18
+ - Minor documentation improvements.
19
+
20
+
1
21
  v7.3.3 (2026-11-18)
2
22
  ===================
3
23
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cachetools
3
- Version: 7.3.3
3
+ Version: 7.3.5
4
4
  Summary: Extensible memoizing collections and decorators
5
5
  Author-email: Thomas Kemmer <tkemmer@computer.org>
6
6
  Maintainer-email: Thomas Kemmer <tkemmer@computer.org>
@@ -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; an update will increment the
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
@@ -110,6 +110,12 @@ inserted into the cache.
110
110
  This class discards the least recently used items first to make
111
111
  space when necessary.
112
112
 
113
+ Retrieving an item via :meth:`LRUCache.__getitem__` marks it as
114
+ most recently used, as does inserting or updating it. Since
115
+ :meth:`items` and :meth:`values` retrieve every item in turn,
116
+ iterating over these views leaves the cache discarding items in
117
+ insertion order.
118
+
113
119
  .. autoclass:: RRCache(maxsize, choice=random.choice, getsizeof=None)
114
120
  :members: choice, popitem
115
121
 
@@ -150,6 +156,10 @@ inserted into the cache.
150
156
 
151
157
  cache = TTLCache(maxsize=10, ttl=timedelta(hours=12), timer=datetime.now)
152
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
+
153
163
  .. method:: expire(self, time=None)
154
164
 
155
165
  Expired items will be removed from a cache lazily, e.g. when
@@ -202,6 +212,9 @@ inserted into the cache.
202
212
  expired items are there to remove, the least recently used items
203
213
  will be discarded first to make space when necessary.
204
214
 
215
+ Copying the items of a :class:`TLRUCache` behaves as described for
216
+ :class:`TTLCache`.
217
+
205
218
  .. method:: expire(self, time=None)
206
219
 
207
220
  Expired items will be removed from a cache lazily, e.g. when
@@ -289,6 +302,36 @@ any more, and will lead to surprising results when used with any of
289
302
  the memoizing decorators described below. However, it may be useful
290
303
  in its own right.
291
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
+
292
335
 
293
336
  Memoizing decorators
294
337
  ====================
@@ -342,14 +385,13 @@ often called with the same arguments:
342
385
  If `condition` is not :const:`None`, it must specify a `condition
343
386
  variable`_, i.e. an object providing :meth:`wait()`,
344
387
  :meth:`wait_for()`, :meth:`notify()` and :meth:`notify_all()`
345
- methods as defined by :class:`threading.Condition`. Using a
346
- `condition` variable will prevent concurrent execution of the
347
- wrapped function with *identical* parameters, or cache keys.
348
- Instead, a calling thread will check if an identical function call
349
- is already executing, and will then wait for the pending call to
350
- finish. The executing thread will notify any waiting threads as
351
- soon as the function completes, which will then return the cached
352
- 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.
353
395
 
354
396
  .. note::
355
397
 
@@ -370,14 +412,18 @@ often called with the same arguments:
370
412
  guards the cache. In practice, when using
371
413
  :class:`threading.Lock` and :class:`threading.Condition`, the
372
414
  condition variable must be created with the corresponding lock,
373
- e.g. ``condition=threading.Condition(lock)``; an unrelated
415
+ e.g. ``condition=threading.Condition(lock)``. An unrelated
374
416
  :class:`threading.Condition` will raise :exc:`RuntimeError`.
417
+ On current CPython versions, passing the lock separately this
418
+ way is also slightly faster than providing `condition` alone,
419
+ since acquiring the lock through a :class:`threading.Condition`
420
+ adds Python-level method calls on every cache access.
375
421
 
376
422
  The decorator's `cache`, `key`, `lock` and `condition` parameters
377
423
  are also available as :attr:`cache`, :attr:`cache_key`,
378
424
  :attr:`cache_lock` and :attr:`cache_condition` attributes of the
379
- memoizing wrapper function. These can be used for clearing the
380
- cache or invalidating individual cache items, for example.
425
+ memoizing callable. These can be used for clearing the cache or
426
+ invalidating individual cache items, for example.
381
427
 
382
428
  .. testcode::
383
429
 
@@ -431,6 +477,10 @@ often called with the same arguments:
431
477
  :attr:`__wrapped__` attribute. This can be used for introspection
432
478
  or for bypassing the cache.
433
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
+
434
484
  It is also possible to use a single shared cache object with
435
485
  multiple functions. However, care must be taken that different
436
486
  cache keys are generated for each function, even for identical
@@ -818,6 +868,9 @@ performance and clear the cache. Please see the
818
868
  :func:`functools.lru_cache` documentation for details. Also note that
819
869
  all the decorators in this module are thread-safe.
820
870
 
871
+ As with :func:`functools.lru_cache`, decorating a method shares the
872
+ cache among all instances of its class.
873
+
821
874
 
822
875
  .. decorator:: fifo_cache(user_function)
823
876
  fifo_cache(maxsize=128, typed=False)
@@ -12,7 +12,7 @@ __all__ = (
12
12
  "cachedmethod",
13
13
  )
14
14
 
15
- __version__ = "7.3.3"
15
+ __version__ = "7.3.5"
16
16
 
17
17
  import collections
18
18
  import collections.abc
@@ -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 = set()
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
- try:
32
- result = cache[k]
33
- hits += 1
34
- return result
35
- except KeyError:
36
- pending.add(k)
37
- misses += 1
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.remove(k)
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
- pending = set()
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
- try:
167
- result = cache[k]
168
- return result
169
- except KeyError:
170
- pending.add(k)
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.remove(k)
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 = set()
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
- try:
86
- result = cache[key]
87
- self.__hits += 1
88
- return result
89
- except KeyError:
90
- self.__pending.add(key)
91
- self.__misses += 1
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.remove(key)
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
- try:
202
- return c[k]
203
- except KeyError:
204
- pending.add(k)
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.remove(k)
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, set())
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 = set()
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)
@@ -5,7 +5,7 @@ __all__ = ("fifo_cache", "lfu_cache", "lru_cache", "rr_cache", "ttl_cache")
5
5
  import math
6
6
  import random
7
7
  import time
8
- from threading import Condition
8
+ from threading import Condition, RLock
9
9
 
10
10
  from . import FIFOCache, LFUCache, LRUCache, RRCache, TTLCache, cached, keys
11
11
 
@@ -21,11 +21,15 @@ class _UnboundTTLCache(TTLCache):
21
21
 
22
22
  def _cache(cache, maxsize, typed):
23
23
  def decorator(func):
24
- # like functools.lru_cache, this has to be thread-safe;
25
- # additionally, this also prevents cache stampede scenarios
26
- # using a condition variable
24
+ # Like functools.lru_cache, this has to be thread-safe. Using
25
+ # a condition variable also prevents cache stampedes. Passing
26
+ # its lock separately skips the Python-level __enter__() and
27
+ # __exit__() methods of Condition on every call.
27
28
  key = keys.typedkey if typed else keys.hashkey
28
- wrapper = cached(cache=cache, key=key, condition=Condition(), info=True)(func)
29
+ lock = RLock()
30
+ wrapper = cached(
31
+ cache=cache, key=key, lock=lock, condition=Condition(lock), info=True
32
+ )(func)
29
33
  wrapper.cache_parameters = lambda: {"maxsize": maxsize, "typed": typed} # type: ignore
30
34
  return wrapper
31
35
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cachetools
3
- Version: 7.3.3
3
+ Version: 7.3.5
4
4
  Summary: Extensible memoizing collections and decorators
5
5
  Author-email: Thomas Kemmer <tkemmer@computer.org>
6
6
  Maintainer-email: Thomas Kemmer <tkemmer@computer.org>