blob: 49f669f8e08da3bcc327dbf1f39dde344f11f2b6 [file] [log] [blame]
Thomas Kemmerfa0e6062014-03-22 11:09:33 +01001cachetools
2========================================================================
3
Thomas Kemmerbf206e02014-05-06 20:26:34 +02004This module provides various memoizing collections and decorators,
Thomas Kemmerb6bc3502015-08-28 16:30:38 +02005including variants of the Python 3 Standard Library `@lru_cache`_
Thomas Kemmer53854452014-11-12 07:58:59 +01006function decorator.
Thomas Kemmerfa0e6062014-03-22 11:09:33 +01007
Thomas Kemmereac1c252018-11-04 16:11:55 +01008.. code-block:: python
Thomas Kemmerfa0e6062014-03-22 11:09:33 +01009
Thomas Kemmereac1c252018-11-04 16:11:55 +010010 from cachetools import cached, LRUCache, TTLCache
11
12 # speed up calculating Fibonacci numbers with dynamic programming
13 @cached(cache={})
14 def fib(n):
15 return n if n < 2 else fib(n - 1) + fib(n - 2)
16
17 # cache least recently used Python Enhancement Proposals
18 @cached(cache=LRUCache(maxsize=32))
19 def get_pep(num):
20 url = 'http://www.python.org/dev/peps/pep-%04d/' % num
21 with urllib.request.urlopen(url) as s:
22 return s.read()
23
24 # cache weather data for no longer than ten minutes
25 @cached(cache=TTLCache(maxsize=1024, ttl=600))
26 def get_weather(place):
27 return owm.weather_at_place(place).get_weather()
Thomas Kemmer35f85f22014-03-25 06:44:31 +010028
Thomas Kemmerb210c142014-06-16 20:22:12 +020029For the purpose of this module, a *cache* is a mutable_ mapping_ of a
Thomas Kemmer886f48e2014-12-19 18:30:57 +010030fixed maximum size. When the cache is full, i.e. by adding another
31item the cache would exceed its maximum size, the cache must choose
32which item(s) to discard based on a suitable `cache algorithm`_. In
33general, a cache's size is the total size of its items, and an item's
34size is a property or function of its value, e.g. the result of
35``sys.getsizeof(value)``. For the trivial but common case that each
Thomas Kemmerb6bc3502015-08-28 16:30:38 +020036item counts as ``1``, a cache's size is equal to the number of its
37items, or ``len(cache)``.
Thomas Kemmer35f85f22014-03-25 06:44:31 +010038
Thomas Kemmer5c7effc2015-09-07 20:21:56 +020039Multiple cache classes based on different caching algorithms are
40implemented, and decorators for easily memoizing function and method
41calls are provided, too.
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010042
Thomas Kemmer13ba7952017-08-11 18:35:58 +020043For more information, please refer to the online documentation_.
Thomas Kemmerd5b299c2017-08-11 18:26:50 +020044
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010045
46Installation
47------------------------------------------------------------------------
48
49Install cachetools using pip::
50
51 pip install cachetools
52
53
54Project Resources
55------------------------------------------------------------------------
56
Thomas Kemmer2f956102014-10-12 15:03:51 +020057.. image:: http://img.shields.io/pypi/v/cachetools.svg?style=flat
Thomas Kemmer46496562014-10-23 18:44:58 +020058 :target: https://pypi.python.org/pypi/cachetools/
59 :alt: Latest PyPI version
Thomas Kemmerbf206e02014-05-06 20:26:34 +020060
Thomas Kemmerc22a0432014-11-04 20:04:22 +010061.. image:: http://img.shields.io/travis/tkem/cachetools/master.svg?style=flat
Thomas Kemmer46496562014-10-23 18:44:58 +020062 :target: https://travis-ci.org/tkem/cachetools/
63 :alt: Travis CI build status
64
Thomas Kemmerc22a0432014-11-04 20:04:22 +010065.. image:: http://img.shields.io/coveralls/tkem/cachetools/master.svg?style=flat
Thomas Kemmer46496562014-10-23 18:44:58 +020066 :target: https://coveralls.io/r/tkem/cachetools
67 :alt: Test coverage
Thomas Kemmer2f956102014-10-12 15:03:51 +020068
Thomas Kemmerd5b299c2017-08-11 18:26:50 +020069.. image:: https://readthedocs.org/projects/cachetools/badge/?version=latest&style=flat
70 :target: http://cachetools.readthedocs.io/en/latest/
71 :alt: Documentation Status
72
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010073- `Issue Tracker`_
74- `Source Code`_
75- `Change Log`_
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010076
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010077
78License
79------------------------------------------------------------------------
80
Thomas Kemmer45d9b0d2018-05-12 14:41:46 +020081Copyright (c) 2014-2018 Thomas Kemmer.
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010082
83Licensed under the `MIT License`_.
84
85
Thomas Kemmer53854452014-11-12 07:58:59 +010086.. _@lru_cache: http://docs.python.org/3/library/functools.html#functools.lru_cache
Thomas Kemmerbf206e02014-05-06 20:26:34 +020087.. _mutable: http://docs.python.org/dev/glossary.html#term-mutable
Thomas Kemmer35f85f22014-03-25 06:44:31 +010088.. _mapping: http://docs.python.org/dev/glossary.html#term-mapping
89.. _cache algorithm: http://en.wikipedia.org/wiki/Cache_algorithms
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010090
Thomas Kemmerd5b299c2017-08-11 18:26:50 +020091.. _Documentation: http://cachetools.readthedocs.io/en/latest/
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010092.. _Issue Tracker: https://github.com/tkem/cachetools/issues/
Thomas Kemmer78408a12014-09-18 05:32:16 +020093.. _Source Code: https://github.com/tkem/cachetools/
Thomas Kemmer101513c2014-11-05 19:56:17 +010094.. _Change Log: https://github.com/tkem/cachetools/blob/master/CHANGES.rst
Thomas Kemmer3b9d8192014-03-27 06:23:04 +010095.. _MIT License: http://raw.github.com/tkem/cachetools/master/LICENSE