CacheConfig
CacheConfig =
object
Defined in: packages/db/src/types.ts:71
Configuration for the database cache layer.
Controls how query results are cached and invalidated. Mutations automatically bump table version counters, causing subsequent reads to miss the cache.
Example
Section titled “Example”const db = createDb({ d1: env.DB, schema, grants: resolvedGrants, user: currentUser, cache: { backend: "cache-api", ttl: "30s", staleWhileRevalidate: "5m", exclude: ["sessions"], },});Properties
Section titled “Properties”backend
Section titled “backend”backend:
CacheBackend
Defined in: packages/db/src/types.ts:73
Which cache backend to use: edge-local Cache API or global KV.
exclude?
Section titled “exclude?”
optionalexclude:string[]
Defined in: packages/db/src/types.ts:81
Table names that should never be cached (e.g., ["sessions", "tokens"]).
optionalkv:KVNamespace
Defined in: packages/db/src/types.ts:75
KV namespace binding. Required when backend is "kv".
onHit()?
Section titled “onHit()?”
optionalonHit: (key,table) =>void
Defined in: packages/db/src/types.ts:83
Observability hook called on cache hits.
Parameters
Section titled “Parameters”string
string
Returns
Section titled “Returns”void
onInvalidate()?
Section titled “onInvalidate()?”
optionalonInvalidate: (tables) =>void
Defined in: packages/db/src/types.ts:87
Observability hook called when tables are invalidated by mutations.
Parameters
Section titled “Parameters”tables
Section titled “tables”string[]
Returns
Section titled “Returns”void
onMiss()?
Section titled “onMiss()?”
optionalonMiss: (key,table) =>void
Defined in: packages/db/src/types.ts:85
Observability hook called on cache misses.
Parameters
Section titled “Parameters”string
string
Returns
Section titled “Returns”void
staleWhileRevalidate?
Section titled “staleWhileRevalidate?”
optionalstaleWhileRevalidate:string
Defined in: packages/db/src/types.ts:79
Stale-while-revalidate window (e.g., "5m"). Serves stale data while revalidating in the background.
optionalttl:string
Defined in: packages/db/src/types.ts:77
Default TTL for cached queries (e.g., "30s", "5m", "1h"). Defaults to "60s".