README.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. cachetools
  2. ========================================================================
  3. .. image:: https://img.shields.io/pypi/v/cachetools
  4. :target: https://pypi.org/project/cachetools/
  5. :alt: Latest PyPI version
  6. .. image:: https://img.shields.io/github/actions/workflow/status/tkem/cachetools/ci.yml
  7. :target: https://github.com/tkem/cachetools/actions/workflows/ci.yml
  8. :alt: CI build status
  9. .. image:: https://img.shields.io/readthedocs/cachetools
  10. :target: https://cachetools.readthedocs.io/
  11. :alt: Documentation build status
  12. .. image:: https://img.shields.io/codecov/c/github/tkem/cachetools/master.svg
  13. :target: https://codecov.io/gh/tkem/cachetools
  14. :alt: Test coverage
  15. .. image:: https://img.shields.io/librariesio/sourcerank/pypi/cachetools
  16. :target: https://libraries.io/pypi/cachetools
  17. :alt: Libraries.io SourceRank
  18. .. image:: https://img.shields.io/github/license/tkem/cachetools
  19. :target: https://raw.github.com/tkem/cachetools/master/LICENSE
  20. :alt: License
  21. .. image:: https://img.shields.io/badge/code%20style-black-000000.svg
  22. :target: https://github.com/psf/black
  23. :alt: Code style: black
  24. This module provides various memoizing collections and decorators,
  25. including variants of the Python Standard Library's `@lru_cache`_
  26. function decorator.
  27. .. code-block:: python
  28. from cachetools import cached, LRUCache, TTLCache
  29. # speed up calculating Fibonacci numbers with dynamic programming
  30. @cached(cache={})
  31. def fib(n):
  32. return n if n < 2 else fib(n - 1) + fib(n - 2)
  33. # cache least recently used Python Enhancement Proposals
  34. @cached(cache=LRUCache(maxsize=32))
  35. def get_pep(num):
  36. url = 'http://www.python.org/dev/peps/pep-%04d/' % num
  37. with urllib.request.urlopen(url) as s:
  38. return s.read()
  39. # cache weather data for no longer than ten minutes
  40. @cached(cache=TTLCache(maxsize=1024, ttl=600))
  41. def get_weather(place):
  42. return owm.weather_at_place(place).get_weather()
  43. For the purpose of this module, a *cache* is a mutable_ mapping_ of a
  44. fixed maximum size. When the cache is full, i.e. by adding another
  45. item the cache would exceed its maximum size, the cache must choose
  46. which item(s) to discard based on a suitable `cache algorithm`_.
  47. This module provides multiple cache classes based on different cache
  48. algorithms, as well as decorators for easily memoizing function and
  49. method calls.
  50. Installation
  51. ------------------------------------------------------------------------
  52. cachetools is available from PyPI_ and can be installed by running::
  53. pip install cachetools
  54. Typing stubs for this package are provided by typeshed_ and can be
  55. installed by running::
  56. pip install types-cachetools
  57. Project Resources
  58. ------------------------------------------------------------------------
  59. - `Documentation`_
  60. - `Issue tracker`_
  61. - `Source code`_
  62. - `Change log`_
  63. Related Projects
  64. ------------------------------------------------------------------------
  65. - asyncache_: Helpers to use cachetools with async functions
  66. - cacheing_: Pure Python Cacheing Library
  67. - CacheToolsUtils_: Cachetools Utilities
  68. - kids.cache_: Kids caching library
  69. - shelved-cache_: Persistent cache for Python cachetools
  70. License
  71. ------------------------------------------------------------------------
  72. Copyright (c) 2014-2024 Thomas Kemmer.
  73. Licensed under the `MIT License`_.
  74. .. _@lru_cache: https://docs.python.org/3/library/functools.html#functools.lru_cache
  75. .. _mutable: https://docs.python.org/dev/glossary.html#term-mutable
  76. .. _mapping: https://docs.python.org/dev/glossary.html#term-mapping
  77. .. _cache algorithm: https://en.wikipedia.org/wiki/Cache_algorithms
  78. .. _PyPI: https://pypi.org/project/cachetools/
  79. .. _typeshed: https://github.com/python/typeshed/
  80. .. _Documentation: https://cachetools.readthedocs.io/
  81. .. _Issue tracker: https://github.com/tkem/cachetools/issues/
  82. .. _Source code: https://github.com/tkem/cachetools/
  83. .. _Change log: https://github.com/tkem/cachetools/blob/master/CHANGELOG.rst
  84. .. _MIT License: https://raw.github.com/tkem/cachetools/master/LICENSE
  85. .. _asyncache: https://pypi.org/project/asyncache/
  86. .. _cacheing: https://github.com/breid48/cacheing
  87. .. _CacheToolsUtils: https://pypi.org/project/CacheToolsUtils/
  88. .. _kids.cache: https://pypi.org/project/kids.cache/
  89. .. _shelved-cache: https://pypi.org/project/shelved-cache/