cachetools 8.0.2__tar.gz → 8.0.3__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 (38) hide show
  1. {cachetools-8.0.2 → cachetools-8.0.3}/CHANGELOG.rst +14 -0
  2. {cachetools-8.0.2/src/cachetools.egg-info → cachetools-8.0.3}/PKG-INFO +1 -1
  3. {cachetools-8.0.2 → cachetools-8.0.3}/docs/index.rst +17 -8
  4. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools/__init__.py +1 -1
  5. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools/_decorator.py +40 -15
  6. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools/_descriptor.py +4 -5
  7. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools/func.py +8 -3
  8. {cachetools-8.0.2 → cachetools-8.0.3/src/cachetools.egg-info}/PKG-INFO +1 -1
  9. {cachetools-8.0.2 → cachetools-8.0.3}/tests/test_cachedmethod.py +7 -0
  10. {cachetools-8.0.2 → cachetools-8.0.3}/tests/test_threading.py +98 -0
  11. {cachetools-8.0.2 → cachetools-8.0.3}/LICENSE +0 -0
  12. {cachetools-8.0.2 → cachetools-8.0.3}/MANIFEST.in +0 -0
  13. {cachetools-8.0.2 → cachetools-8.0.3}/README.rst +0 -0
  14. {cachetools-8.0.2 → cachetools-8.0.3}/docs/conf.py +0 -0
  15. {cachetools-8.0.2 → cachetools-8.0.3}/pyproject.toml +0 -0
  16. {cachetools-8.0.2 → cachetools-8.0.3}/setup.cfg +0 -0
  17. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools/__init__.pyi +0 -0
  18. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools/_cached.py +0 -0
  19. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools/_cachedmethod.py +0 -0
  20. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools/func.pyi +0 -0
  21. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools/keys.py +0 -0
  22. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools/keys.pyi +0 -0
  23. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools/py.typed +0 -0
  24. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools.egg-info/SOURCES.txt +0 -0
  25. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools.egg-info/dependency_links.txt +0 -0
  26. {cachetools-8.0.2 → cachetools-8.0.3}/src/cachetools.egg-info/top_level.txt +0 -0
  27. {cachetools-8.0.2 → cachetools-8.0.3}/tests/__init__.py +0 -0
  28. {cachetools-8.0.2 → cachetools-8.0.3}/tests/test_cache.py +0 -0
  29. {cachetools-8.0.2 → cachetools-8.0.3}/tests/test_cached.py +0 -0
  30. {cachetools-8.0.2 → cachetools-8.0.3}/tests/test_fifo.py +0 -0
  31. {cachetools-8.0.2 → cachetools-8.0.3}/tests/test_func.py +0 -0
  32. {cachetools-8.0.2 → cachetools-8.0.3}/tests/test_keys.py +0 -0
  33. {cachetools-8.0.2 → cachetools-8.0.3}/tests/test_lfu.py +0 -0
  34. {cachetools-8.0.2 → cachetools-8.0.3}/tests/test_lru.py +0 -0
  35. {cachetools-8.0.2 → cachetools-8.0.3}/tests/test_rr.py +0 -0
  36. {cachetools-8.0.2 → cachetools-8.0.3}/tests/test_tlru.py +0 -0
  37. {cachetools-8.0.2 → cachetools-8.0.3}/tests/test_ttl.py +0 -0
  38. {cachetools-8.0.2 → cachetools-8.0.3}/tox.ini +0 -0
@@ -1,3 +1,17 @@
1
+ v8.0.3 (2026-11-25)
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.
7
+
8
+ - Minor ``cachetools.func`` performance improvements.
9
+
10
+ - Minor ``@cachedmethod`` error handling improvements.
11
+
12
+ - Minor documentation improvements.
13
+
14
+
1
15
  v8.0.2 (2026-11-18)
2
16
  ===================
3
17
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cachetools
3
- Version: 8.0.2
3
+ Version: 8.0.3
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>
@@ -120,6 +120,12 @@ inserted into the cache.
120
120
  This class discards the least recently used items first to make
121
121
  space when necessary.
122
122
 
