blob: b53399c2a6ed5d2363056d4ab0919b7373e45254 [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
8.. code-block:: pycon
9
Thomas Kemmer35f85f22014-03-25 06:44:31 +010010 >>> from cachetools import LRUCache
11 >>> cache = LRUCache(maxsize=2)
Thomas Kemmerb210c142014-06-16 20:22:12 +020012 >>> cache.update([('first', 1), ('second', 2)])
Thomas Kemmer35f85f22014-03-25 06:44:31 +010013 >>> cache
Thomas Kemmerb210c142014-06-16 20:22:12 +020014 LRUCache([('second', 2), ('first', 1)], maxsize=2, currsize=2)
Thomas Kemmer35f85f22014-03-25 06:44:31 +010015 >>> cache['third'] = 3
16 >>> cache
Thomas Kemmerb210c142014-06-16 20:22:12 +020017 LRUCache([('second', 2), ('third', 3)], maxsize=2, currsize=2)
Thomas Kemmer35f85f22014-03-25 06:44:31 +010018 >>> cache['second']
19 2
Thomas Kemmer35f85f22014-03-25 06:44:31 +010020 >>> cache['fourth'] = 4
Thomas Kemmer8dd27642014-11-11 06:43:58 +010021 >>> cache
Thomas Kemmerb210c142014-06-16 20:22:12 +020022 LRUCache([('second', 2), ('fourth', 4)], maxsize=2, currsize=2)
Thomas Kemmer35f85f22014-03-25 06:44:31 +010023
Thomas Kemmerb210c142014-06-16 20:22:12 +020024For the purpose of this module, a *cache* is a mutable_ mapping_ of a
Thomas Kemmer886f48e2014-12-19 18:30:57 +010025fixed maximum size. When the cache is full, i.e. by adding another
26item the cache would exceed its maximum size, the cache must choose
27which item(s) to discard based on a suitable `cache algorithm`_. In
28general, a cache's size is the total size of its items, and an item's
29size is a property or function of its value, e.g. the result of
30``sys.getsizeof(value)``. For the trivial but common case that each
Thomas Kemmerb6bc3502015-08-28 16:30:38 +020031item counts as ``1``, a cache's size is equal to the number of its
32items, or ``len(cache)``.
Thomas Kemmer35f85f22014-03-25 06:44:31 +010033
Thomas Kemmer5c7effc2015-09-07 20:21:56 +020034Multiple cache classes based on different caching algorithms are
35implemented, and decorators for easily memoizing function and method
36calls are provided, too.
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010037
Thomas Kemmer13ba7952017-08-11 18:35:58 +020038For more information, please refer to the online documentation_.
Thomas Kemmerd5b299c2017-08-11 18:26:50 +020039
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010040
41Installation
42------------------------------------------------------------------------
43
44Install cachetools using pip::
45
46 pip install cachetools
47
48
49Project Resources
50------------------------------------------------------------------------
51
Thomas Kemmer2f956102014-10-12 15:03:51 +020052.. image:: http://img.shields.io/pypi/v/cachetools.svg?style=flat
Thomas Kemmer46496562014-10-23 18:44:58 +020053 :target: https://pypi.python.org/pypi/cachetools/
54 :alt: Latest PyPI version
Thomas Kemmerbf206e02014-05-06 20:26:34 +020055
Thomas Kemmerc22a0432014-11-04 20:04:22 +010056.. image:: http://img.shields.io/travis/tkem/cachetools/master.svg?style=flat
Thomas Kemmer46496562014-10-23 18:44:58 +020057 :target: https://travis-ci.org/tkem/cachetools/
58 :alt: Travis CI build status
59
Thomas Kemmerc22a0432014-11-04 20:04:22 +010060.. image:: http://img.shields.io/coveralls/tkem/cachetools/master.svg?style=flat
Thomas Kemmer46496562014-10-23 18:44:58 +020061 :target: https://coveralls.io/r/tkem/cachetools
62 :alt: Test coverage
Thomas Kemmer2f956102014-10-12 15:03:51 +020063
Thomas Kemmerd5b299c2017-08-11 18:26:50 +020064.. image:: https://readthedocs.org/projects/cachetools/badge/?version=latest&style=flat
65 :target: http://cachetools.readthedocs.io/en/latest/
66 :alt: Documentation Status
67
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010068- `Issue Tracker`_
69- `Source Code`_
70- `Change Log`_
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010071
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010072
73License
74------------------------------------------------------------------------
75
Thomas Kemmerd57c8a32017-08-11 18:09:00 +020076Copyright (c) 2014-2017 Thomas Kemmer.
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010077
78Licensed under the `MIT License`_.
79
80
Thomas Kemmer53854452014-11-12 07:58:59 +010081.. _@lru_cache: http://docs.python.org/3/library/functools.html#functools.lru_cache
Thomas Kemmerbf206e02014-05-06 20:26:34 +020082.. _mutable: http://docs.python.org/dev/glossary.html#term-mutable
Thomas Kemmer35f85f22014-03-25 06:44:31 +010083.. _mapping: http://docs.python.org/dev/glossary.html#term-mapping
84.. _cache algorithm: http://en.wikipedia.org/wiki/Cache_algorithms
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010085
Thomas Kemmerd5b299c2017-08-11 18:26:50 +020086.. _Documentation: http://cachetools.readthedocs.io/en/latest/
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010087.. _Issue Tracker: https://github.com/tkem/cachetools/issues/
Thomas Kemmer78408a12014-09-18 05:32:16 +020088.. _Source Code: https://github.com/tkem/cachetools/
Thomas Kemmer101513c2014-11-05 19:56:17 +010089.. _Change Log: https://github.com/tkem/cachetools/blob/master/CHANGES.rst
Thomas Kemmer3b9d8192014-03-27 06:23:04 +010090.. _MIT License: http://raw.github.com/tkem/cachetools/master/LICENSE