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 | |
| 38 | |
| 39 | Installation |
| 40 | ------------------------------------------------------------------------ |
| 41 | |
| 42 | Install cachetools using pip:: |
| 43 | |
| 44 | pip install cachetools |
| 45 | |
| 46 | |
| 47 | Project Resources |
| 48 | ------------------------------------------------------------------------ |
| 49 | |
Thomas Kemmer | 2f95610 | 2014-10-12 15:03:51 +0200 | [diff] [blame] | 50 | .. image:: http://img.shields.io/pypi/v/cachetools.svg?style=flat |
Thomas Kemmer | 4649656 | 2014-10-23 18:44:58 +0200 | [diff] [blame] | 51 | :target: https://pypi.python.org/pypi/cachetools/ |
| 52 | :alt: Latest PyPI version |
Thomas Kemmer | bf206e0 | 2014-05-06 20:26:34 +0200 | [diff] [blame] | 53 | |
Thomas Kemmer | c22a043 | 2014-11-04 20:04:22 +0100 | [diff] [blame] | 54 | .. image:: http://img.shields.io/travis/tkem/cachetools/master.svg?style=flat |
Thomas Kemmer | 4649656 | 2014-10-23 18:44:58 +0200 | [diff] [blame] | 55 | :target: https://travis-ci.org/tkem/cachetools/ |
| 56 | :alt: Travis CI build status |
| 57 | |
Thomas Kemmer | c22a043 | 2014-11-04 20:04:22 +0100 | [diff] [blame] | 58 | .. image:: http://img.shields.io/coveralls/tkem/cachetools/master.svg?style=flat |
Thomas Kemmer | 4649656 | 2014-10-23 18:44:58 +0200 | [diff] [blame] | 59 | :target: https://coveralls.io/r/tkem/cachetools |
| 60 | :alt: Test coverage |
Thomas Kemmer | 2f95610 | 2014-10-12 15:03:51 +0200 | [diff] [blame] | 61 | |
Thomas Kemmer | a34b1fa | 2014-03-22 12:57:39 +0100 | [diff] [blame] | 62 | - `Documentation`_ |
| 63 | - `Issue Tracker`_ |
| 64 | - `Source Code`_ |
| 65 | - `Change Log`_ |
Thomas Kemmer | fa0e606 | 2014-03-22 11:09:33 +0100 | [diff] [blame] | 66 | |
Thomas Kemmer | a34b1fa | 2014-03-22 12:57:39 +0100 | [diff] [blame] | 67 | |
| 68 | License |
| 69 | ------------------------------------------------------------------------ |
| 70 | |
Thomas Kemmer | d57c8a3 | 2017-08-11 18:09:00 +0200 | [diff] [blame^] | 71 | Copyright (c) 2014-2017 Thomas Kemmer. |
Thomas Kemmer | a34b1fa | 2014-03-22 12:57:39 +0100 | [diff] [blame] | 72 | |
| 73 | Licensed under the `MIT License`_. |
| 74 | |
| 75 | |
Thomas Kemmer | 5385445 | 2014-11-12 07:58:59 +0100 | [diff] [blame] | 76 | .. _@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] | 77 | .. _mutable: http://docs.python.org/dev/glossary.html#term-mutable |
Thomas Kemmer | 35f85f2 | 2014-03-25 06:44:31 +0100 | [diff] [blame] | 78 | .. _mapping: http://docs.python.org/dev/glossary.html#term-mapping |
| 79 | .. _cache algorithm: http://en.wikipedia.org/wiki/Cache_algorithms |
Thomas Kemmer | a34b1fa | 2014-03-22 12:57:39 +0100 | [diff] [blame] | 80 | |
| 81 | .. _Documentation: http://pythonhosted.org/cachetools/ |
Thomas Kemmer | a34b1fa | 2014-03-22 12:57:39 +0100 | [diff] [blame] | 82 | .. _Issue Tracker: https://github.com/tkem/cachetools/issues/ |
Thomas Kemmer | 78408a1 | 2014-09-18 05:32:16 +0200 | [diff] [blame] | 83 | .. _Source Code: https://github.com/tkem/cachetools/ |
Thomas Kemmer | 101513c | 2014-11-05 19:56:17 +0100 | [diff] [blame] | 84 | .. _Change Log: https://github.com/tkem/cachetools/blob/master/CHANGES.rst |
Thomas Kemmer | 3b9d819 | 2014-03-27 06:23:04 +0100 | [diff] [blame] | 85 | .. _MIT License: http://raw.github.com/tkem/cachetools/master/LICENSE |