Skip to main content

Provides decorators that are inspired by and work closely with cachetools' for caching asyncio functions and methods.

Project description

cachetools_async

Latest pypi version Test Coverage License Code style: black

This module provides decorators for Python asyncio coroutine functions to support memoization. These are compatible, and can be considered extending the functionality of cachetools.

from cachetools import LRUCache, TTLCache
from cachetools_async import cached

# Dependencies for our examples
import aiohttp
import python_weather

# cache least recently used Python Enhancement Proposals
@cached(cache=LRUCache(maxsize=32))
async def get_pep(num: int):
    pep_url = 'http://www.python.org/dev/peps/pep-%04d/' % num
    async with aiohttp.ClientSession() as session:
        async with session.get(pep_url) as response:
            return await response.text()


# cache weather data for no longer than ten minutes
@cached(cache=TTLCache(maxsize=1024, ttl=600))
async def get_weather(place):
    async with python_weather.Client(unit=python_weather.METRIC) as client:
        return await client.get(place)

This module supports the same definition of a cache as cachetools does - a mutable mapping of a fixed maximum size. The cache itself is not asynchronous, even when the functions that are being cached are.

Note that once you call a function once, subsequent calls before the initial call completes will wait until the first complete. To help understand this, take the example from before. However, let's imagine we need to get weather for multiple locations at once, too.

from cachetools import LRUCache, TTLCache
from cachetools_async import cached
import asyncio
import python_weather

@cached(cache=TTLCache(maxsize=1024, ttl=600))
async def get_weather(place):
    async with python_weather.Client(unit=python_weather.METRIC) as client:
        return await client.get(place)

async def get_multiple_weather(places):
    return await asyncio.gather(
        get_weather(place)
        for place in places
    )

get_multiple_weather([
    "New York",
    "London",
    "London",
    "Miami",
    "New York",
    "Miami",
    "London",
    "New York",
])

Even though they all occur in parallel with 8 weather reports, the cached decorator will ensure only 3 requests for weather would actually be made.

Installation

cachetools_async is available via PyPi and can be installed as such in your package manager of choice.

Poetry

poetry add cachetools_async

Rye

rye add "cachetools_async"

Pip

pip install cachetools_async

License

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_async-0.0.3.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

cachetools_async-0.0.3-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file cachetools_async-0.0.3.tar.gz.

File metadata

  • Download URL: cachetools_async-0.0.3.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.8 Linux/6.8.2-arch1-1

File hashes

Hashes for cachetools_async-0.0.3.tar.gz
Algorithm Hash digest
SHA256 baf5a99ad94925ffd2860b10427dc47b376b65f5a413a4e98eb7bae8125d3a7e
MD5 1a24a38128d4da5413ebdb747643be1c
BLAKE2b-256 a3337f8138beddb1cd45acac3da26a67d07349bd7fa6032dfae90ed3d1c842d0

See more details on using hashes here.

File details

Details for the file cachetools_async-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: cachetools_async-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.8 Linux/6.8.2-arch1-1

File hashes

Hashes for cachetools_async-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c23687b3090ca528539f3532a341d08452e2331a724e875c172833079af755da
MD5 92bab9d721c9753c648888d3a3c21abb
BLAKE2b-256 0e47396518ad092d016b238d8c0d4bc45903ea8e148c4c772df7f82ca433a519

See more details on using hashes here.

Supported by

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