blob: af046ed7be72951c19687887a97ea211c1adf014 [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
6`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
25fixed maximum *size*. When the cache is full, i.e. the current size
26of the cache exceeds its maximum size, the cache must choose which
27item(s) to discard based on a suitable `cache algorithm`_.
Thomas Kemmerbf206e02014-05-06 20:26:34 +020028
Thomas Kemmerb210c142014-06-16 20:22:12 +020029In general, a cache's size is the sum of the size of its items. If
30the size of each items is :const:`1`, a cache's size is equal to the
31number of its items, i.e. :func:`len`. An items's size may also be a
32property or function of its value, e.g. the result of
33:func:`sys.getsizeof`, or :func:`len` for string and sequence values.
Thomas Kemmer35f85f22014-03-25 06:44:31 +010034
35This module provides various cache implementations based on different
36cache algorithms, as well as decorators for easily memoizing function
Thomas Kemmerbf206e02014-05-06 20:26:34 +020037and method calls.
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010038
39
40Installation
41------------------------------------------------------------------------
42
43Install cachetools using pip::
44
45 pip install cachetools
46
47
48Project Resources
49------------------------------------------------------------------------
50
Thomas Kemmerbf206e02014-05-06 20:26:34 +020051.. image:: http://img.shields.io/pypi/v/cachetools.svg
52 :target: https://pypi.python.org/pypi/cachetools/
53 :alt: Latest PyPI version
54
55.. image:: http://img.shields.io/pypi/dm/cachetools.svg
56 :target: https://pypi.python.org/pypi/cachetools/
57 :alt: Number of PyPI downloads
58
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010059- `Documentation`_
60- `Issue Tracker`_
61- `Source Code`_
62- `Change Log`_
Thomas Kemmerfa0e6062014-03-22 11:09:33 +010063
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010064
65License
66------------------------------------------------------------------------
67
Thomas Kemmer573b3182014-04-02 20:44:06 +020068Copyright (c) 2014 Thomas Kemmer.
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010069
70Licensed under the `MIT License`_.
71
72
Thomas Kemmer35f85f22014-03-25 06:44:31 +010073.. _functools.lru_cache: http://docs.python.org/3.4/library/functools.html#functools.lru_cache
Thomas Kemmerbf206e02014-05-06 20:26:34 +020074.. _mutable: http://docs.python.org/dev/glossary.html#term-mutable
Thomas Kemmer35f85f22014-03-25 06:44:31 +010075.. _mapping: http://docs.python.org/dev/glossary.html#term-mapping
76.. _cache algorithm: http://en.wikipedia.org/wiki/Cache_algorithms
Thomas Kemmera34b1fa2014-03-22 12:57:39 +010077
78.. _Documentation: http://pythonhosted.org/cachetools/
79.. _Source Code: https://github.com/tkem/cachetools/
80.. _Issue Tracker: https://github.com/tkem/cachetools/issues/
Thomas Kemmer35f85f22014-03-25 06:44:31 +010081.. _Change Log: http://raw.github.com/tkem/cachetools/master/Changes
Thomas Kemmer3b9d8192014-03-27 06:23:04 +010082.. _MIT License: http://raw.github.com/tkem/cachetools/master/LICENSE