Skip to content

API & options

Creates a Drizzle cache adapter backed by a Cloudflare KV namespace.

import { cloudflareKVCache } from 'drizzle-cloudflare-kv-cache-adapter'
const cache = cloudflareKVCache(env.CACHE, {
strategy: 'explicit',
prefix: 'drizzle',
defaultTtlSeconds: 300,
})
const db = drizzle(env.DB, { cache })
ParamTypeDescription
kvKVNamespaceA bound KV namespace, e.g. env.CACHE.
optionsCloudflareKVCacheOptions?Optional configuration (below).
OptionTypeDefaultDescription
strategy'explicit' | 'all''explicit''explicit' caches only .$withCache() queries; 'all' caches all.
prefixstring'drizzle'Key namespace prefix; lets multiple apps share one KV namespace.
defaultTtlSecondsnumber300Fallback TTL when a query sets no ex. Clamped to KV’s 60s minimum.
import {
cloudflareKVCache, // factory (recommended)
CloudflareKVCache, // the class, if you need to extend it
type CloudflareKVCacheOptions,
} from 'drizzle-cloudflare-kv-cache-adapter'

Passed through Drizzle’s .$withCache({ config }):

FieldTypeDescription
exnumberTTL in seconds. Clamped to ≥ 60.
pxnumberTTL in milliseconds; converted to seconds, clamped to ≥ 60.
tagstringCache under a stable tag; invalidate via invalidate({ tags }).

ex takes precedence over px. Absolute-time options (exat / pxat) aren’t expressible as a KV relative TTL and are ignored.