Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

benzene-cache

Caching for Benzene Python: a cache-aside abstraction over one narrow async Cache port, with an in-memory backend for tests and single-process services and a Redis backend for a shared store. Depends only on benzene-core.

pip install benzene-cache          # in-memory only
pip install benzene-cache[redis]   # + the redis.asyncio backend

Cache is three async methods keyed on a string — get, set (with an optional TTL), delete — and every backend is interchangeable behind it. get_or_load is the cache-aside workhorse: return the cached value, or run the loader once, store it, and hand it back.

from benzene.cache import InMemoryCache, RedisCache, get_or_load, CacheAside

cache = InMemoryCache()  # or RedisCache("redis://localhost")


async def load_profile() -> dict:  # the loader may be sync or async
    return await profiles.fetch(user_id)


# Cache-aside: hits the cache, and only runs the loader on a miss (then caches for 300s).
profile = await get_or_load(cache, f"profile:{user_id}", load_profile, ttl=300)

# Bind a cache + default TTL when you're repeating them.
profiles_cache = CacheAside(cache, default_ttl=300)
profile = await profiles_cache.get_or_load(f"profile:{user_id}", load_profile)
  • Cache — the abstraction (mirrors Benzene.Cache.Core): async get(key) -> Any | None, async set(key, value, *, ttl=None), async delete(key). A miss returns None and never raises.
  • InMemoryCache — a dict with optional per-entry TTL and an injectable clock, so expiry is asserted without sleeping. The same shape as benzene.resilience's InMemoryIdempotencyStore.
  • RedisCache — a Cache over redis.asyncio (mirrors Benzene.Cache.Redis). Values round-trip through JSON (benzene.core.encode_body on the way in, json.loads on the way out) and TTL maps to SET ... EX/PX. The redis SDK is imported lazily, and an already-built client can be injected — so it is exercised against an in-memory fake with no SDK and no network.
  • get_or_load / CacheAside — read-through-on-miss over any Cache; the loader is sync or async and runs exactly once per miss.

Nothing here needs Redis or a real clock to test: the in-memory cache takes an injectable clock and the Redis cache takes an injected client, so the whole surface runs deterministically in memory. Mirrors .NET's Benzene.Cache.Core and Benzene.Cache.Redis, and contributes the benzene.cache subpackage to the shared benzene namespace.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

benzene_cache-0.1.0b1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

benzene_cache-0.1.0b1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file benzene_cache-0.1.0b1.tar.gz.

File metadata

  • Download URL: benzene_cache-0.1.0b1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for benzene_cache-0.1.0b1.tar.gz
Algorithm Hash digest
SHA256 7e21245488469d0cf00b891d950af6859ed2f7d3585019b3d5ea3f185cf35c96
MD5 18ace7d95abcacafc142e143db221151
BLAKE2b-256 15bab46f23b4901383d47c22645b3bd021dd521bf892fe3af81da19711da6b75

See more details on using hashes here.

Provenance

The following attestation bundles were made for benzene_cache-0.1.0b1.tar.gz:

Publisher: release.yml on daniellepelley/benzene-python

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

File details

Details for the file benzene_cache-0.1.0b1-py3-none-any.whl.

File metadata

File hashes

Hashes for benzene_cache-0.1.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 9320149b91cf7f0f758e1a8cd49cdfcfca41665bf407c1e5e9df0d13af5355e2
MD5 eb0716525fb83eeed12044be55964096
BLAKE2b-256 d791e18ef97ca54769e3ac07158c8e5888f80be64ceb5cfa9a6545449f344f0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for benzene_cache-0.1.0b1-py3-none-any.whl:

Publisher: release.yml on daniellepelley/benzene-python

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

Release history Release notifications | RSS feed

This release

0.1.0b1 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page