cachetools 7.3.2__tar.gz → 7.3.4__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.2 → cachetools-7.3.4}/CHANGELOG.rst +20 -0
- {cachetools-7.3.2/src/cachetools.egg-info → cachetools-7.3.4}/PKG-INFO +1 -1
- {cachetools-7.3.2 → cachetools-7.3.4}/docs/index.rst +10 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/src/cachetools/__init__.py +26 -18
- {cachetools-7.3.2 → cachetools-7.3.4}/src/cachetools/func.py +9 -5
- {cachetools-7.3.2 → cachetools-7.3.4/src/cachetools.egg-info}/PKG-INFO +1 -1
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/__init__.py +12 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/test_tlru.py +43 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/test_ttl.py +39 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/LICENSE +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/MANIFEST.in +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/README.rst +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/docs/conf.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/pyproject.toml +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/setup.cfg +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/src/cachetools/__init__.pyi +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/src/cachetools/_cached.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/src/cachetools/_cachedmethod.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/src/cachetools/_descriptor.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/src/cachetools/func.pyi +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/src/cachetools/keys.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/src/cachetools/keys.pyi +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/src/cachetools/py.typed +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/src/cachetools.egg-info/SOURCES.txt +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/src/cachetools.egg-info/dependency_links.txt +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/src/cachetools.egg-info/top_level.txt +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/test_cache.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/test_cached.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/test_cachedmethod.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/test_classmethod.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/test_fifo.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/test_func.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/test_keys.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/test_lfu.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/test_lru.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/test_rr.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tests/test_threading.py +0 -0
- {cachetools-7.3.2 → cachetools-7.3.4}/tox.ini +0 -0
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
v7.3.4 (2026-11-25)
|
|
2
|
+
===================
|
|
3
|
+
|
|
4
|
+
- Minor ``cachetools.func`` performance improvements.
|
|
5
|
+
|
|
6
|
+
- Minor documentation improvements.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
v7.3.3 (2026-11-18)
|
|
10
|
+
===================
|
|
11
|
+
|
|
12
|
+
- Fix deep-copying ``TTLCache`` and ``TLRUCache`` instances whose
|
|
13
|
+
timer defines ``__deepcopy__``.
|
|
14
|
+
|
|
15
|
+
- Fix updating ``TTLCache`` and ``TLRUCache`` items while iterating,
|
|
16
|
+
which skipped or repeated keys.
|
|
17
|
+
|
|
18
|
+
- Minor ``LRUCache`` performance improvements.
|
|
19
|
+
|
|
20
|
+
|
|
1
21
|
v7.3.2 (2026-11-14)
|
|
2
22
|
===================
|
|
3
23
|
|
|
@@ -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
|
|
|
@@ -372,6 +378,10 @@ often called with the same arguments:
|
|
|
372
378
|
condition variable must be created with the corresponding lock,
|
|
373
379
|
e.g. ``condition=threading.Condition(lock)``; an unrelated
|
|
374
380
|
:class:`threading.Condition` will raise :exc:`RuntimeError`.
|
|
381
|
+
On current CPython versions, passing the lock separately this
|
|
382
|
+
way is also slightly faster than providing `condition` alone,
|
|
383
|
+
since acquiring the lock through a :class:`threading.Condition`
|
|
384
|
+
adds Python-level method calls on every cache access.
|
|
375
385
|
|
|
376
386
|
The decorator's `cache`, `key`, `lock` and `condition` parameters
|
|
377
387
|
are also available as :attr:`cache`, :attr:`cache_key`,
|
|
@@ -12,7 +12,7 @@ __all__ = (
|
|
|
12
12
|
"cachedmethod",
|
|
13
13
|
)
|
|
14
14
|
|
|
15
|
-
__version__ = "7.3.
|
|
15
|
+
__version__ = "7.3.4"
|
|
16
16
|
|
|
17
17
|
import collections
|
|
18
18
|
import collections.abc
|
|
@@ -351,7 +351,11 @@ class LRUCache(Cache):
|
|
|
351
351
|
|
|
352
352
|
def __setitem__(self, key, value, cache_setitem=Cache.__setitem__):
|
|
353
353
|
cache_setitem(self, key, value)
|
|
354
|
-
self.
|
|
354
|
+
order = self.__order
|
|
355
|
+
if key in order:
|
|
356
|
+
order.move_to_end(key)
|
|
357
|
+
else:
|
|
358
|
+
order[key] = None
|
|
355
359
|
|
|
356
360
|
def __delitem__(self, key, cache_delitem=Cache.__delitem__):
|
|
357
361
|
cache_delitem(self, key)
|
|
@@ -377,13 +381,6 @@ class LRUCache(Cache):
|
|
|
377
381
|
Cache.clear(self)
|
|
378
382
|
self.__order.clear()
|
|
379
383
|
|
|
380
|
-
def __touch(self, key):
|
|
381
|
-
"""Mark as recently used."""
|
|
382
|
-
try:
|
|
383
|
-
self.__order.move_to_end(key)
|
|
384
|
-
except KeyError:
|
|
385
|
-
self.__order[key] = None
|
|
386
|
-
|
|
387
384
|
|
|
388
385
|
class RRCache(Cache):
|
|
389
386
|
"""Random Replacement (RR) cache implementation."""
|
|
@@ -464,6 +461,12 @@ class _TimedCache(Cache):
|
|
|
464
461
|
def __copy__(self):
|
|
465
462
|
return _TimedCache._Timer(self.__timer)
|
|
466
463
|
|
|
464
|
+
def __deepcopy__(self, memo):
|
|
465
|
+
# otherwise __getattr__ forwards to the timer's own __deepcopy__
|
|
466
|
+
import copy
|
|
467
|
+
|
|
468
|
+
return _TimedCache._Timer(copy.deepcopy(self.__timer, memo))
|
|
469
|
+
|
|
467
470
|
def __reduce__(self):
|
|
468
471
|
return _TimedCache._Timer, (self.__timer,)
|
|
469
472
|
|
|
@@ -596,14 +599,15 @@ class TTLCache(_TimedCache):
|
|
|
596
599
|
raise KeyError(key)
|
|
597
600
|
|
|
598
601
|
def __iter__(self):
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
+
# Iterate over a snapshot of keys, in insertion order: Note that
|
|
603
|
+
# changes/updates while iterating do not raise RuntimeError, since
|
|
604
|
+
# items may also expire, so this seems to be the easier way.
|
|
605
|
+
for key in list(Cache.__iter__(self)):
|
|
602
606
|
# "freeze" time for iterator access
|
|
603
607
|
with self.timer as time:
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
608
|
+
link = self.__links.get(key)
|
|
609
|
+
if link is not None and time < link.expires:
|
|
610
|
+
yield key
|
|
607
611
|
|
|
608
612
|
def __copy__(self):
|
|
609
613
|
copy = super().__copy__()
|
|
@@ -752,11 +756,15 @@ class TLRUCache(_TimedCache):
|
|
|
752
756
|
raise KeyError(key)
|
|
753
757
|
|
|
754
758
|
def __iter__(self):
|
|
755
|
-
|
|
759
|
+
# Iterate over a snapshot of keys, in insertion order: Note that
|
|
760
|
+
# changes/updates while iterating do not raise RuntimeError, since
|
|
761
|
+
# items may also expire, so this seems to be the easier way.
|
|
762
|
+
for key in list(Cache.__iter__(self)):
|
|
756
763
|
# "freeze" time for iterator access
|
|
757
764
|
with self.timer as time:
|
|
758
|
-
|
|
759
|
-
|
|
765
|
+
item = self.__items.get(key)
|
|
766
|
+
if item is not None and time < item.expires:
|
|
767
|
+
yield key
|
|
760
768
|
|
|
761
769
|
def __copy__(self):
|
|
762
770
|
copy = super().__copy__()
|
|
@@ -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
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
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
|
-
|
|
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
|
|
|
@@ -457,6 +457,18 @@ class CacheTestMixin(SimplifiedTestCaseProtocol):
|
|
|
457
457
|
self.assertEqual(1, len(cache))
|
|
458
458
|
self.assertEqual(4, cache.currsize)
|
|
459
459
|
|
|
460
|
+
def test_iter_update(self):
|
|
461
|
+
cache = self.Cache(maxsize=3)
|
|
462
|
+
cache.update({1: 1, 2: 2, 3: 3})
|
|
463
|
+
|
|
464
|
+
# updating items while iterating visits each key once, in order
|
|
465
|
+
keys = []
|
|
466
|
+
for key in cache:
|
|
467
|
+
keys.append(key)
|
|
468
|
+
cache[key] = key * 10
|
|
469
|
+
self.assertEqual([1, 2, 3], keys)
|
|
470
|
+
self.assertEqual([10, 20, 30], list(cache.values()))
|
|
471
|
+
|
|
460
472
|
def test_pickle(self):
|
|
461
473
|
import pickle
|
|
462
474
|
|
|
@@ -447,3 +447,46 @@ class TLRUCacheTest(unittest.TestCase, CacheTestMixin):
|
|
|
447
447
|
source.timer.tick()
|
|
448
448
|
self.assertEqual([], list(source))
|
|
449
449
|
self.assertEqual([4], list(cache))
|
|
450
|
+
|
|
451
|
+
def test_tlru_deepcopy_timer(self):
|
|
452
|
+
import copy
|
|
453
|
+
|
|
454
|
+
class DeepCopyTimer(Timer):
|
|
455
|
+
def __deepcopy__(self, memo):
|
|
456
|
+
return copy.copy(self)
|
|
457
|
+
|
|
458
|
+
source = TLRUCache[int, int, int](
|
|
459
|
+
maxsize=2, ttu=lambda _, v, t: t + v, timer=DeepCopyTimer()
|
|
460
|
+
)
|
|
461
|
+
source[1] = 1
|
|
462
|
+
|
|
463
|
+
# a timer may define __deepcopy__ itself
|
|
464
|
+
cache = copy.deepcopy(source)
|
|
465
|
+
self.assertEqual([1], list(cache))
|
|
466
|
+
|
|
467
|
+
# the copy has a timer of its own
|
|
468
|
+
cache.timer.tick()
|
|
469
|
+
self.assertEqual([], list(cache))
|
|
470
|
+
self.assertEqual([1], list(source))
|
|
471
|
+
|
|
472
|
+
def test_tlru_iter_modify(self):
|
|
473
|
+
cache = TLRUCache[int, int, int](
|
|
474
|
+
maxsize=6, ttu=lambda _, v, t: t + 2, timer=Timer()
|
|
475
|
+
)
|
|
476
|
+
cache.update({1: 1, 2: 2, 3: 3})
|
|
477
|
+
|
|
478
|
+
# items inserted while iterating are not visited
|
|
479
|
+
keys = []
|
|
480
|
+
for key in cache:
|
|
481
|
+
keys.append(key)
|
|
482
|
+
cache[key + 10] = key
|
|
483
|
+
self.assertEqual([1, 2, 3], keys)
|
|
484
|
+
self.assertEqual([1, 2, 3, 11, 12, 13], list(cache))
|
|
485
|
+
|
|
486
|
+
# items removed while iterating are skipped
|
|
487
|
+
keys = []
|
|
488
|
+
for key in cache:
|
|
489
|
+
keys.append(key)
|
|
490
|
+
cache.clear()
|
|
491
|
+
self.assertEqual([1], keys)
|
|
492
|
+
self.assertEqual([], list(cache))
|
|
@@ -317,3 +317,42 @@ class TTLCacheTest(unittest.TestCase, CacheTestMixin):
|
|
|
317
317
|
# items expire in the copy as in its source
|
|
318
318
|
self.assertEqual([3, 4], list(cache))
|
|
319
319
|
self.assertEqual([3], list(source))
|
|
320
|
+
|
|
321
|
+
def test_ttl_deepcopy_timer(self):
|
|
322
|
+
import copy
|
|
323
|
+
|
|
324
|
+
class DeepCopyTimer(Timer):
|
|
325
|
+
def __deepcopy__(self, memo):
|
|
326
|
+
return copy.copy(self)
|
|
327
|
+
|
|
328
|
+
source = TTLCache[int, int, int](maxsize=2, ttl=1, timer=DeepCopyTimer())
|
|
329
|
+
source[1] = 1
|
|
330
|
+
|
|
331
|
+
# a timer may define __deepcopy__ itself
|
|
332
|
+
cache = copy.deepcopy(source)
|
|
333
|
+
self.assertEqual([1], list(cache))
|
|
334
|
+
|
|
335
|
+
# the copy has a timer of its own
|
|
336
|
+
cache.timer.tick()
|
|
337
|
+
self.assertEqual([], list(cache))
|
|
338
|
+
self.assertEqual([1], list(source))
|
|
339
|
+
|
|
340
|
+
def test_ttl_iter_modify(self):
|
|
341
|
+
cache = TTLCache[int, int, int](maxsize=6, ttl=2, timer=Timer())
|
|
342
|
+
cache.update({1: 1, 2: 2, 3: 3})
|
|
343
|
+
|
|
344
|
+
# items inserted while iterating are not visited
|
|
345
|
+
keys = []
|
|
346
|
+
for key in cache:
|
|
347
|
+
keys.append(key)
|
|
348
|
+
cache[key + 10] = key
|
|
349
|
+
self.assertEqual([1, 2, 3], keys)
|
|
350
|
+
self.assertEqual([1, 2, 3, 11, 12, 13], list(cache))
|
|
351
|
+
|
|
352
|
+
# items removed while iterating are skipped
|
|
353
|
+
keys = []
|
|
354
|
+
for key in cache:
|
|
355
|
+
keys.append(key)
|
|
356
|
+
cache.clear()
|
|
357
|
+
self.assertEqual([1], keys)
|
|
358
|
+
self.assertEqual([], list(cache))
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|