Thomas Kemmer | fa0e606 | 2014-03-22 11:09:33 +0100 | [diff] [blame] | 1 | cachetools |
| 2 | ======================================================================== |
| 3 | |
Thomas Kemmer | bf206e0 | 2014-05-06 20:26:34 +0200 | [diff] [blame] | 4 | This module provides various memoizing collections and decorators, |
Thomas Kemmer | b6bc350 | 2015-08-28 16:30:38 +0200 | [diff] [blame] | 5 | including variants of the Python 3 Standard Library `@lru_cache`_ |
Thomas Kemmer | 5385445 | 2014-11-12 07:58:59 +0100 | [diff] [blame] | 6 | function decorator. |
Thomas Kemmer | fa0e606 | 2014-03-22 11:09:33 +0100 | [diff] [blame] | 7 | |
| 8 | .. code-block:: pycon |
| 9 | |
Thomas Kemmer | 35f85f2 | 2014-03-25 06:44:31 +0100 | [diff] [blame] | 10 | >>> from cachetools import LRUCache |
| 11 | >>> cache = LRUCache(maxsize=2) |
Thomas Kemmer | b210c14 | 2014-06-16 20:22:12 +0200 | [diff] [blame] | 12 | >>> cache.update([('first', 1), ('second', 2)]) |
Thomas Kemmer | 35f85f2 | 2014-03-25 06:44:31 +0100 | [diff] [blame] | 13 | >>> cache |
Thomas Kemmer | b210c14 | 2014-06-16 20:22:12 +0200 | [diff] [blame] | 14 | LRUCache([('second', 2), ('first', 1)], maxsize=2, currsize=2) |
Thomas Kemmer | 35f85f2 | 2014-03-25 06:44:31 +0100 | [diff] [blame] | 15 | >>> cache['third'] = 3 |
| 16 | >>> cache |
Thomas Kemmer | b210c14 | 2014-06-16 20:22:12 +0200 | [diff] [blame] | 17 | LRUCache([('second', 2), ('third', 3)], maxsize=2, currsize=2) |
Thomas Kemmer | 35f85f2 | 2014-03-25 06:44:31 +0100 | [diff] [blame] | 18 | >>> cache['second'] |
| 19 | 2 |
Thomas Kemmer | 35f85f2 | 2014-03-25 06:44:31 +0100 | [diff] [blame] | 20 | >>> cache['fourth'] = 4 |
Thomas Kemmer | 8dd2764 | 2014-11-11 06:43:58 +0100 | [diff] [blame] | 21 | >>> cache |
Thomas Kemmer | b210c14 | 2014-06-16 20:22:12 +0200 | [diff] [blame] | 22 | LRUCache([('second', 2), ('fourth', 4)], maxsize=2, currsize=2) |
Thomas Kemmer | 35f85f2 | 2014-03-25 06:44:31 +0100 | [diff] [blame] | 23 | |
Thomas Kemmer | b210c14 | 2014-06-16 20:22:12 +0200 | [diff] [blame] | 24 | For the purpose of this module, a *cache* is a mutable_ mapping_ of a |
Thomas Kemmer | 886f48e | 2014-12-19 18:30:57 +0100 | [diff] [blame] | 25 | fixed maximum size. When the cache is full, i.e. by adding another |
| 26 | item the cache would exceed its maximum size, the cache must choose |
| 27 | which item(s) to discard based on a suitable `cache algorithm`_. In |
| 28 | general, a cache's size is the total size of its items, and an item's |
| 29 | size 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 Kemmer | b6bc350 | 2015-08-28 16:30:38 +0200 | [diff] [blame] | 31 | item counts as ``1``, a cache's size is equal to the number of its |
| 32 | items, or ``len(cache)``. |
Thomas Kemmer | 35f85f2 | 2014-03-25 06:44:31 +0100 | [diff] [blame] | 33 | |
Thomas Kemmer | 5c7effc | 2015-09-07 20:21:56 +0200 | [diff] [blame] | 34 | Multiple cache classes based on different caching algorithms are |
| 35 | implemented, and decorators for easily memoizing function and method |
| 36 | calls are provided, too. |
Thomas Kemmer | fa0e606 | 2014-03-22 11:09:33 +0100 | [diff] [blame] | 37 | |
Thomas Kemmer | 13ba795 | 2017-08-11 18:35:58 +0200 | [diff] [blame^] | 38 | For more information, please refer to the online documentation_. |
Thomas Kemmer | d5b299c | 2017-08-11 18:26:50 +0200 | [diff] [blame] | 39 | |
Thomas Kemmer | fa0e606 | 2014-03-22 11:09:33 +0100 | [diff] [blame] | 40 | |
| 41 | Installation |
| 42 | ------------------------------------------------------------------------ |
| 43 | |
| 44 | Install cachetools using pip:: |
| 45 | |
| 46 | pip install cachetools |
| 47 | |
| 48 | |
| 49 | Project Resources |
| 50 | ------------------------------------------------------------------------ |
| 51 | |
Thomas Kemmer | 2f95610 | 2014-10-12 15:03:51 +0200 | [diff] [blame] | 52 | .. image:: http://img.shields.io/pypi/v/cachetools.svg?style=flat |
Thomas Kemmer | 4649656 | 2014-10-23 18:44:58 +0200 | [diff] [blame] | 53 | :target: https://pypi.python.org/pypi/cachetools/ |
| 54 | :alt: Latest PyPI version |
Thomas Kemmer | bf206e0 | 2014-05-06 20:26:34 +0200 | [diff] [blame] | 55 | |
Thomas Kemmer | c22a043 | 2014-11-04 20:04:22 +0100 | [diff] [blame] | 56 | .. image:: http://img.shields.io/travis/tkem/cachetools/master.svg?style=flat |
Thomas Kemmer | 4649656 | 2014-10-23 18:44:58 +0200 | [diff] [blame] | 57 | :target: https://travis-ci.org/tkem/cachetools/ |
| 58 | :alt: Travis CI build status |
| 59 | |
Thomas Kemmer | c22a043 | 2014-11-04 20:04:22 +0100 | [diff] [blame] | 60 | .. image:: http://img.shields.io/coveralls/tkem/cachetools/master.svg?style=flat |
Thomas Kemmer | 4649656 | 2014-10-23 18:44:58 +0200 | [diff] [blame] | 61 | :target: https://coveralls.io/r/tkem/cachetools |
| 62 | :alt: Test coverage |
Thomas Kemmer | 2f95610 | 2014-10-12 15:03:51 +0200 | [diff] [blame] | 63 | |
Thomas Kemmer | d5b299c | 2017-08-11 18:26:50 +0200 | [diff] [blame] | 64 | .. 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 Kemmer | a34b1fa | 2014-03-22 12:57:39 +0100 | [diff] [blame] | 68 | - `Issue Tracker`_ |
| 69 | - `Source Code`_ |
| 70 | - `Change Log`_ |
Thomas Kemmer | fa0e606 | 2014-03-22 11:09:33 +0100 | [diff] [blame] | 71 | |
Thomas Kemmer | a34b1fa | 2014-03-22 12:57:39 +0100 | [diff] [blame] | 72 | |
| 73 | License |
| 74 | ------------------------------------------------------------------------ |
| 75 | |
Thomas Kemmer | d57c8a3 | 2017-08-11 18:09:00 +0200 | [diff] [blame] | 76 | Copyright (c) 2014-2017 Thomas Kemmer. |
Thomas Kemmer | a34b1fa | 2014-03-22 12:57:39 +0100 | [diff] [blame] | 77 | |
| 78 | Licensed under the `MIT License`_. |
| 79 | |
| 80 | |
Thomas Kemmer | 5385445 | 2014-11-12 07:58:59 +0100 | [diff] [blame] | 81 | .. _@lru_cache: http://docs.python.org/3/library/functools.html#functools.lru_cache |
Thomas Kemmer | bf206e0 | 2014-05-06 20:26:34 +0200 | [diff] [blame] | 82 | .. _mutable: http://docs.python.org/dev/glossary.html#term-mutable |
Thomas Kemmer | 35f85f2 | 2014-03-25 06:44:31 +0100 | [diff] [blame] | 83 | .. _mapping: http://docs.python.org/dev/glossary.html#term-mapping |
| 84 | .. _cache algorithm: http://en.wikipedia.org/wiki/Cache_algorithms |
Thomas Kemmer | a34b1fa | 2014-03-22 12:57:39 +0100 | [diff] [blame] | 85 | |
Thomas Kemmer | d5b299c | 2017-08-11 18:26:50 +0200 | [diff] [blame] | 86 | .. _Documentation: http://cachetools.readthedocs.io/en/latest/ |
Thomas Kemmer | a34b1fa | 2014-03-22 12:57:39 +0100 | [diff] [blame] | 87 | .. _Issue Tracker: https://github.com/tkem/cachetools/issues/ |
Thomas Kemmer | 78408a1 | 2014-09-18 05:32:16 +0200 | [diff] [blame] | 88 | .. _Source Code: https://github.com/tkem/cachetools/ |
Thomas Kemmer | 101513c | 2014-11-05 19:56:17 +0100 | [diff] [blame] | 89 | .. _Change Log: https://github.com/tkem/cachetools/blob/master/CHANGES.rst |
Thomas Kemmer | 3b9d819 | 2014-03-27 06:23:04 +0100 | [diff] [blame] | 90 | .. _MIT License: http://raw.github.com/tkem/cachetools/master/LICENSE |