Async hybrid Python cache with in-memory L1, distributed L2 providers, pluggable invalidation, stampede protection, and typed decorators.
Project description
cache-sync
Async hybrid Python cache with in-memory L1 caching, optional Redis L2 caching, pluggable invalidation, stampede protection, fail-safe stale values, and typed decorators.
Features
- Async-first API for Python 3.12 and newer.
- Fast in-process L1 cache with optional Redis-backed L2 storage.
- Pluggable invalidation buses for Redis Streams, RabbitMQ, Kafka, and PostgreSQL.
- Request stampede protection with per-key refresh coordination.
- Fail-safe stale reads for short backend outages.
- Typed decorators that preserve the wrapped function signature.
- Serializer choices for JSON, pickle, and Pydantic models.
Documentation
The end-user documentation is published at https://petercinibulk.github.io/cache-sync/ and is built from docs/ with Zensical.
Install
uv add cache-sync
Install optional providers only when your application uses them:
uv add "cache-sync[redis]"
uv add "cache-sync[rabbitmq]"
uv add "cache-sync[kafka]"
uv add "cache-sync[postgres]"
uv add "cache-sync[all]"
| Extra | Installs | Use when |
|---|---|---|
redis |
redis |
You need Redis L2 storage or Redis Streams invalidation. |
rabbitmq |
aio-pika |
You use RabbitMQ as the invalidation bus. |
kafka |
aiokafka |
You use Kafka as the invalidation bus. |
postgres |
asyncpg |
You use PostgreSQL LISTEN/NOTIFY for invalidation. |
pydantic |
pydantic |
You want Pydantic model serialization helpers. |
all |
all provider dependencies | You want every optional provider available. |
Quick Start
from cache_sync import CacheOptions, CacheSync
cache = CacheSync(
options=CacheOptions(
ttl_seconds=60,
fail_safe_seconds=300,
hard_timeout_seconds=5,
jitter_seconds=5,
),
)
await cache.start()
@cache.cached(lambda user_id: f"user:{user_id}")
async def get_user(user_id: str) -> dict[str, str]:
return {"id": user_id, "name": "Peter"}
user = await get_user("123")
await get_user.remove_cached("123")
await cache.stop()
Redis L2 Example
from redis.asyncio import Redis
from cache_sync import CacheOptions, CacheSync, RedisDistributedCache
redis = Redis.from_url("redis://localhost:6379/0")
cache = CacheSync(
distributed_cache=RedisDistributedCache(redis),
options=CacheOptions(ttl_seconds=60, fail_safe_seconds=300),
)
await cache.start()
@cache.cached(lambda product_id: f"product:{product_id}")
async def get_product(product_id: str) -> dict[str, str]:
return {"id": product_id}
For a complete walkthrough with shared values and cross-instance invalidation, see the get started tutorial.
Project
- License: MIT
- Source: https://github.com/petercinibulk/cache-sync
- Issues: https://github.com/petercinibulk/cache-sync/issues
Project details
Release history Release notifications | RSS feed
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 cache_sync-0.3.1.tar.gz.
File metadata
- Download URL: cache_sync-0.3.1.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a48e2f27badff0898b806f33b0dbf47d864b4e64afdfc2b2ea696ce0a66d9df
|
|
| MD5 |
3470f7ec89058417477e0b48e041210d
|
|
| BLAKE2b-256 |
546340e851431a3719be1bf2575cb5ff43126aefb4aad493b8e1166e6307db1f
|
Provenance
The following attestation bundles were made for cache_sync-0.3.1.tar.gz:
Publisher:
release-please.yml on petercinibulk/cache-sync
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cache_sync-0.3.1.tar.gz -
Subject digest:
5a48e2f27badff0898b806f33b0dbf47d864b4e64afdfc2b2ea696ce0a66d9df - Sigstore transparency entry: 1912558465
- Sigstore integration time:
-
Permalink:
petercinibulk/cache-sync@667d9bd68326c6cfbc01a13139b2510d05414033 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/petercinibulk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-please.yml@667d9bd68326c6cfbc01a13139b2510d05414033 -
Trigger Event:
push
-
Statement type:
File details
Details for the file cache_sync-0.3.1-py3-none-any.whl.
File metadata
- Download URL: cache_sync-0.3.1-py3-none-any.whl
- Upload date:
- Size: 18.6 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 |
9ab877f60e410cbd05b4a83d656a636f6a13029df0f7d04796664c8776b33ae4
|
|
| MD5 |
405ef6225b21dbb1891f63a1ee2553a0
|
|
| BLAKE2b-256 |
cccba8405804dd7195b5285dc25736c24ca3713a7e17ebed6c1aa5c469d052cc
|
Provenance
The following attestation bundles were made for cache_sync-0.3.1-py3-none-any.whl:
Publisher:
release-please.yml on petercinibulk/cache-sync
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cache_sync-0.3.1-py3-none-any.whl -
Subject digest:
9ab877f60e410cbd05b4a83d656a636f6a13029df0f7d04796664c8776b33ae4 - Sigstore transparency entry: 1912558583
- Sigstore integration time:
-
Permalink:
petercinibulk/cache-sync@667d9bd68326c6cfbc01a13139b2510d05414033 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/petercinibulk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-please.yml@667d9bd68326c6cfbc01a13139b2510d05414033 -
Trigger Event:
push
-
Statement type: