blob: 3b69c696ebc516d72d13e24491977009e343a878 [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,
5including a variant of the Python 3 Standard Library
Thomas Kemmer8e1d7ed2014-06-16 20:46:19 +02006``functools.lru_cache`` function 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 Kemmerb210c142014-06-16 20:22:12 +020021 LRUCache([('second', 2), ('fourth', 4)], maxsize=2, currsize=2)
Thomas Kemmer35f85f22014-03-25 06:44:31 +010022
Thomas Kemmerbf206e02014-05-06 20:26:34 +020023
Thomas Kemmerb210c142014-06-16 20:22:12 +020024For the purpose of this module, a *cache* is a mutable_ mapping_ of a
Thomas Kemmera8371422014-09-25 18:06:23 +020025fixed maximum size. When the cache is full, i.e. the size of the
26cache would exceed its maximum size, the cache must choose which
27item(s) to discard based on a suitable `cache algorithm`_. A cache's
28size is the sum of the size of its items, and an item's size in
29general is a property or function of its value, e.g. the result of
30``sys.getsizeof``, or ``len`` for string and sequence values.
Thomas Kemmer35f85f22014-03-25 06:44:31 +010031
Thomas Kemmer101513c2014-11-05 19:56:17 +010032This module provides multiple cache implementations based on different
Thomas Kemmer35f85f22014-03-25 06:44:31 +010033cache algorithms, as well as decorators for easily memoizing function
Thomas Kemmerbf206e02014-05-06 20:26:34 +020034and method calls.
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010035
36
37Installation
38------------------------------------------------------------------------
39
40Install cachetools using pip::
41
42 pip install cachetools
43
44
45Project Resources
46------------------------------------------------------------------------
47
Thomas Kemmer2f956102014-10-12 15:03:51 +020048.. image:: http://img.shields.io/pypi/v/cachetools.svg?style=flat
Thomas Kemmer46496562014-10-23 18:44:58 +020049 :target: https://pypi.python.org/pypi/cachetools/
50 :alt: Latest PyPI version
Thomas Kemmerbf206e02014-05-06 20:26:34 +020051
Thomas Kemmer2f956102014-10-12 15:03:51 +020052.. image:: http://img.shields.io/pypi/dm/cachetools.svg?style=flat
Thomas Kemmer46496562014-10-23 18:44:58 +020053 :target: https://pypi.python.org/pypi/cachetools/
54 :alt: Number of PyPI downloads
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 Kemmera34b1fa2014-03-22 12:57:39 +010064- `Documentation`_
65- `Issue Tracker`_
66- `Source Code`_
67- `Change Log`_
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010068
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010069
70License
71------------------------------------------------------------------------
72
Thomas Kemmer573b3182014-04-02 20:44:06 +020073Copyright (c) 2014 Thomas Kemmer.
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010074
75Licensed under the `MIT License`_.
76
77
Thomas Kemmer35f85f22014-03-25 06:44:31 +010078.. _functools.lru_cache: http://docs.python.org/3.4/library/functools.html#functools.lru_cache
Thomas Kemmerbf206e02014-05-06 20:26:34 +020079.. _mutable: http://docs.python.org/dev/glossary.html#term-mutable
Thomas Kemmer35f85f22014-03-25 06:44:31 +010080.. _mapping: http://docs.python.org/dev/glossary.html#term-mapping
81.. _cache algorithm: http://en.wikipedia.org/wiki/Cache_algorithms
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010082
83.. _Documentation: http://pythonhosted.org/cachetools/
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010084.. _Issue Tracker: https://github.com/tkem/cachetools/issues/
Thomas Kemmer78408a12014-09-18 05:32:16 +020085.. _Source Code: https://github.com/tkem/cachetools/
Thomas Kemmer101513c2014-11-05 19:56:17 +010086.. _Change Log: https://github.com/tkem/cachetools/blob/master/CHANGES.rst
Thomas Kemmer3b9d8192014-03-27 06:23:04 +010087.. _MIT License: http://raw.github.com/tkem/cachetools/master/LICENSE