Skip to content

feat(cache): decouple cache storage from kv with cache config - #4709

Merged
pi0 merged 4 commits into
mainfrom
feat/cache-storage
Oct 3, 2026
Merged

pi0 merged 4 commits into
mainfrom
feat/cache-storage

Conversation

@pi0x

@pi0x pi0x commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Cached functions and handlers no longer use KV storage by default. A new cache config chooses the cache storage driver and sets global cache options.

export default defineConfig({
  cache: {
    driver: "memory", // "memory" (default) | "fs" | "kv"
    memory: { maxSize: 10_000, maxBytes: 100 * 1024 * 1024 },
    fs: { dir: ".data/cache" },
    // global defaults for cached functions, handlers and `cache` route rules
    defaults: { maxAge: 60, swr: true },
  },
});

Drivers

  • memory (default): ocache's built-in createMemoryStorage, one LRU store shared by all caches.
  • fs: one file per entry, built on ocache's createBlobStorage (ocache has no fs driver yet). Node.js compatible runtimes only; Nitro warns on other presets. A relative dir resolves against rootDir in dev and prerender, and against the working directory in production.
  • kv: the previous behavior. Entries go through useKV() under the cache: prefix.

Only the selected driver is bundled, via a new #nitro/virtual/cache module.

Backward compatibility

The default becomes kv when a cache, cache:* or cache/* KV mount point is configured (including $development / deprecated devStorage), so existing persistent cache setups keep working.

These cases are not detected, and need cache: { driver: "kv" } (documented in the cache docs and the migration guide):

  • a custom base pointing to another mount, for example base: "redis"
  • cache mounts added at runtime with useKV().mount(...)

Global options (cache.defaults)

  • Shared defaults (maxAge, swr, staleMaxAge, maxResolveTime, base) apply to cached functions, cached handlers and route rules.
  • Handler-only defaults (varies, allowQuery, allowCookies, allowAuthorization, sendCacheControl, cacheStatusHeader, maxBodySize) apply to handlers and route rules only.
  • Per-cache options take precedence. An option passed explicitly as undefined keeps the global default.

Notes

  • memory and fs declare binary, so binary bodies are stored as bytes instead of base64.
  • memory declares a per-entry ceiling, so maxBodySize now defaults to about 50 MB with that driver.
  • The fs driver doesn't evict expired files. ocache checks expiry on read, and the next write overwrites the file.

🤖 Generated with AI assistant

pi0 added 3 commits October 3, 2026 14:17
Add a `cache` config to choose the cache driver (`memory` (default), `fs` or `kv`)
and set global cache options. A `cache` kv mount point keeps the `kv` driver
for backward compatibility.
Keep global defaults for explicit `undefined` options, harden fs temp files,
validate `cache` config, and fix docs placement and limits.
@pi0x
pi0x requested a review from pi0 as a code owner October 3, 2026 14:22
@vercel

vercel Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nitro.build Ready Ready Preview Oct 3, 2026 2:25pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3819a2ea-08b6-44d2-ae34-5e44ee221730
📥 Commits

Reviewing files that changed from the base of the PR and between 6619e00 and f3c1e9a.

📒 Files selected for processing (16)
  • docs/1.docs/7.cache.md
  • docs/1.docs/8.storage.md
  • docs/1.docs/99.migration.md
  • docs/3.config/0.index.md
  • src/build/virtual/_all.ts
  • src/build/virtual/cache.ts
  • src/config/defaults.ts
  • src/config/loader.ts
  • src/config/resolvers/cache.ts
  • src/runtime/internal/cache-fs.ts
  • src/runtime/internal/cache-kv.ts
  • src/runtime/internal/cache.ts
  • src/runtime/internal/route-rule-handlers.ts
  • src/runtime/virtual/cache.ts
  • src/types/config.ts
  • test/unit/config-cache.test.ts
 _________________________________
< My GPU is bigger than your GPU. >
 ---------------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4709

commit: f3c1e9a

@pi0
pi0 merged commit 87e338e into main Oct 3, 2026
14 of 15 checks passed
@pi0
pi0 deleted the feat/cache-storage branch October 3, 2026 14:28

This branch was successfully deployed

1 active deployment
Preview — f3c1e9ac Deployed Oct 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants