Skip to main content

Extensible memoizing collections and decorators

Project description

This module provides various memoizing collections and decorators, including variants of the Python 3 Standard Library @lru_cache function decorator.

from cachetools import cached, LRUCache, TTLCache

# speed up calculating Fibonacci numbers with dynamic programming
@cached(cache={})
def fib(n):
    return n if n < 2 else fib(n - 1) + fib(n - 2)

# cache least recently used Python Enhancement Proposals
@cached(cache=LRUCache(maxsize=32))
def get_pep(num):
    url = 'http://www.python.org/dev/peps/pep-%04d/' % num
    with urllib.request.urlopen(url) as s:
        return s.read()

# cache weather data for no longer than ten minutes
@cached(cache=TTLCache(maxsize=1024, ttl=600))
def get_weather(place):
    return owm.weather_at_place(place).get_weather()

For the purpose of this module, a cache is a mutable mapping 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. In general, a cache’s size is the total size of its items, and an item’s size is a property or function of its value, e.g. the result of sys.getsizeof(value). For the trivial but common case that each item counts as 1, a cache’s size is equal to the number of its items, or len(cache).

Multiple cache classes based on different caching algorithms are implemented, and decorators for easily memoizing function and method calls are provided, too.

For more information, please refer to the online documentation.

Installation

Install cachetools using pip:

pip install cachetools

Project Resources

Latest PyPI version Travis CI build status Test coverage Documentation Status

License

Copyright (c) 2014-2018 Thomas Kemmer.

Licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cachetools-3.0.0.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cachetools-3.0.0-py2.py3-none-any.whl (12.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file cachetools-3.0.0.tar.gz.

File metadata

  • Download URL: cachetools-3.0.0.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/3.6.6

File hashes

Hashes for cachetools-3.0.0.tar.gz
Algorithm Hash digest
SHA256 4621965b0d9d4c82a79a29edbad19946f5e7702df4afae7d1ed2df951559a8cc
MD5 72f72cd6493614226a5ed18e71cadc01
BLAKE2b-256 e6287cde8e73835ff48b4f35b2d93a509575f7bc02b7d614ada71b820c8d9233

See more details on using hashes here.

File details

Details for the file cachetools-3.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: cachetools-3.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/3.6.6

File hashes

Hashes for cachetools-3.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0a258d82933a1dd18cb540aca4ac5d5690731e24d1239a08577b814998f49785
MD5 20f5e88f2236b8d6329d89b79a2a79d1
BLAKE2b-256 767e08cd3846bebeabb6b1cfc4af8aae649d90249b4aeed080bddb5297f1d73b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page