123
+ Retrieving an item via :meth:`LRUCache.__getitem__` marks it as
124
+ most recently used, as does inserting or updating it. Since
125
+ :meth:`items` and :meth:`values` retrieve every item in turn,
126
+ iterating over these views leaves the cache discarding items in
127
+ insertion order.
128
+
123
129
  .. autoclass:: RRCache(maxsize, choice=random.choice, getsizeof=None)
124
130
  :members: choice, popitem
125
131
 
@@ -352,14 +358,13 @@ often called with the same arguments:
352
358
  If `condition` is not :const:`None`, it must specify a `condition
353
359
  variable`_, i.e. an object providing :meth:`wait()`,
354
360
  :meth:`wait_for()`, :meth:`notify()` and :meth:`notify_all()`
355
- methods as defined by :class:`threading.Condition`. Using a
356
- `condition` variable will prevent concurrent execution of the
357
- wrapped function with *identical* parameters, or cache keys.
358
- Instead, a calling thread will check if an identical function call
359
- is already executing, and will then wait for the pending call to
360
- finish. The executing thread will notify any waiting threads as
361
- soon as the function completes, which will then return the cached
362
- function result.
361
+ methods as defined by :class:`threading.Condition`. When one
362
+ thread is computing a result for a cache key, other threads
363
+ calling the same wrapper with that key wait, then check the cache
364
+ again. If the computing thread calls the wrapper recursively with
365
+ the same key, the recursive call runs uncached and counts as a
366
+ miss when `info=True`. Only the outermost call can store a result
367
+ for that key.
363
368
 
364
369
  .. note::
365
370
 
@@ -382,6 +387,10 @@ often called with the same arguments:
382
387
  condition variable must be created with the corresponding lock,
383
388
  e.g. ``condition=threading.Condition(lock)``; an unrelated
384
389
  :class:`threading.Condition` will raise :exc:`RuntimeError`.
390
+ On current CPython versions, passing the lock separately this
391
+ way is also slightly faster than providing `condition` alone,
392
+ since acquiring the lock through a :class:`threading.Condition`
393
+ adds Python-level method calls on every cache access.
385
394
 
386
395
  The decorator's `cache`, `key`, `lock` and `condition` parameters
387
396
  are also available as :attr:`cache`, :attr:`cache_key`,
@@ -13,7 +13,7 @@ __all__ = (
13
13
  "cachedmethod",
14
14
  )
15
15
 
16
- __version__ = "8.0.2"
16
+ __version__ = "8.0.3"
17
17
 
18
18
  import collections
19
19
  import collections.abc
@@ -65,8 +65,11 @@ class _LockedMixin:
65
65
 
66
66
  class _ConditionMixin:
67
67
  def __init__(self, func):
68
+ from threading import get_ident
69
+
68
70
  self.__func = func
69
- self.__pending = set()
71
+ self.__get_ident = get_ident
72
+ self.__pending = {} # maps keys to the calling thread
70
73
 
71
74
  def __call__(self, /, *args, **kwargs):
72
75
  cache = self.cache
@@ -76,11 +79,20 @@ class _ConditionMixin:
76
79
 
77
80
  with lock:
78
81
  while key in self.__pending:
82
+ if self.__pending[key] == self.__get_ident():
83
+ # A recursive call would wait for its own pending
84
+ # call forever, so it calls the function again.
85
+ recursive = True
86
+ break
79
87
  cond.wait()
80
- try:
81
- return cache[key]
82
- except KeyError:
83
- self.__pending.add(key)
88
+ else:
89
+ try:
90
+ return cache[key]
91
+ except KeyError:
92
+ self.__pending[key] = self.__get_ident()
93
+ recursive = False
94
+ if recursive:
95
+ return self.__func(*args, **kwargs)
84
96
  try:
85
97
  val = self.__func(*args, **kwargs)
86
98
  with lock:
@@ -91,7 +103,7 @@ class _ConditionMixin:
91
103
  return val
92
104
  finally:
93
105
  with lock:
94
- self.__pending.remove(key)
106
+ del self.__pending[key]
95
107
  cond.notify_all()
96
108
 
97
109
  def cache_clear(self):
@@ -170,10 +182,13 @@ class _LockedInfoMixin:
170
182
 
