cachetools 7.3.2__tar.gz → 8.0.1__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-8.0.1}/CHANGELOG.rst +53 -0
- {cachetools-7.3.2/src/cachetools.egg-info → cachetools-8.0.1}/PKG-INFO +12 -9
- {cachetools-7.3.2 → cachetools-8.0.1}/README.rst +10 -6
- {cachetools-7.3.2 → cachetools-8.0.1}/docs/index.rst +66 -29
- {cachetools-7.3.2 → cachetools-8.0.1}/pyproject.toml +1 -2
- {cachetools-7.3.2 → cachetools-8.0.1}/src/cachetools/__init__.py +28 -46
- {cachetools-7.3.2 → cachetools-8.0.1}/src/cachetools/__init__.pyi +20 -23
- cachetools-8.0.1/src/cachetools/_cached.py +110 -0
- cachetools-8.0.1/src/cachetools/_cachedmethod.py +139 -0
- cachetools-8.0.1/src/cachetools/_decorator.py +216 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/src/cachetools/_descriptor.py +57 -79
- {cachetools-7.3.2 → cachetools-8.0.1}/src/cachetools/func.pyi +2 -2
- cachetools-8.0.1/src/cachetools/keys.py +61 -0
- {cachetools-7.3.2 → cachetools-8.0.1/src/cachetools.egg-info}/PKG-INFO +12 -9
- {cachetools-7.3.2 → cachetools-8.0.1}/src/cachetools.egg-info/SOURCES.txt +1 -1
- {cachetools-7.3.2 → cachetools-8.0.1}/tests/test_cached.py +160 -31
- {cachetools-7.3.2 → cachetools-8.0.1}/tests/test_cachedmethod.py +154 -64
- {cachetools-7.3.2 → cachetools-8.0.1}/tests/test_keys.py +0 -2
- cachetools-7.3.2/src/cachetools/_cached.py +0 -284
- cachetools-7.3.2/src/cachetools/_cachedmethod.py +0 -344
- cachetools-7.3.2/src/cachetools/keys.py +0 -77
- cachetools-7.3.2/tests/test_classmethod.py +0 -212
- {cachetools-7.3.2 → cachetools-8.0.1}/LICENSE +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/MANIFEST.in +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/docs/conf.py +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/setup.cfg +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/src/cachetools/func.py +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/src/cachetools/keys.pyi +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/src/cachetools/py.typed +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/src/cachetools.egg-info/dependency_links.txt +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/src/cachetools.egg-info/top_level.txt +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/tests/__init__.py +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/tests/test_cache.py +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/tests/test_fifo.py +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/tests/test_func.py +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/tests/test_lfu.py +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/tests/test_lru.py +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/tests/test_rr.py +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/tests/test_threading.py +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/tests/test_tlru.py +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/tests/test_ttl.py +0 -0
- {cachetools-7.3.2 → cachetools-8.0.1}/tox.ini +0 -0
|
@@ -1,3 +1,56 @@
|
|
|
1
|
+
v8.0.1 (2026-11-17)
|
|
2
|
+
===================
|
|
3
|
+
|
|
4
|
+
- Fix copying and deep-copying ``@cached`` wrappers of callables
|
|
5
|
+
without a ``__qualname__``, e.g. ``functools.partial`` objects
|
|
6
|
+
(broken since v8.0.0).
|
|
7
|
+
|
|
8
|
+
- Fix deep-copying and pickling ``LFUCache`` instances with many
|
|
9
|
+
different use counts (broken since v6.1.0).
|
|
10
|
+
|
|
11
|
+
- Properly support shallow copies of caches, including subclasses
|
|
12
|
+
with additional attributes.
|
|
13
|
+
|
|
14
|
+
- Minor ``LRUCache`` performance improvements.
|
|
15
|
+
|
|
16
|
+
- Minor ``@cachedmethod`` error handling improvements.
|
|
17
|
+
|
|
18
|
+
- Minor documentation improvements.
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
v8.0.0 (2026-11-06)
|
|
22
|
+
===================
|
|
23
|
+
|
|
24
|
+
- Drop Python 3.10 support (breaking change).
|
|
25
|
+
|
|
26
|
+
- Drop support for deprecated ``@cachedmethod`` features (breaking
|
|
27
|
+
change).
|
|
28
|
+
|
|
29
|
+
- Drop support for ``@cached(cache=None)`` (breaking change).
|
|
30
|
+
|
|
31
|
+
- Require custom cache classes to support ``__len__`` when used with
|
|
32
|
+
``info=True`` (breaking change).
|
|
33
|
+
|
|
34
|
+
- Refactor ``_CacheInfo`` and make it public as ``CacheInfo``
|
|
35
|
+
(potentially breaking change).
|
|
36
|
+
|
|
37
|
+
- Return callable objects instead of functions from ``@cached`` and
|
|
38
|
+
the ``cachetools.func`` decorators (potentially breaking change).
|
|
39
|
+
|
|
40
|
+
- Do not set ``cache_info`` to ``None`` on ``@cached`` wrappers if
|
|
41
|
+
``info`` is not set (potentially breaking change).
|
|
42
|
+
|
|
43
|
+
- Raise ``TypeError`` for ``@cachedmethod`` descriptors assigned
|
|
44
|
+
after class creation, i.e. without ``__set_name__`` being called
|
|
45
|
+
(potentially breaking change).
|
|
46
|
+
|
|
47
|
+
- Various performance improvements.
|
|
48
|
+
|
|
49
|
+
- Various documentation improvements.
|
|
50
|
+
|
|
51
|
+
- Various test improvements.
|
|
52
|
+
|
|
53
|
+
|
|
1
54
|
v7.3.2 (2026-11-14)
|
|
2
55
|
===================
|
|
3
56
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cachetools
|
|
3
|
-
Version:
|
|
3
|
+
Version: 8.0.1
|
|
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>
|
|
@@ -16,14 +16,13 @@ Classifier: Intended Audience :: Developers
|
|
|
16
16
|
Classifier: Operating System :: OS Independent
|
|
17
17
|
Classifier: Programming Language :: Python
|
|
18
18
|
Classifier: Programming Language :: Python :: 3
|
|
19
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
20
19
|
Classifier: Programming Language :: Python :: 3.11
|
|
21
20
|
Classifier: Programming Language :: Python :: 3.12
|
|
22
21
|
Classifier: Programming Language :: Python :: 3.13
|
|
23
22
|
Classifier: Programming Language :: Python :: 3.14
|
|
24
23
|
Classifier: Programming Language :: Python :: 3.15
|
|
25
24
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
26
|
-
Requires-Python: >=3.
|
|
25
|
+
Requires-Python: >=3.11
|
|
27
26
|
Description-Content-Type: text/x-rst
|
|
28
27
|
License-File: LICENSE
|
|
29
28
|
Dynamic: license-file
|
|
@@ -67,14 +66,18 @@ function decorator.
|
|
|
67
66
|
# cache least recently used Python Enhancement Proposals
|
|
68
67
|
@cached(cache=LRUCache(maxsize=32))
|
|
69
68
|
def get_pep(num):
|
|
70
|
-
|
|
71
|
-
|
|
69
|
+
from urllib.request import urlopen
|
|
70
|
+
url = 'https://peps.python.org/pep-%04d/' % num
|
|
71
|
+
with urlopen(url) as s:
|
|
72
72
|
return s.read()
|
|
73
73
|
|
|
74
|
-
# cache weather data for no longer than ten minutes
|
|
74
|
+
# cache PyOWM weather data for no longer than ten minutes
|
|
75
|
+
from pyowm.owm import OWM
|
|
76
|
+
owm = OWM('your-free-api-key')
|
|
77
|
+
|
|
75
78
|
@cached(cache=TTLCache(maxsize=1024, ttl=600))
|
|
76
79
|
def get_weather(place):
|
|
77
|
-
return owm.weather_at_place(place).
|
|
80
|
+
return owm.weather_manager().weather_at_place(place).weather
|
|
78
81
|
|
|
79
82
|
For the purpose of this module, a *cache* is a mutable_ mapping_ of a
|
|
80
83
|
fixed maximum size. When the cache is full, i.e. by adding another
|
|
@@ -124,8 +127,8 @@ Licensed under the `MIT License`_.
|
|
|
124
127
|
|
|
125
128
|
|
|
126
129
|
.. _@lru_cache: https://docs.python.org/3/library/functools.html#functools.lru_cache
|
|
127
|
-
.. _mutable: https://docs.python.org/
|
|
128
|
-
.. _mapping: https://docs.python.org/
|
|
130
|
+
.. _mutable: https://docs.python.org/3/glossary.html#term-mutable
|
|
131
|
+
.. _mapping: https://docs.python.org/3/glossary.html#term-mapping
|
|
129
132
|
.. _cache algorithm: https://en.wikipedia.org/wiki/Cache_algorithms
|
|
130
133
|
|
|
131
134
|
.. _PyPI: https://pypi.org/project/cachetools/
|
|
@@ -37,14 +37,18 @@ function decorator.
|
|
|
37
37
|
# cache least recently used Python Enhancement Proposals
|
|
38
38
|
@cached(cache=LRUCache(maxsize=32))
|
|
39
39
|
def get_pep(num):
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
from urllib.request import urlopen
|
|
41
|
+
url = 'https://peps.python.org/pep-%04d/' % num
|
|
42
|
+
with urlopen(url) as s:
|
|
42
43
|
return s.read()
|
|
43
44
|
|
|
44
|
-
# cache weather data for no longer than ten minutes
|
|
45
|
+
# cache PyOWM weather data for no longer than ten minutes
|
|
46
|
+
from pyowm.owm import OWM
|
|
47
|
+
owm = OWM('your-free-api-key')
|
|
48
|
+
|
|
45
49
|
@cached(cache=TTLCache(maxsize=1024, ttl=600))
|
|
46
50
|
def get_weather(place):
|
|
47
|
-
return owm.weather_at_place(place).
|
|
51
|
+
return owm.weather_manager().weather_at_place(place).weather
|
|
48
52
|
|
|
49
53
|
For the purpose of this module, a *cache* is a mutable_ mapping_ of a
|
|
50
54
|
fixed maximum size. When the cache is full, i.e. by adding another
|
|
@@ -94,8 +98,8 @@ Licensed under the `MIT License`_.
|
|
|
94
98
|
|
|
95
99
|
|
|
96
100
|
.. _@lru_cache: https://docs.python.org/3/library/functools.html#functools.lru_cache
|
|
97
|
-
.. _mutable: https://docs.python.org/
|
|
98
|
-
.. _mapping: https://docs.python.org/
|
|
101
|
+
.. _mutable: https://docs.python.org/3/glossary.html#term-mutable
|
|
102
|
+
.. _mapping: https://docs.python.org/3/glossary.html#term-mapping
|
|
99
103
|
.. _cache algorithm: https://en.wikipedia.org/wiki/Cache_algorithms
|
|
100
104
|
|
|
101
105
|
.. _PyPI: https://pypi.org/project/cachetools/
|
|
@@ -44,15 +44,15 @@ calls :meth:`self.popitem()` repeatedly until there is enough room for
|
|
|
44
44
|
the item to be added.
|
|
45
45
|
|
|
46
46
|
In general, a cache's size is the total size of its items' values.
|
|
47
|
-
Therefore, :class:`Cache` provides a :meth:`~Cache.getsizeof` method,
|
|
48
|
-
returns the size of a given `value`. The default implementation
|
|
49
|
-
:meth:`~Cache.getsizeof` returns :const:`1` irrespective of its
|
|
50
|
-
making the cache's size equal to the number of its items, or
|
|
47
|
+
Therefore, :class:`Cache` provides a :meth:`~Cache.getsizeof` method,
|
|
48
|
+
which returns the size of a given `value`. The default implementation
|
|
49
|
+
of :meth:`~Cache.getsizeof` returns :const:`1` irrespective of its
|
|
50
|
+
argument, making the cache's size equal to the number of its items, or
|
|
51
51
|
`len(cache)`. For convenience, all cache classes accept an optional
|
|
52
52
|
named constructor parameter `getsizeof`, which may specify a callable
|
|
53
|
-
``getsizeof(value)`` returning a non-negative number as the size of
|
|
54
|
-
|
|
55
|
-
stored, and trying to do so raises :exc:`ValueError`.
|
|
53
|
+
``getsizeof(value)`` returning a non-negative number as the size of an
|
|
54
|
+
item's value. An item whose size exceeds :attr:`~Cache.maxsize`
|
|
55
|
+
cannot be stored, and trying to do so raises :exc:`ValueError`.
|
|
56
56
|
|
|
57
57
|
The values of a :class:`Cache` are mutable by default, as are e.g. the
|
|
58
58
|
values of a :class:`dict`. It is the user's responsibility to take
|
|
@@ -78,6 +78,16 @@ inserted into the cache.
|
|
|
78
78
|
additionally need to override :meth:`__getitem__`,
|
|
79
79
|
:meth:`__setitem__` and :meth:`__delitem__`.
|
|
80
80
|
|
|
81
|
+
.. note::
|
|
82
|
+
|
|
83
|
+
Even though :class:`Cache` and the subclasses provided by this
|
|
84
|
+
module can generally be `pickled`_, the byte stream format is an
|
|
85
|
+
implementation detail that *any* release may change, even a
|
|
86
|
+
bugfix release. Therefore, do not rely on being able to
|
|
87
|
+
unpickle a cache, e.g. one stored in a file or database, with a
|
|
88
|
+
different :mod:`cachetools` version than the one it was pickled
|
|
89
|
+
with.
|
|
90
|
+
|
|
81
91
|
.. autoclass:: FIFOCache(maxsize, getsizeof=None)
|
|
82
92
|
:members: popitem
|
|
83
93
|
|
|
@@ -404,9 +414,10 @@ often called with the same arguments:
|
|
|
404
414
|
get_pep.cache_clear()
|
|
405
415
|
|
|
406
416
|
If `info` is set to :const:`True`, the wrapped function is
|
|
407
|
-
instrumented with a :
|
|
408
|
-
|
|
409
|
-
|
|
417
|
+
instrumented with a :meth:`cache_info()` function that returns a
|
|
418
|
+
:class:`CacheInfo` instance, a named tuple with fields `hits`,
|
|
419
|
+
`misses`, `maxsize` and `currsize`, to help measure the
|
|
420
|
+
effectiveness of the cache.
|
|
410
421
|
|
|
411
422
|
.. note::
|
|
412
423
|
|
|
@@ -540,12 +551,10 @@ often called with the same arguments:
|
|
|
540
551
|
>>> foo(1)
|
|
541
552
|
>>> foo(a=1)
|
|
542
553
|
|
|
543
|
-
..
|
|
554
|
+
.. versionchanged:: 8.0
|
|
544
555
|
|
|
545
|
-
Passing :const:`None` as `cache` parameter is
|
|
546
|
-
|
|
547
|
-
mainly for consistency with :func:`cachedmethod`, which no
|
|
548
|
-
longer supports returning :const:`None` from `cache(self)`.
|
|
556
|
+
Passing :const:`None` as the `cache` parameter is no longer
|
|
557
|
+
supported, and now raises a :exc:`TypeError`.
|
|
549
558
|
|
|
550
559
|
|
|
551
560
|
.. decorator:: cachedmethod(cache, key=cachetools.keys.methodkey, lock=None, condition=None, info=False)
|
|
@@ -631,6 +640,17 @@ often called with the same arguments:
|
|
|
631
640
|
e.g. :func:`cachetools.keys.hashkey`, if you need `self` to be part
|
|
632
641
|
of the cache key.
|
|
633
642
|
|
|
643
|
+
Accessing a decorated method through its class, e.g.
|
|
644
|
+
``Cls.method``, yields a callable that takes the instance as its
|
|
645
|
+
first positional argument, or as a keyword argument named after the
|
|
646
|
+
wrapped method's first parameter. Unlike the method of an
|
|
647
|
+
instance, this callable does not provide the :attr:`cache`,
|
|
648
|
+
:attr:`cache_lock`, etc. attributes or the :meth:`cache_clear()`
|
|
649
|
+
and :meth:`cache_info()` functions, so this usage pattern is not
|
|
650
|
+
encouraged. Note also that pickling or deep-copying an instance
|
|
651
|
+
restores its methods with fresh :meth:`cache_info()` statistics,
|
|
652
|
+
i.e. with the hit and miss counts reset to zero.
|
|
653
|
+
|
|
634
654
|
.. note::
|
|
635
655
|
|
|
636
656
|
Once a decorated method has been accessed on an instance, a
|
|
@@ -684,7 +704,8 @@ often called with the same arguments:
|
|
|
684
704
|
|
|
685
705
|
Note how keyword arguments are used with :func:`functools.partial`
|
|
686
706
|
to create distinct cache keys, to avoid issues with
|
|
687
|
-
:func:`~cachetools.keys.methodkey` skipping its initial `self`
|
|
707
|
+
:func:`~cachetools.keys.methodkey` skipping its initial `self`
|
|
708
|
+
argument.
|
|
688
709
|
|
|
689
710
|
.. versionchanged:: 7.0
|
|
690
711
|
|
|
@@ -698,23 +719,38 @@ often called with the same arguments:
|
|
|
698
719
|
Require :attr:`__dict__` to be a mutable mapping to support
|
|
699
720
|
per-instance :meth:`cache_info()`.
|
|
700
721
|
|
|
701
|
-
Returning :const:`None` from `cache(self)` is no longer
|
|
722
|
+
Returning :const:`None` from `cache(self)` is no longer
|
|
723
|
+
supported.
|
|
702
724
|
|
|
703
|
-
..
|
|
725
|
+
.. versionchanged:: 8.0
|
|
704
726
|
|
|
705
|
-
Using :func:`cachedmethod` with :func:`classmethod` is
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
descriptors
|
|
709
|
-
|
|
710
|
-
using :func:`cache_info()`, an instance method is already
|
|
711
|
-
required.
|
|
727
|
+
Using :func:`cachedmethod` with :func:`classmethod` is no longer
|
|
728
|
+
supported, and now raises a :exc:`TypeError`. This is mostly
|
|
729
|
+
due to the fact that chaining descriptors, i.e. using
|
|
730
|
+
descriptors with :func:`classmethod`, was deprecated in Python
|
|
731
|
+
3.11 and removed in Python 3.13.
|
|
712
732
|
|
|
713
733
|
Using :func:`cachedmethod` with an instance that does not
|
|
714
|
-
provide a mutable :attr:`__dict__` attribute is
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
734
|
+
provide a mutable :attr:`__dict__` attribute is no longer
|
|
735
|
+
supported, and now raises a :exc:`TypeError`.
|
|
736
|
+
|
|
737
|
+
.. autoclass:: CacheInfo
|
|
738
|
+
|
|
739
|
+
A named tuple with the fields `hits`, `misses`, `maxsize` and
|
|
740
|
+
`currsize`, as returned by the :meth:`cache_info()` function of the
|
|
741
|
+
:func:`cached` and :func:`cachedmethod` decorators. If the
|
|
742
|
+
decorator's `cache` is a :class:`Cache` instance, `maxsize` and
|
|
743
|
+
`currsize` report its :attr:`~Cache.maxsize` and
|
|
744
|
+
:attr:`~Cache.currsize` properties. For any other mutable mapping,
|
|
745
|
+
`maxsize` is :const:`None` and `currsize` is `len(cache)`, so
|
|
746
|
+
custom cache classes used with `info=True` must support
|
|
747
|
+
:func:`len`.
|
|
748
|
+
|
|
749
|
+
.. versionchanged:: 8.0
|
|
750
|
+
|
|
751
|
+
:class:`CacheInfo` is now part of the public API. Cache objects
|
|
752
|
+
that are not :class:`Cache` instances must support :func:`len`
|
|
753
|
+
when used with `info=True`.
|
|
718
754
|
|
|
719
755
|
|
|
720
756
|
*****************************************************************
|
|
@@ -863,4 +899,5 @@ all the decorators in this module are thread-safe.
|
|
|
863
899
|
.. _context manager: https://docs.python.org/3/glossary.html#term-context-manager
|
|
864
900
|
.. _mapping: https://docs.python.org/3/glossary.html#term-mapping
|
|
865
901
|
.. _mutable: https://docs.python.org/3/glossary.html#term-mutable
|
|
902
|
+
.. _pickled: https://docs.python.org/3/library/pickle.html
|
|
866
903
|
.. _thread-safe: https://en.wikipedia.org/wiki/Thread_safety
|
|
@@ -15,14 +15,13 @@ maintainers = [
|
|
|
15
15
|
readme = "README.rst"
|
|
16
16
|
license = "MIT"
|
|
17
17
|
license-files = ["LICENSE"]
|
|
18
|
-
requires-python = ">= 3.
|
|
18
|
+
requires-python = ">= 3.11"
|
|
19
19
|
classifiers = [
|
|
20
20
|
"Development Status :: 5 - Production/Stable",
|
|
21
21
|
"Intended Audience :: Developers",
|
|
22
22
|
"Operating System :: OS Independent",
|
|
23
23
|
"Programming Language :: Python",
|
|
24
24
|
"Programming Language :: Python :: 3",
|
|
25
|
-
"Programming Language :: Python :: 3.10",
|
|
26
25
|
"Programming Language :: Python :: 3.11",
|
|
27
26
|
"Programming Language :: Python :: 3.12",
|
|
28
27
|
"Programming Language :: Python :: 3.13",
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
__all__ = (
|
|
4
4
|
"Cache",
|
|
5
|
+
"CacheInfo",
|
|
5
6
|
"FIFOCache",
|
|
6
7
|
"LFUCache",
|
|
7
8
|
"LRUCache",
|
|
@@ -12,7 +13,7 @@ __all__ = (
|
|
|
12
13
|
"cachedmethod",
|
|
13
14
|
)
|
|
14
15
|
|
|
15
|
-
__version__ = "
|
|
16
|
+
__version__ = "8.0.1"
|
|
16
17
|
|
|
17
18
|
import collections
|
|
18
19
|
import collections.abc
|
|
@@ -351,7 +352,11 @@ class LRUCache(Cache):
|
|
|
351
352
|
|
|
352
353
|
def __setitem__(self, key, value, cache_setitem=Cache.__setitem__):
|
|
353
354
|
cache_setitem(self, key, value)
|
|
354
|
-
self.
|
|
355
|
+
order = self.__order
|
|
356
|
+
if key in order:
|
|
357
|
+
order.move_to_end(key)
|
|
358
|
+
else:
|
|
359
|
+
order[key] = None
|
|
355
360
|
|
|
356
361
|
def __delitem__(self, key, cache_delitem=Cache.__delitem__):
|
|
357
362
|
cache_delitem(self, key)
|
|
@@ -377,13 +382,6 @@ class LRUCache(Cache):
|
|
|
377
382
|
Cache.clear(self)
|
|
378
383
|
self.__order.clear()
|
|
379
384
|
|
|
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
385
|
|
|
388
386
|
class RRCache(Cache):
|
|
389
387
|
"""Random Replacement (RR) cache implementation."""
|
|
@@ -831,11 +829,17 @@ class TLRUCache(_TimedCache):
|
|
|
831
829
|
cache_delitem(self, key)
|
|
832
830
|
|
|
833
831
|
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
832
|
+
class CacheInfo(collections.namedtuple("CacheInfo", "hits misses maxsize currsize")):
|
|
833
|
+
"""Named tuple holding cache statistics."""
|
|
834
|
+
|
|
835
|
+
__slots__ = ()
|
|
836
|
+
|
|
837
|
+
@classmethod
|
|
838
|
+
def _make_info(cls, cache, hits, misses):
|
|
839
|
+
if isinstance(cache, Cache):
|
|
840
|
+
return cls(hits, misses, cache.maxsize, cache.currsize)
|
|
841
|
+
else:
|
|
842
|
+
return cls(hits, misses, None, len(cache))
|
|
839
843
|
|
|
840
844
|
|
|
841
845
|
def cached(cache, key=keys.hashkey, lock=None, condition=None, info=False):
|
|
@@ -845,26 +849,12 @@ def cached(cache, key=keys.hashkey, lock=None, condition=None, info=False):
|
|
|
845
849
|
"""
|
|
846
850
|
from ._cached import _wrapper
|
|
847
851
|
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
if isinstance(cache, Cache):
|
|
852
|
+
if cache is None: # no longer supported since v8.0.0
|
|
853
|
+
raise TypeError("cache must not be None")
|
|
851
854
|
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
elif isinstance(cache, collections.abc.Mapping):
|
|
856
|
-
|
|
857
|
-
def make_info(hits, misses):
|
|
858
|
-
return _CacheInfo(hits, misses, None, len(cache))
|
|
859
|
-
|
|
860
|
-
else:
|
|
861
|
-
|
|
862
|
-
def make_info(hits, misses):
|
|
863
|
-
return _CacheInfo(hits, misses, 0, 0)
|
|
864
|
-
|
|
865
|
-
return _wrapper(func, cache, key, lock, condition, info=make_info)
|
|
866
|
-
else:
|
|
867
|
-
return _wrapper(func, cache, key, lock, condition)
|
|
855
|
+
def decorator(func):
|
|
856
|
+
make_info = CacheInfo._make_info if info else None
|
|
857
|
+
return _wrapper(func, cache, key, lock, condition, make_info)
|
|
868
858
|
|
|
869
859
|
return decorator
|
|
870
860
|
|
|
@@ -876,19 +866,11 @@ def cachedmethod(cache, key=keys.methodkey, lock=None, condition=None, info=Fals
|
|
|
876
866
|
"""
|
|
877
867
|
from ._cachedmethod import _wrapper
|
|
878
868
|
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
def make_info(cache, hits, misses):
|
|
883
|
-
if isinstance(cache, Cache):
|
|
884
|
-
return _CacheInfo(hits, misses, cache.maxsize, cache.currsize)
|
|
885
|
-
elif isinstance(cache, collections.abc.Mapping):
|
|
886
|
-
return _CacheInfo(hits, misses, None, len(cache))
|
|
887
|
-
else:
|
|
888
|
-
raise TypeError("cache(self) must return a mutable mapping")
|
|
869
|
+
if not callable(cache):
|
|
870
|
+
raise TypeError("cache must be callable")
|
|
889
871
|
|
|
890
|
-
|
|
891
|
-
else
|
|
892
|
-
|
|
872
|
+
def decorator(method):
|
|
873
|
+
make_info = CacheInfo._make_info if info else None
|
|
874
|
+
return _wrapper(method, cache, key, lock, condition, make_info)
|
|
893
875
|
|
|
894
876
|
return decorator
|
|
@@ -17,6 +17,7 @@ from typing import (
|
|
|
17
17
|
|
|
18
18
|
__all__: Final = (
|
|
19
19
|
"Cache",
|
|
20
|
+
"CacheInfo",
|
|
20
21
|
"FIFOCache",
|
|
21
22
|
"LFUCache",
|
|
22
23
|
"LRUCache",
|
|
@@ -138,7 +139,7 @@ class TLRUCache(_TimedCache[_KT, _VT, _TT]):
|
|
|
138
139
|
def ttu(self) -> Callable[[_KT, _VT, _TT], _TT]: ...
|
|
139
140
|
def expire(self, time: _TT | None = None) -> list[tuple[_KT, _VT]]: ...
|
|
140
141
|
|
|
141
|
-
class
|
|
142
|
+
class CacheInfo(NamedTuple):
|
|
142
143
|
hits: int
|
|
143
144
|
misses: int
|
|
144
145
|
maxsize: float | None
|
|
@@ -159,7 +160,7 @@ class _cached_wrapper(Generic[_P, _R]):
|
|
|
159
160
|
__name__: str
|
|
160
161
|
__qualname__: str
|
|
161
162
|
__doc__: str | None
|
|
162
|
-
cache: MutableMapping[Any, Any]
|
|
163
|
+
cache: MutableMapping[Any, Any]
|
|
163
164
|
cache_key: Callable[..., Any] = ...
|
|
164
165
|
cache_lock: AbstractContextManager[Any] | None = None
|
|
165
166
|
cache_condition: _AbstractCondition | None = None
|
|
@@ -198,28 +199,28 @@ class _cached_wrapper_info(_cached_wrapper[_P, _R]):
|
|
|
198
199
|
def __get__( # pyright: ignore[reportIncompatibleMethodOverride]
|
|
199
200
|
self, obj: object, objtype: type | None = None
|
|
200
201
|
) -> _cached_wrapper_info[_P, _R]: ...
|
|
201
|
-
def cache_info(self) ->
|
|
202
|
+
def cache_info(self) -> CacheInfo: ...
|
|
202
203
|
|
|
203
204
|
@overload
|
|
204
205
|
def cached(
|
|
205
|
-
cache: MutableMapping[_KT, Any]
|
|
206
|
+
cache: MutableMapping[_KT, Any],
|
|
206
207
|
key: Callable[..., _KT] = ...,
|
|
207
208
|
lock: AbstractContextManager[Any] | None = None,
|
|
208
209
|
condition: _AbstractCondition | None = None,
|
|
209
|
-
|
|
210
|
-
|
|
210
|
+
*,
|
|
211
|
+
info: Literal[True],
|
|
212
|
+
) -> Callable[[Callable[_P, _R]], _cached_wrapper_info[_P, _R]]: ...
|
|
211
213
|
@overload
|
|
212
214
|
def cached(
|
|
213
|
-
cache: MutableMapping[_KT, Any]
|
|
215
|
+
cache: MutableMapping[_KT, Any],
|
|
214
216
|
key: Callable[..., _KT] = ...,
|
|
215
217
|
lock: AbstractContextManager[Any] | None = None,
|
|
216
218
|
condition: _AbstractCondition | None = None,
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
) -> Callable[[Callable[_P, _R]], _cached_wrapper_info[_P, _R]]: ...
|
|
219
|
+
info: Literal[False] = ...,
|
|
220
|
+
) -> Callable[[Callable[_P, _R]], _cached_wrapper[_P, _R]]: ...
|
|
220
221
|
@overload
|
|
221
222
|
def cached(
|
|
222
|
-
cache: MutableMapping[_KT, Any]
|
|
223
|
+
cache: MutableMapping[_KT, Any],
|
|
223
224
|
key: Callable[..., _KT],
|
|
224
225
|
lock: AbstractContextManager[Any] | None,
|
|
225
226
|
condition: _AbstractCondition | None,
|
|
@@ -240,11 +241,7 @@ class _cachedmethod_unbound(Generic[_P, _R]):
|
|
|
240
241
|
def __get__(
|
|
241
242
|
self, obj: Any, objtype: type | None = None
|
|
242
243
|
) -> _cachedmethod_wrapper[_P, _R]: ...
|
|
243
|
-
@overload
|
|
244
244
|
def __call__(self, obj: Any, /, *args: _P.args, **kwargs: _P.kwargs) -> _R: ...
|
|
245
|
-
# deprecated @classmethod use
|
|
246
|
-
@overload
|
|
247
|
-
def __call__(self, /, *args: _P.args, **kwargs: _P.kwargs) -> _R: ...
|
|
248
245
|
|
|
249
246
|
@type_check_only
|
|
250
247
|
class _cachedmethod_wrapper(Generic[_P, _R]):
|
|
@@ -278,7 +275,7 @@ class _cachedmethod_wrapper_info(_cachedmethod_wrapper[_P, _R]):
|
|
|
278
275
|
def __get__(
|
|
279
276
|
self, obj: Any, objtype: type | None = None
|
|
280
277
|
) -> _cachedmethod_wrapper_info[_P, _R]: ...
|
|
281
|
-
def cache_info(self) ->
|
|
278
|
+
def cache_info(self) -> CacheInfo: ...
|
|
282
279
|
|
|
283
280
|
@overload
|
|
284
281
|
def cachedmethod(
|
|
@@ -286,19 +283,19 @@ def cachedmethod(
|
|
|
286
283
|
key: Callable[..., _KT] = ...,
|
|
287
284
|
lock: Callable[[Any], AbstractContextManager[Any]] | None = None,
|
|
288
285
|
condition: Callable[[Any], _AbstractCondition] | None = None,
|
|
289
|
-
|
|
290
|
-
|
|
286
|
+
*,
|
|
287
|
+
info: Literal[True],
|
|
288
|
+
) -> Callable[
|
|
289
|
+
[Callable[Concatenate[Any, _P], _R]], _cachedmethod_wrapper_info[_P, _R]
|
|
290
|
+
]: ...
|
|
291
291
|
@overload
|
|
292
292
|
def cachedmethod(
|
|
293
293
|
cache: Callable[[Any], MutableMapping[_KT, Any]],
|
|
294
294
|
key: Callable[..., _KT] = ...,
|
|
295
295
|
lock: Callable[[Any], AbstractContextManager[Any]] | None = None,
|
|
296
296
|
condition: Callable[[Any], _AbstractCondition] | None = None,
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
) -> Callable[
|
|
300
|
-
[Callable[Concatenate[Any, _P], _R]], _cachedmethod_wrapper_info[_P, _R]
|
|
301
|
-
]: ...
|
|
297
|
+
info: Literal[False] = ...,
|
|
298
|
+
) -> Callable[[Callable[Concatenate[Any, _P], _R]], _cachedmethod_wrapper[_P, _R]]: ...
|
|
302
299
|
@overload
|
|
303
300
|
def cachedmethod(
|
|
304
301
|
cache: Callable[[Any], MutableMapping[_KT, Any]],
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""Function decorator helpers.
|
|
2
|
+
|
|
3
|
+
At least for now, the implementation prefers clarity and performance
|
|
4
|
+
over ease of maintenance, thus providing separate wrappers for all
|
|
5
|
+
valid combinations of parameters lock, condition and info.
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
# pyright: reportArgumentType=false, reportAttributeAccessIssue=false
|
|
10
|
+
|
|
11
|
+
__all__ = ()
|
|
12
|
+
|
|
13
|
+
import functools
|
|
14
|
+
import types
|
|
15
|
+
|
|
16
|
+
from ._decorator import (
|
|
17
|
+
_BasicMixin,
|
|
18
|
+
_ConditionInfoMixin,
|
|
19
|
+
_ConditionMixin,
|
|
20
|
+
_InfoMixin,
|
|
21
|
+
_LockedInfoMixin,
|
|
22
|
+
_LockedMixin,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class _WrapperBase:
|
|
27
|
+
"""Wrapper base class providing the public cache attributes."""
|
|
28
|
+
|
|
29
|
+
def __init__(self, func, cache, key, lock=None, cond=None):
|
|
30
|
+
# update first, so func.__dict__ cannot override wrapper state,
|
|
31
|
+
# e.g. when stacking decorators
|
|
32
|
+
functools.update_wrapper(self, func)
|
|
33
|
+
self.cache = cache
|
|
34
|
+
self.cache_key = key
|
|
35
|
+
self.cache_lock = lock
|
|
36
|
+
self.cache_condition = cond
|
|
37
|
+
|
|
38
|
+
# descriptor, copy and pickle support as for functools.lru_cache (C version)
|
|
39
|
+
def __get__(self, obj, objtype=None):
|
|
40
|
+
return self if obj is None else types.MethodType(self, obj)
|
|
41
|
+
|
|
42
|
+
def __copy__(self):
|
|
43
|
+
return self
|
|
44
|
+
|
|
45
|
+
def __deepcopy__(self, memo):
|
|
46
|
+
return self
|
|
47
|
+
|
|
48
|
+
def __reduce__(self):
|
|
49
|
+
return self.__qualname__
|
|
50
|
+
|
|
51
|
+
def __repr__(self):
|
|
52
|
+
return f"<cached {self.__wrapped__!r}>"
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class _BasicWrapper(_WrapperBase, _BasicMixin):
|
|
56
|
+
def __init__(self, func, cache, key):
|
|
57
|
+
_WrapperBase.__init__(self, func, cache, key)
|
|
58
|
+
_BasicMixin.__init__(self, func)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class _LockedWrapper(_WrapperBase, _LockedMixin):
|
|
62
|
+
def __init__(self, func, cache, key, lock):
|
|
63
|
+
_WrapperBase.__init__(self, func, cache, key, lock)
|
|
64
|
+
_LockedMixin.__init__(self, func)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class _ConditionWrapper(_WrapperBase, _ConditionMixin):
|
|
68
|
+
def __init__(self, func, cache, key, lock, cond):
|
|
69
|
+
_WrapperBase.__init__(self, func, cache, key, lock, cond)
|
|
70
|
+
_ConditionMixin.__init__(self, func)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class _InfoWrapper(_WrapperBase, _InfoMixin):
|
|
74
|
+
def __init__(self, func, cache, key, info):
|
|
75
|
+
_WrapperBase.__init__(self, func, cache, key)
|
|
76
|
+
_InfoMixin.__init__(self, func, info)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class _LockedInfoWrapper(_WrapperBase, _LockedInfoMixin):
|
|
80
|
+
def __init__(self, func, cache, key, lock, info):
|
|
81
|
+
_WrapperBase.__init__(self, func, cache, key, lock)
|
|
82
|
+
_LockedInfoMixin.__init__(self, func, info)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class _ConditionInfoWrapper(_WrapperBase, _ConditionInfoMixin):
|
|
86
|
+
def __init__(self, func, cache, key, lock, cond, info):
|
|
87
|
+
_WrapperBase.__init__(self, func, cache, key, lock, cond)
|
|
88
|
+
_ConditionInfoMixin.__init__(self, func, info)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _wrapper(func, cache, key, lock=None, cond=None, info=None):
|
|
92
|
+
if info is not None:
|
|
93
|
+
if cond is not None and lock is not None:
|
|
94
|
+
wrapper = _ConditionInfoWrapper(func, cache, key, lock, cond, info)
|
|
95
|
+
elif cond is not None:
|
|
96
|
+
wrapper = _ConditionInfoWrapper(func, cache, key, cond, cond, info)
|
|
97
|
+
elif lock is not None:
|
|
98
|
+
wrapper = _LockedInfoWrapper(func, cache, key, lock, info)
|
|
99
|
+
else:
|
|
100
|
+
wrapper = _InfoWrapper(func, cache, key, info)
|
|
101
|
+
else:
|
|
102
|
+
if cond is not None and lock is not None:
|
|
103
|
+
wrapper = _ConditionWrapper(func, cache, key, lock, cond)
|
|
104
|
+
elif cond is not None:
|
|
105
|
+
wrapper = _ConditionWrapper(func, cache, key, cond, cond)
|
|
106
|
+
elif lock is not None:
|
|
107
|
+
wrapper = _LockedWrapper(func, cache, key, lock)
|
|
108
|
+
else:
|
|
109
|
+
wrapper = _BasicWrapper(func, cache, key)
|
|
110
|
+
return wrapper
|