blob: c6cbf022abf5a669ef843cee674ebfb07b685ce1 [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
38
39Installation
40------------------------------------------------------------------------
41
42Install cachetools using pip::
43
44 pip install cachetools
45
46
47Project Resources
48------------------------------------------------------------------------
49
Thomas Kemmer2f956102014-10-12 15:03:51 +020050.. image:: http://img.shields.io/pypi/v/cachetools.svg?style=flat
Thomas Kemmer46496562014-10-23 18:44:58 +020051 :target: https://pypi.python.org/pypi/cachetools/
52 :alt: Latest PyPI version
Thomas Kemmerbf206e02014-05-06 20:26:34 +020053
Thomas Kemmerc22a0432014-11-04 20:04:22 +010054.. image:: http://img.shields.io/travis/tkem/cachetools/master.svg?style=flat
Thomas Kemmer46496562014-10-23 18:44:58 +020055 :target: https://travis-ci.org/tkem/cachetools/
56 :alt: Travis CI build status
57
Thomas Kemmerc22a0432014-11-04 20:04:22 +010058.. image:: http://img.shields.io/coveralls/tkem/cachetools/master.svg?style=flat
Thomas Kemmer46496562014-10-23 18:44:58 +020059 :target: https://coveralls.io/r/tkem/cachetools
60 :alt: Test coverage
Thomas Kemmer2f956102014-10-12 15:03:51 +020061
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010062- `Documentation`_
63- `Issue Tracker`_
64- `Source Code`_
65- `Change Log`_
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010066
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010067
68License
69------------------------------------------------------------------------
70
Thomas Kemmerd57c8a32017-08-11 18:09:00 +020071Copyright (c) 2014-2017 Thomas Kemmer.
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010072
73Licensed under the `MIT License`_.
74
75
Thomas Kemmer53854452014-11-12 07:58:59 +010076.. _@lru_cache: http://docs.python.org/3/library/functools.html#functools.lru_cache
Thomas Kemmerbf206e02014-05-06 20:26:34 +020077.. _mutable: http://docs.python.org/dev/glossary.html#term-mutable
Thomas Kemmer35f85f22014-03-25 06:44:31 +010078.. _mapping: http://docs.python.org/dev/glossary.html#term-mapping
79.. _cache algorithm: http://en.wikipedia.org/wiki/Cache_algorithms
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010080
81.. _Documentation: http://pythonhosted.org/cachetools/
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010082.. _Issue Tracker: https://github.com/tkem/cachetools/issues/
Thomas Kemmer78408a12014-09-18 05:32:16 +020083.. _Source Code: https://github.com/tkem/cachetools/
Thomas Kemmer101513c2014-11-05 19:56:17 +010084.. _Change Log: https://github.com/tkem/cachetools/blob/master/CHANGES.rst
Thomas Kemmer3b9d8192014-03-27 06:23:04 +010085.. _MIT License: http://raw.github.com/tkem/cachetools/master/LICENSE