Tags & Invalidation
Tag-based invalidation keeps the cache correct without manual key bookkeeping.
The CacheBridge tags each cached query by the tables it touches.
Automatic tagging
Section titled “Automatic tagging”const { rows } = await bridge.query(db, plan); // tagged with plan's tablesWhen you write, the bridge invalidates every cache entry tagged with the affected table:
await db.from(orders).insert({ /* ... */ });await bridge.invalidateAfterWrite(ordersPlan); // drops orders-tagged cachesManual invalidation
Section titled “Manual invalidation”await bridge.invalidateTag("orders"); // one tagawait bridge.invalidateTags(["orders", "users"]); // severalWhy tags?
Section titled “Why tags?”A single write (e.g. orders) can stale many cached queries. Tags let one
invalidation clear them all, keyed by table name — no per-query bookkeeping.
Best practices
Section titled “Best practices”- Let
invalidateAfterWriterun after every write path. - Invalidate by table, not by guessing cache keys.
- Use L2 (Redis) tags so all instances see the invalidation.
Common mistakes
Section titled “Common mistakes”- Forgetting
invalidateAfterWrite— serves stale data. - Manual key invalidation that misses some queries.
