drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Add Cloudflare KV cache adapter

Open
#5,758 2 comments 3 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Feature hasn't been suggested before.

- [x] I have verified this feature I'm about to request hasn't been suggested before.

### Describe the enhancement you want to request

## Describe the enhancement

Drizzle currently has first-party query caching support through `upstashCache()` and `.$withCache()`.

It would be useful to also have a first-party Cloudflare KV cache adapter for Cloudflare-native apps using Drizzle with D1 / Workers.

Example API:

```ts
import { cloudflareKVCache } from "drizzle-orm/cache/cloudflare-kv";

const db = drizzle(env.DB, {
cache: cloudflareKVCache(env.QUERY_CACHE, {
strategy: "explicit",
prefix: "drizzle",
config: {
ex: 300,
},
}),
});
````

This would let users cache read-heavy queries without adding Upstash Redis as an extra external dependency when they are already deploying on Cloudflare.

This is not asking for KV to become a database driver. It is specifically for Drizzle query caching.

KV has some important limitations, so the adapter should document that it is best for stale-tolerant, read-heavy cache use cases. Cloudflare KV is eventually consistent and has a minimum expiration TTL of 60 seconds, so lower TTL values should probably be clamped or rejected with a clear error.

Possible implementation:

* store query results under keys like `:query:`
* optionally maintain table/tag index keys for invalidation
* support `.$withCache()`
* support `db.$cache.invalidate(...)` where practical

This would make Drizzle nicer to use in Cloudflare-first stacks using Workers, D1, KV, Durable Objects, and R2.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.