README.rst 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. cachetools
  2. ========================================================================
  3. This module provides various memoizing collections and decorators,
  4. including variants of the Python 3 Standard Library `@lru_cache`_
  5. function decorator.
  6. .. code-block:: python
  7. from cachetools import cached, LRUCache, TTLCache
  8. # speed up calculating Fibonacci numbers with dynamic programming
  9. @cached(cache={})
  10. def fib(n):
  11. return n if n < 2 else fib(n - 1) + fib(n - 2)
  12. # cache least recently used Python Enhancement Proposals
  13. @cached(cache=LRUCache(maxsize=32))
  14. def get_pep(num):
  15. url = 'http://www.python.org/dev/peps/pep-%04d/' % num
  16. with urllib.request.urlopen(url) as s:
  17. return s.read()
  18. # cache weather data for no longer than ten minutes
  19. @cached(cache=TTLCache(maxsize=1024, ttl=600))
  20. def get_weather(place):
  21. return owm.weather_at_place(place).get_weather()
  22. For the purpose of this module, a *cache* is a mutable_ mapping_ of a
  23. fixed maximum size. When the cache is full, i.e. by adding another
  24. item the cache would exceed its maximum size, the cache must choose
  25. which item(s) to discard based on a suitable `cache algorithm`_. In
  26. general, a cache's size is the total size of its items, and an item's
  27. size is a property or function of its value, e.g. the result of
  28. ``sys.getsizeof(value)``. For the trivial but common case that each
  29. item counts as ``1``, a cache's size is equal to the number of its
  30. items, or ``len(cache)``.
  31. Multiple cache classes based on different caching algorithms are
  32. implemented, and decorators for easily memoizing function and method
  33. calls are provided, too.
  34. For more information, please refer to the online documentation_.
  35. Installation
  36. ------------------------------------------------------------------------
  37. Install cachetools using pip::
  38. pip install cachetools
  39. Project Resources
  40. ------------------------------------------------------------------------
  41. .. image:: http://img.shields.io/pypi/v/cachetools.svg?style=flat
  42. :target: https://pypi.python.org/pypi/cachetools/
  43. :alt: Latest PyPI version
  44. .. image:: http://img.shields.io/travis/tkem/cachetools/master.svg?style=flat
  45. :target: https://travis-ci.org/tkem/cachetools/
  46. :alt: Travis CI build status
  47. .. image:: http://img.shields.io/coveralls/tkem/cachetools/master.svg?style=flat
  48. :target: https://coveralls.io/r/tkem/cachetools
  49. :alt: Test coverage
  50. .. image:: https://readthedocs.org/projects/cachetools/badge/?version=latest&style=flat
  51. :target: http://cachetools.readthedocs.io/en/latest/
  52. :alt: Documentation Status
  53. - `Issue Tracker`_
  54. - `Source Code`_
  55. - `Change Log`_
  56. License
  57. ------------------------------------------------------------------------
  58. Copyright (c) 2014-2019 Thomas Kemmer.
  59. Licensed under the `MIT License`_.
  60. .. _@lru_cache: http://docs.python.org/3/library/functools.html#functools.lru_cache
  61. .. _mutable: http://docs.python.org/dev/glossary.html#term-mutable
  62. .. _mapping: http://docs.python.org/dev/glossary.html#term-mapping
  63. .. _cache algorithm: http://en.wikipedia.org/wiki/Cache_algorithms
  64. .. _Documentation: http://cachetools.readthedocs.io/en/latest/
  65. .. _Issue Tracker: https://github.com/tkem/cachetools/issues/
  66. .. _Source Code: https://github.com/tkem/cachetools/
  67. .. _Change Log: https://github.com/tkem/cachetools/blob/master/CHANGES.rst
  68. .. _MIT License: http://raw.github.com/tkem/cachetools/master/LICENSE