Skip to main content

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

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

cache_sync-0.3.1.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

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

cache_sync-0.3.1-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

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

Hashes for cache_sync-0.3.1.tar.gz
Algorithm Hash digest
SHA256 5a48e2f27badff0898b806f33b0dbf47d864b4e64afdfc2b2ea696ce0a66d9df
MD5 3470f7ec89058417477e0b48e041210d
BLAKE2b-256 546340e851431a3719be1bf2575cb5ff43126aefb4aad493b8e1166e6307db1f

See more details on using hashes here.

Provenance

The following attestation bundles were made for cache_sync-0.3.1.tar.gz:

Publisher: release-please.yml on petercinibulk/cache-sync

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for cache_sync-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9ab877f60e410cbd05b4a83d656a636f6a13029df0f7d04796664c8776b33ae4
MD5 405ef6225b21dbb1891f63a1ee2553a0
BLAKE2b-256 cccba8405804dd7195b5285dc25736c24ca3713a7e17ebed6c1aa5c469d052cc

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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