blob: 106d64a29984428453207fa28cd901227c7fd80a [file] [log] [blame]
Thomas Kemmerfa0e6062014-03-22 11:09:33 +01001cachetools
2========================================================================
3
Thomas Kemmer1311be62021-03-08 22:27:57 +01004.. image:: https://img.shields.io/pypi/v/cachetools
Thomas Kemmeraa936f02019-12-15 20:46:13 +01005 :target: https://pypi.org/project/cachetools/
6 :alt: Latest PyPI version
7
8.. image:: https://img.shields.io/readthedocs/cachetools
Thomas Kemmer1311be62021-03-08 22:27:57 +01009 :target: https://cachetools.readthedocs.io/
Thomas Kemmeraa936f02019-12-15 20:46:13 +010010 :alt: Documentation build status
11
Thomas Kemmerfc477612021-08-09 21:57:40 +020012.. image:: https://img.shields.io/github/workflow/status/tkem/cachetools/CI
13 :target: https://github.com/tkem/cachetools/actions
14 :alt: CI build status
Thomas Kemmeraa936f02019-12-15 20:46:13 +010015
Thomas Kemmerfc477612021-08-09 21:57:40 +020016.. image:: https://img.shields.io/codecov/c/github/tkem/cachetools/master.svg
17 :target: https://codecov.io/gh/tkem/cachetools
Thomas Kemmeraa936f02019-12-15 20:46:13 +010018 :alt: Test coverage
19
20.. image:: https://img.shields.io/github/license/tkem/cachetools
Thomas Kemmer1311be62021-03-08 22:27:57 +010021 :target: https://raw.github.com/tkem/cachetools/master/LICENSE
Thomas Kemmeraa936f02019-12-15 20:46:13 +010022 :alt: License
23
Thomas Kemmer187b13a2021-04-27 07:12:43 +020024.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
25 :target: https://github.com/psf/black
26 :alt: Code style: black
27
Thomas Kemmerbf206e02014-05-06 20:26:34 +020028This module provides various memoizing collections and decorators,
Thomas Kemmeraa936f02019-12-15 20:46:13 +010029including variants of the Python Standard Library's `@lru_cache`_
Thomas Kemmer53854452014-11-12 07:58:59 +010030function decorator.
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010031
Thomas Kemmereac1c252018-11-04 16:11:55 +010032.. code-block:: python
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010033
Thomas Kemmereac1c252018-11-04 16:11:55 +010034 from cachetools import cached, LRUCache, TTLCache
35
36 # speed up calculating Fibonacci numbers with dynamic programming
37 @cached(cache={})
38 def fib(n):
39 return n if n < 2 else fib(n - 1) + fib(n - 2)
40
41 # cache least recently used Python Enhancement Proposals
42 @cached(cache=LRUCache(maxsize=32))
43 def get_pep(num):
44 url = 'http://www.python.org/dev/peps/pep-%04d/' % num
45 with urllib.request.urlopen(url) as s:
46 return s.read()
47
48 # cache weather data for no longer than ten minutes
49 @cached(cache=TTLCache(maxsize=1024, ttl=600))
50 def get_weather(place):
51 return owm.weather_at_place(place).get_weather()
Thomas Kemmer35f85f22014-03-25 06:44:31 +010052
Thomas Kemmerb210c142014-06-16 20:22:12 +020053For the purpose of this module, a *cache* is a mutable_ mapping_ of a
Thomas Kemmer886f48e2014-12-19 18:30:57 +010054fixed maximum size. When the cache is full, i.e. by adding another
55item the cache would exceed its maximum size, the cache must choose
56which item(s) to discard based on a suitable `cache algorithm`_. In
57general, a cache's size is the total size of its items, and an item's
58size is a property or function of its value, e.g. the result of
59``sys.getsizeof(value)``. For the trivial but common case that each
Thomas Kemmerb6bc3502015-08-28 16:30:38 +020060item counts as ``1``, a cache's size is equal to the number of its
61items, or ``len(cache)``.
Thomas Kemmer35f85f22014-03-25 06:44:31 +010062
Thomas Kemmer5c7effc2015-09-07 20:21:56 +020063Multiple cache classes based on different caching algorithms are
64implemented, and decorators for easily memoizing function and method
65calls are provided, too.
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010066
67
68Installation
69------------------------------------------------------------------------
70
Thomas Kemmeraa936f02019-12-15 20:46:13 +010071cachetools is available from PyPI_ and can be installed by running::
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010072
Thomas Kemmeraa936f02019-12-15 20:46:13 +010073 pip install cachetools
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010074
75
76Project Resources
77------------------------------------------------------------------------
78
Thomas Kemmeraa936f02019-12-15 20:46:13 +010079- `Documentation`_
80- `Issue tracker`_
81- `Source code`_
82- `Change log`_
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010083
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010084
85License
86------------------------------------------------------------------------
87
Thomas Kemmer1d99e162021-01-24 20:49:22 +010088Copyright (c) 2014-2021 Thomas Kemmer.
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010089
90Licensed under the `MIT License`_.
91
92
Thomas Kemmer1311be62021-03-08 22:27:57 +010093.. _@lru_cache: https://docs.python.org/3/library/functools.html#functools.lru_cache
94.. _mutable: https://docs.python.org/dev/glossary.html#term-mutable
95.. _mapping: https://docs.python.org/dev/glossary.html#term-mapping
96.. _cache algorithm: https://en.wikipedia.org/wiki/Cache_algorithms
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010097
Thomas Kemmeraa936f02019-12-15 20:46:13 +010098.. _PyPI: https://pypi.org/project/cachetools/
99.. _Documentation: https://cachetools.readthedocs.io/
100.. _Issue tracker: https://github.com/tkem/cachetools/issues/
101.. _Source code: https://github.com/tkem/cachetools/
102.. _Change log: https://github.com/tkem/cachetools/blob/master/CHANGELOG.rst
Thomas Kemmer1311be62021-03-08 22:27:57 +0100103.. _MIT License: https://raw.github.com/tkem/cachetools/master/LICENSE