Skip to content

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.

const db = createDb({
d1: env.DB,
schema,
grants: resolvedGrants,
user: currentUser,
cache: {
backend: "cache-api",
ttl: "30s",
staleWhileRevalidate: "5m",
exclude: ["sessions"],
},
});

backend: CacheBackend

Defined in: packages/db/src/types.ts:73

Which cache backend to use: edge-local Cache API or global KV.


optional exclude: string[]

Defined in: packages/db/src/types.ts:81

Table names that should never be cached (e.g., ["sessions", "tokens"]).


optional kv: KVNamespace

Defined in: packages/db/src/types.ts:75

KV namespace binding. Required when backend is "kv".


optional onHit: (key, table) => void

Defined in: packages/db/src/types.ts:83

Observability hook called on cache hits.

string

string

void


optional onInvalidate: (tables) => void

Defined in: packages/db/src/types.ts:87

Observability hook called when tables are invalidated by mutations.

string[]

void


optional onMiss: (key, table) => void

Defined in: packages/db/src/types.ts:85

Observability hook called on cache misses.

string

string

void


optional staleWhileRevalidate: string

Defined in: packages/db/src/types.ts:79

Stale-while-revalidate window (e.g., "5m"). Serves stale data while revalidating in the background.


optional ttl: string

Defined in: packages/db/src/types.ts:77

Default TTL for cached queries (e.g., "30s", "5m", "1h"). Defaults to "60s".