cachetools_async
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.
Metadata
Release files for cachetools-async 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cachetools_async-0.0.5.tar.gz | 24.8 kB | Without provenance | Details for cachetools_async-0.0.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cachetools_async-0.0.5-py3-none-any.whl | Python 3 | none | any | Details for cachetools_async-0.0.5-py3-none-any.whl |
Total release size: 29.6 kB
Release files / cachetools_async-0.0.5.tar.gz
| Download URL | cachetools_async-0.0.5.tar.gz |
|---|---|
| Size | 24.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e99ec8f3a78e82b4f524e78f01d15b8746531481d862ac4f2d8af2a686e1c571
|
|
BLAKE2b-256 checksum How to use checksums |
596d01404fffea95b33d186a4d921b141918c0daf3ff74b6a715c0ac2de8f42e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.7.12
|
Release files / cachetools_async-0.0.5-py3-none-any.whl
| Download URL | cachetools_async-0.0.5-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8f690c8bc291718a652395832e90afa797195c838e98d6a9e530dc1de66fbc34
|
|
BLAKE2b-256 checksum How to use checksums |
b5fa136f97313ab298286725fc8004d44f5fc352ea416c2b874b7a91e83df66a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.7.12
|
Security for cachetools-async 0.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.