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.4.0.tar.gz (11.0 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.4.0-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

Details for the file cache_sync-0.4.0.tar.gz.

File metadata

  • Download URL: cache_sync-0.4.0.tar.gz
  • Upload date:
  • Size: 11.0 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.4.0.tar.gz
Algorithm Hash digest
SHA256 1bd5acd51c78ba595d7735e2ddd0c9edcad429d704a83bd9c526d6b1b3dcc944
MD5 c140b69652798451fb6d11047fd303b6
BLAKE2b-256 7da74abb5bdf67e72dbbdda31e87069a70f48a2798aea69828f62d64c6fbb11e

See more details on using hashes here.

Provenance

The following attestation bundles were made for cache_sync-0.4.0.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.4.0-py3-none-any.whl.

File metadata

  • Download URL: cache_sync-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 18.7 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.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 74a55ed6314a3c037a0930f39a8eae39d5cb488ca1e3f04159cfb69e4303c1e9
MD5 09fa6ce9753408a1f0a6ea2f8f5491ed
BLAKE2b-256 514112a5759bc84f840b03d86a3a966f97d8bfb52a08c2f2514b35df365a9f7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cache_sync-0.4.0-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