171
183
  class _ConditionInfoMixin:
172
184
  def __init__(self, func, info):
185
+ from threading import get_ident
186
+
173
187
  self.__func = func
174
188
  self.__info = info
175
189
  self.__hits = self.__misses = 0
176
- self.__pending = set()
190
+ self.__get_ident = get_ident
191
+ self.__pending = {} # maps keys to the calling thread
177
192
 
178
193
  def __call__(self, /, *args, **kwargs):
179
194
  cache = self.cache
@@ -183,14 +198,24 @@ class _ConditionInfoMixin:
183
198
 
184
199
  with lock:
185
200
  while key in self.__pending:
201
+ if self.__pending[key] == self.__get_ident():
202
+ # A recursive call would wait for its own pending
203
+ # call forever, so it calls the function again.
204
+ self.__misses += 1
205
+ recursive = True
206
+ break
186
207
  cond.wait()
187
- try:
188
- result = cache[key]
189
- self.__hits += 1
190
- return result
191
- except KeyError:
192
- self.__pending.add(key)
193
- self.__misses += 1
208
+ else:
209
+ try:
210
+ result = cache[key]
211
+ self.__hits += 1
212
+ return result
213
+ except KeyError:
214
+ self.__pending[key] = self.__get_ident()
215
+ self.__misses += 1
216
+ recursive = False
217
+ if recursive:
218
+ return self.__func(*args, **kwargs)
194
219
  try:
195
220
  val = self.__func(*args, **kwargs)
196
221
  with lock:
@@ -201,7 +226,7 @@ class _ConditionInfoMixin:
201
226
  return val
202
227
  finally:
203
228
  with lock:
204
- self.__pending.remove(key)
229
+ del self.__pending[key]
205
230
  cond.notify_all()
206
231
 
207
232
  def cache_clear(self):
@@ -144,6 +144,9 @@ class _Unbound:
144
144
  obj, *args = args
145
145
  else:
146
146
  obj = self.__pop_receiver(kwargs)
147
+ if obj is None:
148
+ msg = "Cannot call @cachedmethod through the class without an instance"
149
+ raise TypeError(msg)
147
150
  return self.__descriptor.__get__(obj)(*args, **kwargs)
148
151
 
149
152
  def __pop_receiver(self, kwargs):
@@ -159,8 +162,4 @@ class _Unbound:
159
162
  else:
160
163
  if param.kind is param.POSITIONAL_OR_KEYWORD:
161
164
  self.__receiver = param.name
162
- try:
163
- return kwargs.pop(self.__receiver)
164
- except KeyError:
165
- msg = "Cannot call @cachedmethod through the class without an instance"
166
- raise TypeError(msg) from None
165
+ return kwargs.pop(self.__receiver, None)
@@ -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
 
@@ -22,9 +22,14 @@ class _UnboundTTLCache(TTLCache):
22
22
  def _cache(cache, maxsize, typed):
23
23
  def decorator(func):
24
24
  # Like functools.lru_cache, this has to be thread-safe. Using
25
- # a condition variable also prevents cache stampedes.
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.
26
28
  key = keys.typedkey if typed else keys.hashkey
27
- 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)
28
33
  wrapper.cache_parameters = lambda: {"maxsize": maxsize, "typed": typed} # type: ignore
29
34
  return wrapper
30
35
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cachetools
3
- Version: 8.0.2
3
+ Version: 8.0.3
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>
@@ -1056,8 +1056,15 @@ class UnboundMethodTest(unittest.TestCase):
1056
1056
  self.assertEqual(cached.count, 1)
1057
1057
 
1058
1058
  def test_class_call_without_instance(self):
1059
+ cached = Cached(Cache(2))
1059
1060
  with self.assertRaises(TypeError):
1060
1061
  Cached.get() # type: ignore
1062
+ with self.assertRaises(TypeError):
1063
+ Cached.get(None, cached, 1) # type: ignore
1064
+ with self.assertRaises(TypeError):
1065
+ Cached.get(self=None, value=1) # type: ignore
1066
+ self.assertEqual(cached.count, 0)
1067
+ self.assertFalse(cached.cache)
1061
1068
 
1062
1069
  def test_class_call_keyword_receiver(self):
