an asyncio application layer cache and dataloader for python based microservices and applications with thundering herd protection
Project description
- info:
In-memory application layer cache
Installation
pip install async-cache
See full documentation at https://async-cache.readthedocs.io/
Core Usage: Function API for Microservices
Use AsyncCache for flexible caching:
from cache import AsyncCache
cache = AsyncCache(maxsize=1000, default_ttl=300) # TTL in seconds
async def get_data(key):
return await cache.get(
key,
loader=lambda: db_query(key), # auto-caches on miss
)
# Warmup hot keys at startup
await cache.warmup({"hot:key": lambda: preload_hot()})
# Metrics for observability
print(cache.get_metrics()) # hits, misses, size, hit_rate
Key Features & Examples
- Thundering Herd Protection
Prevents duplicate work under concurrent load (e.g., popular keys). Without it, 100 misses = 100 DB hits; with it, = 1.
cache = AsyncCache() async def loader(): return await db_query() # expensive # 100 concurrent -> 1 loader call results = await asyncio.gather(*[cache.get('key', loader=loader) for _ in range(100)])- DataLoader-Style Batching
Groups concurrent gets into one batch call (reduces DB load; configurable window/size).
async def batch_loader(keys): # one DB query for batch return {k: await db_batch_query(k) for k in keys} # auto-groups within 5ms window await asyncio.gather( cache.get(1, batch_loader=batch_loader), cache.get(2, batch_loader=batch_loader) )- Cache Warmup
Preload at startup to avoid cold misses.
await cache.warmup({ "user:1": lambda: load_user(1), "config:global": lambda: load_config(), })- Metrics
Observability for hit rate, size, etc. (global or per-function).
metrics = cache.get_metrics() # or func.get_metrics() # {'hits': 950, 'misses': 50, 'size': 200, 'hit_rate': 0.95} # Use for Prometheus/monitoring- TTL & Invalidation
Per-key control + size-based eviction.
await cache.set('key', value, ttl=60) # override await cache.delete('key') # or func.invalidate_cache(args) cache.clear()
Decorator Convenience
For simple/readable code (uses core API under the hood):
from cache import AsyncLRU, AsyncTTL
@AsyncLRU(maxsize=128)
async def func(*args):
...
@AsyncTTL(time_to_live=60, skip_args=1) # e.g. skip 'self'
async def method(self, arg):
...
Testing
A local test dashboard is available for interactive testing:
python demo/app.py # Runs on http://localhost:5001
Use it to verify caching behavior, metrics, and concurrent load handling.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file async_cache-2.0.1.tar.gz.
File metadata
- Download URL: async_cache-2.0.1.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fea3568605233a7aa044e89ca339e827946c198e1edcd686cae6ac2e593c143c
|
|
| MD5 |
ec5ea6e0009e06f4d943201e388826f9
|
|
| BLAKE2b-256 |
37833a42e1753d239ffe74ca9992aed1dfbaf2f6e17b7363ea9617839e7ba563
|
Provenance
The following attestation bundles were made for async_cache-2.0.1.tar.gz:
Publisher:
python-publish.yml on iamsinghrajat/async-cache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
async_cache-2.0.1.tar.gz -
Subject digest:
fea3568605233a7aa044e89ca339e827946c198e1edcd686cae6ac2e593c143c - Sigstore transparency entry: 1354819370
- Sigstore integration time:
-
Permalink:
iamsinghrajat/async-cache@d65247424e67e9ccd8378321c1276d9d2a61762b -
Branch / Tag:
refs/tags/2.0.1 - Owner: https://github.com/iamsinghrajat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d65247424e67e9ccd8378321c1276d9d2a61762b -
Trigger Event:
release
-
Statement type:
File details
Details for the file async_cache-2.0.1-py3-none-any.whl.
File metadata
- Download URL: async_cache-2.0.1-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06dcad7fc1967dcf697790b33645c83676cee7d0dfb08f2118b6dcbd8c234758
|
|
| MD5 |
0887d7d76213c02fcd8bde9c84facb7f
|
|
| BLAKE2b-256 |
9567ba63c0e4ee2eca1c9713ff5aa4c2d3e68c6f3841a53eb9a25d4ea890000e
|
Provenance
The following attestation bundles were made for async_cache-2.0.1-py3-none-any.whl:
Publisher:
python-publish.yml on iamsinghrajat/async-cache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
async_cache-2.0.1-py3-none-any.whl -
Subject digest:
06dcad7fc1967dcf697790b33645c83676cee7d0dfb08f2118b6dcbd8c234758 - Sigstore transparency entry: 1354819427
- Sigstore integration time:
-
Permalink:
iamsinghrajat/async-cache@d65247424e67e9ccd8378321c1276d9d2a61762b -
Branch / Tag:
refs/tags/2.0.1 - Owner: https://github.com/iamsinghrajat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d65247424e67e9ccd8378321c1276d9d2a61762b -
Trigger Event:
release
-
Statement type: