Migrating from cashews¶
Guide for migrating async caching from cashews to paceapi.
See Migrating to paceapi for shared defaults (no globals, Redis cutover, async-only functions).
API mapping¶
cashews |
paceapi |
|---|---|
cache.setup("mem://") / cache.setup("redis://...") |
cache = Cache() / cache = Cache(RedisStore(client)) |
cache.setup("mem://", prefix=...) + secondary Redis |
Cache(TieredStore(RedisStore(client), local_ttl=...)) |
@cache(ttl="10m", key="user:{user_id}") |
@users.cached("items", entity="user_id", key=("user_id",), ttl=600) |
@cache(ttl="10m", lock=True) |
@users.cached("items", ttl=600, lock=True) (in-process single-flight is always on; lock=True adds best-effort cross-process locking) |
@cache.early(ttl="10m", early_ttl="3m") |
@reports.cached("summary", ttl=600, refresh_after=420) (note: refresh_after is elapsed age, not remaining TTL) |
@cache.soft_fail / stale fallback |
stale= (stale-while-revalidate) + automatic fail-open reads |
@cache.invalidate("user:{user_id}:page:*") |
@users.invalidates("items", entity="user_id") (O(1), no wildcard scan) |
await cache.delete_match("user:1:*") |
await users.entity(1).invalidate() (O(1)) |
@cache.rate_limit(...) |
Limiter |
@cache.circuit_breaker(...) |
Built-in store_cooldown on Limiter; LoadShedder / ConcurrencyLimit for app protection |
Migrated app example¶
Namespaces (replacing cache.setup(...)):
# Replaces global `cache.setup("redis://...")` with explicit Cache and Namespace instances.
users = cache.namespace("user", ttl=600)
reports = cache.namespace("report", ttl=600)
Locked cached function (replacing @cache(..., lock=True)):
# Replaces `@cache(ttl="10m", lock=True, key="user:{user_id}:page:{page}")`.
@users.cached("items", entity="user_id", key=("user_id", "page"), lock=True)
async def fetch_items(user_id: int, page: int = 1) -> list[Item]:
return await db.list_items(user_id, page)
Proactive background refresh (replacing @cache.early(...)):
# Replaces `@cache.early(ttl="10m", early_ttl="3m")`: refresh proactively after 7m (420s).
@reports.cached("summary", ttl=600, refresh_after=420, lock=True)
async def fetch_summary() -> dict[str, int]:
return await db.build_summary()
O(1) scoped invalidation (replacing wildcard @cache.invalidate(...)):
# Replaces wildcard `@cache.invalidate("user:{user_id}:page:*")` with O(1) entity invalidation.
@users.invalidates("items", entity="user_id")
async def save_item(user_id: int, item: Item) -> Item:
return await db.add_item(user_id, item)
Full example: examples/from_cashews.py.
Behavioral differences¶
refresh_after=vs.early_ttl=:cashews'searly_ttl="3m"on a10mTTL refreshes when 3 minutes remain.paceapi'srefresh_after=420refreshes once 420 seconds (7 minutes) have elapsed.- No wildcard key templates: Instead of format-string keys +
SCAN/delete_match,paceapistructures entries by(namespace, entity, resource)so invalidating all pages for a user is a single O(1) write. - Explicit TTL units: Pass numeric seconds (
ttl=600) rather than duration strings ("10m"). - Typed Pydantic round-trips & deploy isolation: Return type annotations preserve models and automatically fingerprint schemas;
release=prevents rolling-deploy conflicts (Deploys and schema changes). - Graceful shutdown for background refreshes: Call
await cache.drain()in your app lifespan shutdown hook (Stampedes and stale values). - No disk backend or hit-rate decorator (
@cache.hit): Built-in stores are memory, Redis/Valkey, MongoDB, Memcached, andTieredStore.