cachetools 7.2.4__tar.gz → 7.2.6__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.2.4 → cachetools-7.2.6}/CHANGELOG.rst +37 -15
- {cachetools-7.2.4/src/cachetools.egg-info → cachetools-7.2.6}/PKG-INFO +1 -1
- {cachetools-7.2.4 → cachetools-7.2.6}/docs/index.rst +91 -87
- {cachetools-7.2.4 → cachetools-7.2.6}/src/cachetools/__init__.py +41 -27
- {cachetools-7.2.4 → cachetools-7.2.6}/src/cachetools/__init__.pyi +8 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/src/cachetools/_cached.py +6 -2
- {cachetools-7.2.4 → cachetools-7.2.6}/src/cachetools/_cachedmethod.py +28 -16
- {cachetools-7.2.4 → cachetools-7.2.6}/src/cachetools/_descriptor.py +47 -6
- {cachetools-7.2.4 → cachetools-7.2.6}/src/cachetools/keys.py +22 -11
- {cachetools-7.2.4 → cachetools-7.2.6/src/cachetools.egg-info}/PKG-INFO +1 -1
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/test_cached.py +8 -8
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/test_cachedmethod.py +235 -26
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/test_keys.py +1 -2
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/test_threading.py +60 -11
- {cachetools-7.2.4 → cachetools-7.2.6}/LICENSE +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/MANIFEST.in +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/README.rst +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/docs/conf.py +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/pyproject.toml +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/setup.cfg +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/src/cachetools/func.py +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/src/cachetools/func.pyi +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/src/cachetools/keys.pyi +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/src/cachetools/py.typed +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/src/cachetools.egg-info/SOURCES.txt +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/src/cachetools.egg-info/dependency_links.txt +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/src/cachetools.egg-info/top_level.txt +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/__init__.py +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/test_cache.py +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/test_classmethod.py +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/test_fifo.py +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/test_func.py +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/test_lfu.py +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/test_lru.py +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/test_rr.py +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/test_tlru.py +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/tests/test_ttl.py +0 -0
- {cachetools-7.2.4 → cachetools-7.2.6}/tox.ini +0 -0
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
v7.2.6 (2026-10-26)
|
|
2
|
+
===================
|
|
3
|
+
|
|
4
|
+
- Various performance improvements.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
v7.2.5 (2026-10-25)
|
|
8
|
+
===================
|
|
9
|
+
|
|
10
|
+
- Fix ``@cachedmethod`` with ``condition`` for decorated methods
|
|
11
|
+
taking a ``pending`` keyword argument (broken since v7.0.0).
|
|
12
|
+
|
|
13
|
+
- Fix pickling and deep-copying of instances using ``@cachedmethod``
|
|
14
|
+
(broken since v7.0.0).
|
|
15
|
+
|
|
16
|
+
- Fix calling ``@cachedmethod`` decorated methods through the class
|
|
17
|
+
with the receiver (``self``) passed as a keyword argument (broken
|
|
18
|
+
since v7.2.4).
|
|
19
|
+
|
|
20
|
+
- Various documentation improvements.
|
|
21
|
+
|
|
22
|
+
|
|
1
23
|
v7.2.4 (2026-10-19)
|
|
2
24
|
===================
|
|
3
25
|
|
|
@@ -162,7 +184,7 @@ v7.0.3 (2026-03-05)
|
|
|
162
184
|
v7.0.2 (2026-03-02)
|
|
163
185
|
===================
|
|
164
186
|
|
|
165
|
-
- Provide more efficient ``clear()`` implementation for all
|
|
187
|
+
- Provide more efficient ``clear()`` implementation for all supported
|
|
166
188
|
Cache classes (courtesy Josep Pon Farreny).
|
|
167
189
|
|
|
168
190
|
|
|
@@ -226,7 +248,7 @@ v6.2.5 (2026-01-25)
|
|
|
226
248
|
v6.2.4 (2025-12-15)
|
|
227
249
|
===================
|
|
228
250
|
|
|
229
|
-
- Fix license information displayed on PyPI
|
|
251
|
+
- Fix license information displayed on PyPI by using an updated
|
|
230
252
|
version of ``twine`` for uploading.
|
|
231
253
|
|
|
232
254
|
|
|
@@ -299,7 +321,7 @@ v6.0.0 (2025-05-23)
|
|
|
299
321
|
- Convert the ``cachetools.func`` decorators to use a
|
|
300
322
|
``threading.Condition`` instance to deal with `cache stampede
|
|
301
323
|
<https://en.wikipedia.org/wiki/Cache_stampede>`_ issues. Note that
|
|
302
|
-
this *may* result in a
|
|
324
|
+
this *may* result in a noticeable performance degradation, depending
|
|
303
325
|
on your actual use case.
|
|
304
326
|
|
|
305
327
|
- Deprecate support for ``cache(self)`` returning ``None`` to suppress
|
|
@@ -424,7 +446,7 @@ v5.0.0 (2021-12-21)
|
|
|
424
446
|
|
|
425
447
|
``from cachetools.ttl import TTLCache``
|
|
426
448
|
|
|
427
|
-
will no longer work.
|
|
449
|
+
will no longer work. Use
|
|
428
450
|
|
|
429
451
|
``from cachetools import TTLCache``
|
|
430
452
|
|
|
@@ -541,7 +563,7 @@ v3.1.1 (2019-05-23)
|
|
|
541
563
|
|
|
542
564
|
- Document how to use shared caches with ``@cachedmethod``.
|
|
543
565
|
|
|
544
|
-
- Fix pickling/unpickling of cache keys
|
|
566
|
+
- Fix pickling/unpickling of cache keys.
|
|
545
567
|
|
|
546
568
|
|
|
547
569
|
v3.1.0 (2019-01-29)
|
|
@@ -670,17 +692,17 @@ v1.1.0 (2015-08-28)
|
|
|
670
692
|
|
|
671
693
|
- Add ``hashkey`` and ``typedkey`` functions.
|
|
672
694
|
|
|
673
|
-
- Add
|
|
695
|
+
- Add ``key`` and ``lock`` arguments to ``@cachedmethod``.
|
|
674
696
|
|
|
675
697
|
- Set ``__wrapped__`` attributes for Python versions < 3.2.
|
|
676
698
|
|
|
677
699
|
- Move ``functools`` compatible decorators to ``cachetools.func``.
|
|
678
700
|
|
|
679
|
-
- Deprecate ``@cachedmethod``
|
|
701
|
+
- Deprecate ``@cachedmethod`` ``typed`` argument.
|
|
680
702
|
|
|
681
|
-
- Deprecate
|
|
703
|
+
- Deprecate ``cache`` attribute for ``@cachedmethod`` wrappers.
|
|
682
704
|
|
|
683
|
-
- Deprecate
|
|
705
|
+
- Deprecate ``getsizeof`` and ``lock`` arguments for ``cachetools.func``
|
|
684
706
|
decorator.
|
|
685
707
|
|
|
686
708
|
|
|
@@ -739,13 +761,13 @@ v0.8.0 (2014-12-03)
|
|
|
739
761
|
|
|
740
762
|
- Add ``Cache.__missing__()``.
|
|
741
763
|
|
|
742
|
-
- Feature freeze for
|
|
764
|
+
- Feature freeze for ``v1.0``.
|
|
743
765
|
|
|
744
766
|
|
|
745
767
|
v0.7.1 (2014-11-22)
|
|
746
768
|
===================
|
|
747
769
|
|
|
748
|
-
- Fix
|
|
770
|
+
- Fix ``MANIFEST.in``.
|
|
749
771
|
|
|
750
772
|
|
|
751
773
|
v0.7.0 (2014-11-12)
|
|
@@ -753,7 +775,7 @@ v0.7.0 (2014-11-12)
|
|
|
753
775
|
|
|
754
776
|
- Deprecate ``TTLCache.ExpiredError``.
|
|
755
777
|
|
|
756
|
-
- Add
|
|
778
|
+
- Add ``choice`` argument to ``RRCache`` constructor.
|
|
757
779
|
|
|
758
780
|
- Refactor ``LFUCache``, ``LRUCache`` and ``TTLCache``.
|
|
759
781
|
|
|
@@ -768,7 +790,7 @@ v0.6.0 (2014-10-13)
|
|
|
768
790
|
|
|
769
791
|
- Support unsynchronized function decorators.
|
|
770
792
|
|
|
771
|
-
- Allow ``@cachedmethod.cache()`` to return None
|
|
793
|
+
- Allow ``@cachedmethod.cache()`` to return ``None``.
|
|
772
794
|
|
|
773
795
|
|
|
774
796
|
v0.5.1 (2014-09-25)
|
|
@@ -796,7 +818,7 @@ v0.4.0 (2014-06-16)
|
|
|
796
818
|
|
|
797
819
|
- Add ``Cache`` base class.
|
|
798
820
|
|
|
799
|
-
- Remove ``@cachedmethod``
|
|
821
|
+
- Remove ``@cachedmethod`` ``lock`` parameter.
|
|
800
822
|
|
|
801
823
|
|
|
802
824
|
v0.3.1 (2014-05-07)
|
|
@@ -804,7 +826,7 @@ v0.3.1 (2014-05-07)
|
|
|
804
826
|
|
|
805
827
|
- Add proper locking for ``cache_clear()`` and ``cache_info()``.
|
|
806
828
|
|
|
807
|
-
- Report
|
|
829
|
+
- Report ``size`` in ``cache_info()``.
|
|
808
830
|
|
|
809
831
|
|
|
810
832
|
v0.3.0 (2014-05-06)
|
|
@@ -36,21 +36,23 @@ Cache implementations
|
|
|
36
36
|
This module provides several classes implementing caches using
|
|
37
37
|
different cache algorithms. All these classes derive from class
|
|
38
38
|
:class:`Cache`, which in turn derives from
|
|
39
|
-
:class:`collections.abc.MutableMapping`, and provide
|
|
40
|
-
and :attr
|
|
39
|
+
:class:`collections.abc.MutableMapping`, and provide
|
|
40
|
+
:attr:`~Cache.maxsize` and :attr:`~Cache.currsize` properties to
|
|
41
|
+
retrieve the maximum and current
|
|
41
42
|
size of the cache. When a cache is full, :meth:`Cache.__setitem__()`
|
|
42
43
|
calls :meth:`self.popitem()` repeatedly until there is enough room for
|
|
43
44
|
the item to be added.
|
|
44
45
|
|
|
45
|
-
In general, a cache's size is the total size of its
|
|
46
|
-
Therefore, :class:`Cache` provides a :meth
|
|
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, which
|
|
47
48
|
returns the size of a given `value`. The default implementation of
|
|
48
|
-
:meth
|
|
49
|
+
:meth:`~Cache.getsizeof` returns :const:`1` irrespective of its argument,
|
|
49
50
|
making the cache's size equal to the number of its items, or
|
|
50
51
|
`len(cache)`. For convenience, all cache classes accept an optional
|
|
51
52
|
named constructor parameter `getsizeof`, which may specify a callable
|
|
52
|
-
``getsizeof(value)
|
|
53
|
-
value.
|
|
53
|
+
``getsizeof(value)`` returning a non-negative number as the size of
|
|
54
|
+
an item's value. An item whose size exceeds :attr:`~Cache.maxsize` cannot be
|
|
55
|
+
stored, and trying to do so raises :exc:`ValueError`.
|
|
54
56
|
|
|
55
57
|
The values of a :class:`Cache` are mutable by default, as are e.g. the
|
|
56
58
|
values of a :class:`dict`. It is the user's responsibility to take
|
|
@@ -92,6 +94,9 @@ inserted into the cache.
|
|
|
92
94
|
This class counts how often an item is retrieved, and discards the
|
|
93
95
|
items used least often to make space when necessary.
|
|
94
96
|
|
|
97
|
+
Updating a cache item via :meth:`LFUCache.__setitem__` also counts
|
|
98
|
+
as a use of the item, i.e. it will increment the item's use count.
|
|
99
|
+
|
|
95
100
|
.. autoclass:: LRUCache(maxsize, getsizeof=None)
|
|
96
101
|
:members: popitem
|
|
97
102
|
|
|
@@ -113,8 +118,8 @@ inserted into the cache.
|
|
|
113
118
|
:members: popitem, timer, ttl
|
|
114
119
|
|
|
115
120
|
This class associates a time-to-live value with each item. Items
|
|
116
|
-
that expire because they have exceeded their time-to-live will
|
|
117
|
-
|
|
121
|
+
that expire because they have exceeded their time-to-live will no
|
|
122
|
+
longer be accessible, and will be removed eventually. If no
|
|
118
123
|
expired items are there to remove, the least recently used items
|
|
119
124
|
will be discarded first to make space when necessary.
|
|
120
125
|
|
|
@@ -140,14 +145,13 @@ inserted into the cache.
|
|
|
140
145
|
|
|
141
146
|
.. method:: expire(self, time=None)
|
|
142
147
|
|
|
143
|
-
Expired items will be removed from a cache
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
:attr:`timer`.
|
|
148
|
+
Expired items will be removed from a cache lazily, e.g. when
|
|
149
|
+
:meth:`__setitem__` is next called, and therefore may still
|
|
150
|
+
claim memory. Calling this method removes all items whose
|
|
151
|
+
time-to-live would have expired by `time`, so garbage collection
|
|
152
|
+
is free to reuse their memory. If `time` is :const:`None`, this
|
|
153
|
+
removes all items that have expired by the current value
|
|
154
|
+
returned by :attr:`timer`.
|
|
151
155
|
|
|
152
156
|
:returns: An iterable of expired `(key, value)` pairs.
|
|
153
157
|
|
|
@@ -187,20 +191,19 @@ inserted into the cache.
|
|
|
187
191
|
cache = TLRUCache(maxsize=10, ttu=datetime_ttu, timer=datetime.now)
|
|
188
192
|
|
|
189
193
|
Items that expire because they have exceeded their time-to-use will
|
|
190
|
-
|
|
194
|
+
no longer be accessible, and will be removed eventually. If no
|
|
191
195
|
expired items are there to remove, the least recently used items
|
|
192
196
|
will be discarded first to make space when necessary.
|
|
193
197
|
|
|
194
198
|
.. method:: expire(self, time=None)
|
|
195
199
|
|
|
196
|
-
Expired items will be removed from a cache
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
:attr:`timer`.
|
|
200
|
+
Expired items will be removed from a cache lazily, e.g. when
|
|
201
|
+
:meth:`__setitem__` is next called, and therefore may still
|
|
202
|
+
claim memory. Calling this method removes all items whose
|
|
203
|
+
time-to-use would have expired by `time`, so garbage collection
|
|
204
|
+
is free to reuse their memory. If `time` is :const:`None`, this
|
|
205
|
+
removes all items that have expired by the current value
|
|
206
|
+
returned by :attr:`timer`.
|
|
204
207
|
|
|
205
208
|
:returns: An iterable of expired `(key, value)` pairs.
|
|
206
209
|
|
|
@@ -208,10 +211,11 @@ inserted into the cache.
|
|
|
208
211
|
Extending cache classes
|
|
209
212
|
=======================
|
|
210
213
|
|
|
211
|
-
Sometimes it may be desirable to
|
|
212
|
-
evicted, i.e. removed from a cache to make room for new items
|
|
213
|
-
all cache implementations call :meth:`popitem` to
|
|
214
|
-
cache, this can be achieved by overriding this
|
|
214
|
+
Sometimes it may be desirable to be notified when cache items are
|
|
215
|
+
evicted, i.e. removed from a cache to make room for new items, and
|
|
216
|
+
which ones. Since all cache implementations call :meth:`popitem` to
|
|
217
|
+
evict items from the cache, this can be achieved by overriding this
|
|
218
|
+
method in a subclass:
|
|
215
219
|
|
|
216
220
|
.. doctest::
|
|
217
221
|
|
|
@@ -229,10 +233,10 @@ cache, this can be achieved by overriding this method in a subclass:
|
|
|
229
233
|
|
|
230
234
|
With :class:`TTLCache` and :class:`TLRUCache`, items may also be
|
|
231
235
|
removed after they expire. In this case, :meth:`popitem` will *not*
|
|
232
|
-
be called, but :meth:`expire` will be called from the
|
|
233
|
-
|
|
234
|
-
pairs. By overriding :meth:`expire`, a subclass will
|
|
235
|
-
expired items:
|
|
236
|
+
be called, but :meth:`expire` will be called lazily, e.g. from the
|
|
237
|
+
next :meth:`__setitem__`, and will return an iterable of the expired
|
|
238
|
+
`(key, value)` pairs. By overriding :meth:`expire`, a subclass will
|
|
239
|
+
be able to track expired items:
|
|
236
240
|
|
|
237
241
|
.. doctest::
|
|
238
242
|
|
|
@@ -329,16 +333,16 @@ often called with the same arguments:
|
|
|
329
333
|
therefore must be `thread-safe`_ by itself.
|
|
330
334
|
|
|
331
335
|
If `condition` is not :const:`None`, it must specify a `condition
|
|
332
|
-
variable`_, i.e. an object providing :
|
|
333
|
-
:
|
|
336
|
+
variable`_, i.e. an object providing :meth:`wait()`,
|
|
337
|
+
:meth:`wait_for()`, :meth:`notify()` and :meth:`notify_all()`
|
|
334
338
|
methods as defined by :class:`threading.Condition`. Using a
|
|
335
339
|
`condition` variable will prevent concurrent execution of the
|
|
336
340
|
wrapped function with *identical* parameters, or cache keys.
|
|
337
341
|
Instead, a calling thread will check if an identical function call
|
|
338
|
-
is already executing, and will then
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
+
is already executing, and will then wait for the pending call to
|
|
343
|
+
finish. The executing thread will notify any waiting threads as
|
|
344
|
+
soon as the function completes, which will then return the cached
|
|
345
|
+
function result.
|
|
342
346
|
|
|
343
347
|
.. note::
|
|
344
348
|
|
|
@@ -346,7 +350,7 @@ often called with the same arguments:
|
|
|
346
350
|
make :func:`cached` `thread-safe`_, it may still be subject to
|
|
347
351
|
`cache stampede`_ issues under high load, depending on your
|
|
348
352
|
actual use case. Providing a `condition` variable will mitigate
|
|
349
|
-
these situations, but will
|
|
353
|
+
these situations, but will incur a performance penalty.
|
|
350
354
|
|
|
351
355
|
If no separate `lock` parameter is provided, `condition` must also
|
|
352
356
|
implement the `context manager`_ protocol, and will also be used to
|
|
@@ -374,7 +378,7 @@ often called with the same arguments:
|
|
|
374
378
|
get_pep.cache.pop(key, None)
|
|
375
379
|
|
|
376
380
|
For the common use case of clearing or invalidating the cache, the
|
|
377
|
-
decorator also provides a :
|
|
381
|
+
decorator also provides a :meth:`cache_clear()` function which
|
|
378
382
|
takes care of locking automatically, if needed:
|
|
379
383
|
|
|
380
384
|
.. testcode::
|
|
@@ -389,8 +393,8 @@ often called with the same arguments:
|
|
|
389
393
|
|
|
390
394
|
.. note::
|
|
391
395
|
|
|
392
|
-
|
|
393
|
-
|
|
396
|
+
This will incur a small performance penalty, so it has to be
|
|
397
|
+
enabled explicitly.
|
|
394
398
|
|
|
395
399
|
.. doctest::
|
|
396
400
|
|
|
@@ -427,18 +431,18 @@ often called with the same arguments:
|
|
|
427
431
|
>>> # compute Fibonacci numbers
|
|
428
432
|
>>> @cached(numcache, key=partial(hashkey, 'fib'))
|
|
429
433
|
... def fib(n):
|
|
430
|
-
...
|
|
434
|
+
... return n if n < 2 else fib(n - 1) + fib(n - 2)
|
|
431
435
|
|
|
432
436
|
>>> # compute Lucas numbers
|
|
433
437
|
>>> @cached(numcache, key=partial(hashkey, 'luc'))
|
|
434
438
|
... def luc(n):
|
|
435
|
-
...
|
|
439
|
+
... return 2 - n if n < 2 else luc(n - 1) + luc(n - 2)
|
|
436
440
|
|
|
437
441
|
>>> fib(42)
|
|
438
442
|
267914296
|
|
439
443
|
>>> luc(42)
|
|
440
444
|
599074578
|
|
441
|
-
>>>
|
|
445
|
+
>>> sorted(numcache.items())
|
|
442
446
|
[..., (('fib', 42), 267914296), ..., (('luc', 42), 599074578)]
|
|
443
447
|
|
|
444
448
|
|
|
@@ -507,7 +511,7 @@ often called with the same arguments:
|
|
|
507
511
|
.. doctest::
|
|
508
512
|
|
|
509
513
|
>>> def foo(a=1):
|
|
510
|
-
... _foo(a)
|
|
514
|
+
... return _foo(a)
|
|
511
515
|
...
|
|
512
516
|
>>> @cached(LRUCache(maxsize=100))
|
|
513
517
|
... def _foo(a):
|
|
@@ -533,10 +537,10 @@ often called with the same arguments:
|
|
|
533
537
|
saves results in a cache.
|
|
534
538
|
|
|
535
539
|
The main difference between this and the :func:`cached` function
|
|
536
|
-
decorator is that `cache`, `lock` and `condition` are not
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
+
decorator is that `cache`, `lock` and `condition` are not objects
|
|
541
|
+
but functions. These will be called with `self` as their sole
|
|
542
|
+
argument to retrieve the cache, lock and condition objects for the
|
|
543
|
+
respective instance.
|
|
540
544
|
|
|
541
545
|
.. note::
|
|
542
546
|
|
|
@@ -549,15 +553,15 @@ often called with the same arguments:
|
|
|
549
553
|
`key`, `lock` and `condition` parameters are also available as
|
|
550
554
|
:attr:`cache`, :attr:`cache_key`, :attr:`cache_lock` and
|
|
551
555
|
:attr:`cache_condition` attributes of the wrapped instance method.
|
|
552
|
-
A :
|
|
553
|
-
an optional :
|
|
556
|
+
A :meth:`cache_clear()` function will also be provided, as well as
|
|
557
|
+
an optional :meth:`cache_info()` function reporting per-instance
|
|
554
558
|
cache statistics.
|
|
555
559
|
|
|
556
560
|
Also, mostly for efficiency, this decorator requires that the
|
|
557
561
|
:attr:`__dict__` attribute on each instance be a mutable mapping.
|
|
558
|
-
This means it will not work with some types, such as
|
|
559
|
-
|
|
560
|
-
|
|
562
|
+
This means it will not work with some types, such as those that
|
|
563
|
+
specify :attr:`__slots__` without including :attr:`__dict__` as one
|
|
564
|
+
of the defined slots.
|
|
561
565
|
|
|
562
566
|
One advantage of :func:`cachedmethod` over the :func:`cached`
|
|
563
567
|
function decorator is that cache properties such as `maxsize` can
|
|
@@ -587,7 +591,7 @@ often called with the same arguments:
|
|
|
587
591
|
>>> # remove an individual cached PEP from the cache
|
|
588
592
|
>>> key = peps.get.cache_key(320)
|
|
589
593
|
>>> with peps.get.cache_lock:
|
|
590
|
-
...
|
|
594
|
+
... del peps.get.cache[key]
|
|
591
595
|
|
|
592
596
|
>>> peps.get.cache_info()
|
|
593
597
|
CacheInfo(hits=3, misses=8, maxsize=32, currsize=7)
|
|
@@ -601,13 +605,12 @@ often called with the same arguments:
|
|
|
601
605
|
The `key` function will be called as `key(self, *args, **kwargs)`
|
|
602
606
|
to retrieve a suitable cache key. Note that the default `key`
|
|
603
607
|
function, :func:`cachetools.keys.methodkey`, ignores its first
|
|
604
|
-
implicit argument, i.e.
|
|
605
|
-
reasons, but also ensures that
|
|
606
|
-
hashable.
|
|
608
|
+
implicit argument, i.e. `self`. This is mostly for historical
|
|
609
|
+
reasons, but also ensures that `self` does not have to be hashable.
|
|
607
610
|
|
|
608
611
|
You may provide a different `key` function,
|
|
609
|
-
e.g. :func:`cachetools.keys.hashkey`, if you need
|
|
610
|
-
|
|
612
|
+
e.g. :func:`cachetools.keys.hashkey`, if you need `self` to be part
|
|
613
|
+
of the cache key.
|
|
611
614
|
|
|
612
615
|
When using a shared cache for multiple methods, be aware that
|
|
613
616
|
different cache keys must be created for each method even when
|
|
@@ -634,7 +637,7 @@ often called with the same arguments:
|
|
|
634
637
|
@cachedmethod(lambda self: self.cache, key=partial(methodkey, method='rfc'))
|
|
635
638
|
def get_rfc(self, num):
|
|
636
639
|
"""Retrieve text of an IETF Request for Comments"""
|
|
637
|
-
url = 'https://
|
|
640
|
+
url = 'https://www.rfc-editor.org/rfc/rfc%d.txt' % num
|
|
638
641
|
with urllib.request.urlopen(url) as s:
|
|
639
642
|
return s.read()
|
|
640
643
|
|
|
@@ -652,7 +655,7 @@ often called with the same arguments:
|
|
|
652
655
|
|
|
653
656
|
Note how keyword arguments are used with :func:`functools.partial`
|
|
654
657
|
to create distinct cache keys, to avoid issues with
|
|
655
|
-
:func
|
|
658
|
+
:func:`~cachetools.keys.methodkey` skipping its initial `self` argument.
|
|
656
659
|
|
|
657
660
|
.. versionchanged:: 7.0
|
|
658
661
|
|
|
@@ -664,7 +667,7 @@ often called with the same arguments:
|
|
|
664
667
|
instance methods, and are finally officially supported.
|
|
665
668
|
|
|
666
669
|
Require :attr:`__dict__` to be a mutable mapping to support
|
|
667
|
-
per-instance :
|
|
670
|
+
per-instance :meth:`cache_info()`.
|
|
668
671
|
|
|
669
672
|
Returning :const:`None` from `cache(self)` is no longer supported.
|
|
670
673
|
|
|
@@ -692,18 +695,19 @@ often called with the same arguments:
|
|
|
692
695
|
.. module:: cachetools.keys
|
|
693
696
|
|
|
694
697
|
This module provides several functions that can be used as key
|
|
695
|
-
functions with the :func
|
|
698
|
+
functions with the :func:`~cachetools.cached` and
|
|
699
|
+
:func:`~cachetools.cachedmethod` decorators:
|
|
696
700
|
|
|
697
701
|
.. autofunction:: hashkey
|
|
698
702
|
|
|
699
703
|
This function returns a :class:`tuple` instance suitable as a cache
|
|
700
|
-
key, provided the positional and
|
|
704
|
+
key, provided the positional and keyword arguments are hashable.
|
|
701
705
|
|
|
702
706
|
.. autofunction:: methodkey
|
|
703
707
|
|
|
704
708
|
This function is similar to :func:`hashkey`, but ignores its
|
|
705
709
|
first positional argument, i.e. `self` when used with the
|
|
706
|
-
:func
|
|
710
|
+
:func:`~cachetools.cachedmethod` decorator.
|
|
707
711
|
|
|
708
712
|
.. autofunction:: typedkey
|
|
709
713
|
|
|
@@ -715,12 +719,12 @@ functions with the :func:`cached` and :func:`cachedmethod` decorators:
|
|
|
715
719
|
|
|
716
720
|
This function is similar to :func:`typedkey`, but ignores its
|
|
717
721
|
first positional argument, i.e. `self` when used with the
|
|
718
|
-
:func
|
|
722
|
+
:func:`~cachetools.cachedmethod` decorator.
|
|
719
723
|
|
|
720
724
|
These functions can also be helpful when implementing custom key
|
|
721
725
|
functions for handling some non-hashable arguments. For example,
|
|
722
726
|
calling the following function with a dictionary as its `env` argument
|
|
723
|
-
will raise a :
|
|
727
|
+
will raise a :exc:`TypeError`, since :class:`dict` is not hashable::
|
|
724
728
|
|
|
725
729
|
@cached(LRUCache(maxsize=128))
|
|
726
730
|
def foo(x, y, z, env={}):
|
|
@@ -735,7 +739,7 @@ specially::
|
|
|
735
739
|
key += tuple(sorted(env.items()))
|
|
736
740
|
return key
|
|
737
741
|
|
|
738
|
-
The
|
|
742
|
+
The ``envkey`` function can then be used in decorator declarations
|
|
739
743
|
like this::
|
|
740
744
|
|
|
741
745
|
@cached(LRUCache(maxsize=128), key=envkey)
|
|
@@ -746,19 +750,19 @@ like this::
|
|
|
746
750
|
|
|
747
751
|
|
|
748
752
|
****************************************************************************
|
|
749
|
-
:mod:`cachetools.func` --- :func:`functools.lru_cache
|
|
753
|
+
:mod:`cachetools.func` --- :func:`functools.lru_cache`-compatible decorators
|
|
750
754
|
****************************************************************************
|
|
751
755
|
|
|
752
756
|
.. module:: cachetools.func
|
|
753
757
|
|
|
754
|
-
To ease migration from (or to)
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
758
|
+
To ease migration from (or to) :func:`functools.lru_cache`, this
|
|
759
|
+
module provides several memoizing function decorators with a similar
|
|
760
|
+
API. All these decorators wrap a function with a memoizing callable
|
|
761
|
+
that saves up to `maxsize` results, using different caching
|
|
762
|
+
strategies. If `maxsize` is set to :const:`None`, the size limit is
|
|
763
|
+
disabled and the cache can grow without bound. For :func:`ttl_cache`,
|
|
764
|
+
the time-to-live still applies, so cached results expire after `ttl`
|
|
765
|
+
even without a size limit.
|
|
762
766
|
|
|
763
767
|
If the optional argument `typed` is set to :const:`True`, function
|
|
764
768
|
arguments of different types will be cached separately. For example,
|
|
@@ -774,16 +778,16 @@ leaving the `maxsize` at its default value of 128::
|
|
|
774
778
|
sentence = sentence.casefold()
|
|
775
779
|
return sum(sentence.count(vowel) for vowel in 'aeiou')
|
|
776
780
|
|
|
777
|
-
The wrapped function is instrumented with a :
|
|
781
|
+
The wrapped function is instrumented with a :meth:`cache_parameters`
|
|
778
782
|
function that returns a new :class:`dict` showing the values for
|
|
779
783
|
`maxsize` and `typed`. This is for information purposes only.
|
|
780
784
|
Mutating the values has no effect.
|
|
781
785
|
|
|
782
|
-
The wrapped function is also instrumented with :
|
|
783
|
-
:
|
|
786
|
+
The wrapped function is also instrumented with :meth:`cache_info` and
|
|
787
|
+
:meth:`cache_clear` functions to provide information about cache
|
|
784
788
|
performance and clear the cache. Please see the
|
|
785
789
|
:func:`functools.lru_cache` documentation for details. Also note that
|
|
786
|
-
all the decorators in this module are thread-safe
|
|
790
|
+
all the decorators in this module are thread-safe.
|
|
787
791
|
|
|
788
792
|
|
|
789
793
|
.. decorator:: fifo_cache(user_function)
|
|
@@ -817,8 +821,8 @@ all the decorators in this module are thread-safe by default.
|
|
|
817
821
|
.. decorator:: ttl_cache(user_function)
|
|
818
822
|
ttl_cache(maxsize=128, ttl=600, timer=time.monotonic, typed=False)
|
|
819
823
|
|
|
820
|
-
Decorator
|
|
821
|
-
up to `maxsize` results based on a Least Recently Used (LRU)
|
|
824
|
+
Decorator that wraps a function with a memoizing callable that
|
|
825
|
+
saves up to `maxsize` results based on a Least Recently Used (LRU)
|
|
822
826
|
algorithm with a per-item time-to-live (TTL) value. By default,
|
|
823
827
|
the time-to-live is set to 600 seconds and :func:`time.monotonic`
|
|
824
828
|
is used to retrieve the current time.
|