blob: 869d3cbdf7aaafe9f75805ab1b0bd3aa9f68b601 [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
Thomas Kemmer40d27102021-08-09 22:11:55 +020013 :target: https://github.com/tkem/cachetools/actions/workflows/ci.yml
Thomas Kemmerfc477612021-08-09 21:57:40 +020014 :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
Thomas Kemmer2a7afc22021-09-29 22:10:14 +020075Typing stubs for this package are provided by typeshed_ and can be
76installed by running::
77
78 pip install types-cachetools
79
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010080
81Project Resources
82------------------------------------------------------------------------
83
Thomas Kemmeraa936f02019-12-15 20:46:13 +010084- `Documentation`_
85- `Issue tracker`_
86- `Source code`_
87- `Change log`_
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010088
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010089
90License
91------------------------------------------------------------------------
92
Thomas Kemmer1d99e162021-01-24 20:49:22 +010093Copyright (c) 2014-2021 Thomas Kemmer.
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010094
95Licensed under the `MIT License`_.
96
97
Thomas Kemmer1311be62021-03-08 22:27:57 +010098.. _@lru_cache: https://docs.python.org/3/library/functools.html#functools.lru_cache
99.. _mutable: https://docs.python.org/dev/glossary.html#term-mutable
100.. _mapping: https://docs.python.org/dev/glossary.html#term-mapping
101.. _cache algorithm: https://en.wikipedia.org/wiki/Cache_algorithms
Thomas Kemmera34b1fa2014-03-22 12:57:39 +0100102
Thomas Kemmeraa936f02019-12-15 20:46:13 +0100103.. _PyPI: https://pypi.org/project/cachetools/
Thomas Kemmer2a7afc22021-09-29 22:10:14 +0200104.. _typeshed: https://github.com/python/typeshed/
Thomas Kemmeraa936f02019-12-15 20:46:13 +0100105.. _Documentation: https://cachetools.readthedocs.io/
106.. _Issue tracker: https://github.com/tkem/cachetools/issues/
107.. _Source code: https://github.com/tkem/cachetools/
108.. _Change log: https://github.com/tkem/cachetools/blob/master/CHANGELOG.rst
Thomas Kemmer1311be62021-03-08 22:27:57 +0100109.. _MIT License: https://raw.github.com/tkem/cachetools/master/LICENSE