Thomas Kemmer | fa0e606 | 2014-03-22 11:09:33 +0100 | [diff] [blame] | 1 | cachetools |
| 2 | ======================================================================== |
| 3 | |
Thomas Kemmer | 1311be6 | 2021-03-08 22:27:57 +0100 | [diff] [blame] | 4 | .. image:: https://img.shields.io/pypi/v/cachetools |
Thomas Kemmer | aa936f0 | 2019-12-15 20:46:13 +0100 | [diff] [blame] | 5 | :target: https://pypi.org/project/cachetools/ |
| 6 | :alt: Latest PyPI version |
| 7 | |
| 8 | .. image:: https://img.shields.io/readthedocs/cachetools |
Thomas Kemmer | 1311be6 | 2021-03-08 22:27:57 +0100 | [diff] [blame] | 9 | :target: https://cachetools.readthedocs.io/ |
Thomas Kemmer | aa936f0 | 2019-12-15 20:46:13 +0100 | [diff] [blame] | 10 | :alt: Documentation build status |
| 11 | |
Thomas Kemmer | fc47761 | 2021-08-09 21:57:40 +0200 | [diff] [blame^] | 12 | .. 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 Kemmer | aa936f0 | 2019-12-15 20:46:13 +0100 | [diff] [blame] | 15 | |
Thomas Kemmer | fc47761 | 2021-08-09 21:57:40 +0200 | [diff] [blame^] | 16 | .. image:: https://img.shields.io/codecov/c/github/tkem/cachetools/master.svg |
| 17 | :target: https://codecov.io/gh/tkem/cachetools |
Thomas Kemmer | aa936f0 | 2019-12-15 20:46:13 +0100 | [diff] [blame] | 18 | :alt: Test coverage |
| 19 | |
| 20 | .. image:: https://img.shields.io/github/license/tkem/cachetools |
Thomas Kemmer | 1311be6 | 2021-03-08 22:27:57 +0100 | [diff] [blame] | 21 | :target: https://raw.github.com/tkem/cachetools/master/LICENSE |
Thomas Kemmer | aa936f0 | 2019-12-15 20:46:13 +0100 | [diff] [blame] | 22 | :alt: License |
| 23 | |
Thomas Kemmer | 187b13a | 2021-04-27 07:12:43 +0200 | [diff] [blame] | 24 | .. 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 Kemmer | bf206e0 | 2014-05-06 20:26:34 +0200 | [diff] [blame] | 28 | This module provides various memoizing collections and decorators, |
Thomas Kemmer | aa936f0 | 2019-12-15 20:46:13 +0100 | [diff] [blame] | 29 | including variants of the Python Standard Library's `@lru_cache`_ |
Thomas Kemmer | 5385445 | 2014-11-12 07:58:59 +0100 | [diff] [blame] | 30 | function decorator. |
Thomas Kemmer | fa0e606 | 2014-03-22 11:09:33 +0100 | [diff] [blame] | 31 | |
Thomas Kemmer | eac1c25 | 2018-11-04 16:11:55 +0100 | [diff] [blame] | 32 | .. code-block:: python |
Thomas Kemmer | fa0e606 | 2014-03-22 11:09:33 +0100 | [diff] [blame] | 33 | |
Thomas Kemmer | eac1c25 | 2018-11-04 16:11:55 +0100 | [diff] [blame] | 34 | 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 Kemmer | 35f85f2 | 2014-03-25 06:44:31 +0100 | [diff] [blame] | 52 | |
Thomas Kemmer | b210c14 | 2014-06-16 20:22:12 +0200 | [diff] [blame] | 53 | For the purpose of this module, a *cache* is a mutable_ mapping_ of a |
Thomas Kemmer | 886f48e | 2014-12-19 18:30:57 +0100 | [diff] [blame] | 54 | fixed maximum size. When the cache is full, i.e. by adding another |
| 55 | item the cache would exceed its maximum size, the cache must choose |
| 56 | which item(s) to discard based on a suitable `cache algorithm`_. In |
| 57 | general, a cache's size is the total size of its items, and an item's |
| 58 | size 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 Kemmer | b6bc350 | 2015-08-28 16:30:38 +0200 | [diff] [blame] | 60 | item counts as ``1``, a cache's size is equal to the number of its |
| 61 | items, or ``len(cache)``. |
Thomas Kemmer | 35f85f2 | 2014-03-25 06:44:31 +0100 | [diff] [blame] | 62 | |
Thomas Kemmer | 5c7effc | 2015-09-07 20:21:56 +0200 | [diff] [blame] | 63 | Multiple cache classes based on different caching algorithms are |
| 64 | implemented, and decorators for easily memoizing function and method |
| 65 | calls are provided, too. |
Thomas Kemmer | fa0e606 | 2014-03-22 11:09:33 +0100 | [diff] [blame] | 66 | |
| 67 | |
| 68 | Installation |
| 69 | ------------------------------------------------------------------------ |
| 70 | |
Thomas Kemmer | aa936f0 | 2019-12-15 20:46:13 +0100 | [diff] [blame] | 71 | cachetools is available from PyPI_ and can be installed by running:: |
Thomas Kemmer | fa0e606 | 2014-03-22 11:09:33 +0100 | [diff] [blame] | 72 | |
Thomas Kemmer | aa936f0 | 2019-12-15 20:46:13 +0100 | [diff] [blame] | 73 | pip install cachetools |
Thomas Kemmer | fa0e606 | 2014-03-22 11:09:33 +0100 | [diff] [blame] | 74 | |
| 75 | |
| 76 | Project Resources |
| 77 | ------------------------------------------------------------------------ |
| 78 | |
Thomas Kemmer | aa936f0 | 2019-12-15 20:46:13 +0100 | [diff] [blame] | 79 | - `Documentation`_ |
| 80 | - `Issue tracker`_ |
| 81 | - `Source code`_ |
| 82 | - `Change log`_ |
Thomas Kemmer | fa0e606 | 2014-03-22 11:09:33 +0100 | [diff] [blame] | 83 | |
Thomas Kemmer | a34b1fa | 2014-03-22 12:57:39 +0100 | [diff] [blame] | 84 | |
| 85 | License |
| 86 | ------------------------------------------------------------------------ |
| 87 | |
Thomas Kemmer | 1d99e16 | 2021-01-24 20:49:22 +0100 | [diff] [blame] | 88 | Copyright (c) 2014-2021 Thomas Kemmer. |
Thomas Kemmer | a34b1fa | 2014-03-22 12:57:39 +0100 | [diff] [blame] | 89 | |
| 90 | Licensed under the `MIT License`_. |
| 91 | |
| 92 | |
Thomas Kemmer | 1311be6 | 2021-03-08 22:27:57 +0100 | [diff] [blame] | 93 | .. _@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 Kemmer | a34b1fa | 2014-03-22 12:57:39 +0100 | [diff] [blame] | 97 | |
Thomas Kemmer | aa936f0 | 2019-12-15 20:46:13 +0100 | [diff] [blame] | 98 | .. _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 Kemmer | 1311be6 | 2021-03-08 22:27:57 +0100 | [diff] [blame] | 103 | .. _MIT License: https://raw.github.com/tkem/cachetools/master/LICENSE |