You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This module provides various memoizing collections and decorators,
including variants of the Python Standard Library's @lru_cache
function decorator.
fromcachetoolsimportcached, LRUCache, TTLCache# speed up calculating Fibonacci numbers with dynamic programming@cached(cache={})deffib(n):
returnnifn<2elsefib(n-1) +fib(n-2)
# cache least recently used Python Enhancement Proposals@cached(cache=LRUCache(maxsize=32))defget_pep(num):
url='https://www.python.org/dev/peps/pep-%04d/'%numwithurllib.request.urlopen(url) ass:
returns.read()
# cache weather data for no longer than ten minutes@cached(cache=TTLCache(maxsize=1024, ttl=600))defget_weather(place):
returnowm.weather_at_place(place).get_weather()
For the purpose of this module, a cache is a mutablemapping of a
fixed maximum size. When the cache is full, i.e. by adding another
item the cache would exceed its maximum size, the cache must choose
which item(s) to discard based on a suitable cache algorithm.
This module provides multiple cache classes based on different cache
algorithms, as well as decorators for easily memoizing function and
method calls.
Installation
cachetools is available from PyPI and can be installed by running:
pip install cachetools
Typing stubs for this package are provided by typeshed and can be
installed by running: