PRO

Drizzle Cache Tag Invalidation

A cached Postgres read layer on drizzle-orm 1.0 rc.4 built around the opt-in query cache and the upstashCache provider from drizzle-orm/cache/upstash. It keeps the default global: false so the cache strategy stays explicit and every cached read is visible at its call site, uses .$withCache tags for the reads that a raw SQL write has to reach by name, reserves autoInvalidate: false for one aggregate that trades bounded staleness for a stable hit rate, and pairs db.$cache.invalidate by tag and by table with the writes the ORM cannot see. Most of the file confronts the eligibility list directly: raw SQL fails open and leaves stale entries, relational queries have no $withCache method at all so a hot read must be rewritten as an explicit join select, and a cached view read is indexed under zero tables so no write will ever drop it.

Install
npx shadcn@latest add https://ui.aryank.space/r/drizzle-cache-tag-invalidation.json

Installs from ui.aryank.space. To add it by hand, copy the files in Files below, or register the @compronents namespace via the docs.

Visualization
after the write
UPDATE price = 80
Product
price read
$100
db.query.products.findFirst() // cached, no tag, TTL 60s

The price changes to $80 but the cached read has 60 seconds left on its TTL, so every checkout until then charges the old $100. Watch the value the readers see stay wrong AFTER the write lands: a cache with no invalidation is a machine for serving the past.

Files

Dependencies