1063
1070
  cached = Cached(Cache(2))
@@ -177,6 +177,104 @@ class ThreadingTest(unittest.TestCase):
177
177
  with self.assertRaises(RuntimeError):
178
178
  Cached().method()
179
179
 
180
+ def test_cached_cond_recursion(self):
181
+ for info in (False, True):
182
+ with self.subTest(info=info):
183
+ calls = []
184
+
185
+ @cached(LRUCache(1), condition=Condition(), info=info)
186
+ def func(calls=calls):
187
+ calls.append(None)
188
+ return func() + 1 if len(calls) < 3 else 0
189
+
190
+ self.run_cond_recursion_test(func, calls, info)
191
+
192
+ def test_cachedmethod_cond_recursion(self):
193
+ for info in (False, True):
194
+ with self.subTest(info=info):
195
+ cache = LRUCache(1)
196
+ cond = Condition()
197
+ calls = []
198
+
199
+ class Cached:
200
+ @cachedmethod(lambda _: cache, condition=lambda _: cond, info=info)
201
+ def method(self, calls=calls):
202
+ calls.append(None)
203
+ return self.method() + 1 if len(calls) < 3 else 0
204
+
205
+ self.run_cond_recursion_test(Cached().method, calls, info)
206
+
207
+ def run_cond_recursion_test(self, func, calls, info):
208
+ results = []
209
+ # a daemon thread, so a recursive call waiting forever for its
210
+ # own pending call fails the test instead of blocking the exit
211
+ thread = Thread(target=lambda: results.append(func()), daemon=True)
212
+ thread.start()
213
+ thread.join(timeout=self.TIMEOUT)
214
+ self.assertFalse(thread.is_alive())
215
+ self.assertEqual(results, [2])
216
+
217
+ # only the outermost call's result is cached
218
+ self.assertEqual(func(), 2)
219
+ self.assertEqual(len(calls), 3)
220
+ if info:
221
+ cache_info = func.cache_info()
222
+ self.assertEqual((cache_info.hits, cache_info.misses), (1, 3))
223
+
224
+ def test_cached_cond_recursive_error(self):
225
+ for info in (False, True):
226
+ with self.subTest(info=info):
227
+ cond = WaitCondition()
228
+
229
+ @cached(LRUCache(1), key=lambda call: (), condition=cond, info=info)
230
+ def func(call):
231
+ return call()
232
+
233
+ self.run_cond_recursive_error_test(func, cond)
234
+
235
+ def test_cachedmethod_cond_recursive_error(self):
236
+ for info in (False, True):
237
+ with self.subTest(info=info):
238
+ cache = LRUCache(1)
239
+ cond = WaitCondition()
240
+
241
+ class Cached:
242
+ @cachedmethod(
243
+ lambda _: cache,
244
+ key=lambda self, call: (),
245
+ condition=lambda _: cond,
246
+ info=info,
247
+ )
248
+ def method(self, call):
249
+ return call()
250
+
251
+ self.run_cond_recursive_error_test(Cached().method, cond)
252
+
253
+ def run_cond_recursive_error_test(self, func, cond):
254
+ error = ValueError("recursive call failed")
255
+ results = {}
256
+
257
+ def inner():
258
+ waiter.start()
259
+ if not cond.waiting.wait(self.TIMEOUT):
260
+ raise TimeoutError("second caller did not wait")
261
+ raise error
262
+
263
+ def run(name, call):
264
+ try:
265
+ results[name] = func(call)
266
+ except ValueError as exc:
267
+ results[name] = exc
268
+
269
+ owner = Thread(target=run, args=("owner", lambda: func(inner)), daemon=True)
270
+ waiter = Thread(target=run, args=("waiter", lambda: 42), daemon=True)
271
+ owner.start()
272
+ for thread in (owner, waiter):
273
+ thread.join(timeout=self.TIMEOUT)
274
+ self.assertFalse(thread.is_alive())
275
+ self.assertEqual(results, {"owner": error, "waiter": 42})
276
+ self.assertEqual(func.cache[()], 42)
277
+
180
278
  def run_cond_wait_test(self, func, cond, started, release, calls):
181
279
  results = [None] * 2
182
280
 
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes