[PR #433] [MERGED] perf(kv): local tag cache to reduce KV round-trips on cache hit #570

Closed
opened 2026-05-06 13:08:48 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/433
Author: @Divkix
Created: 3/11/2026
Status: Merged
Merged: 3/11/2026
Merged by: @james-elicx

Base: mainHead: perf/kv-tag-batching


📝 Commits (5)

  • 24b27ba perf(kv): add local in-memory tag cache to reduce KV round-trips
  • 94f07b7 fix(test): guard fake timers with try/finally to prevent leaks
  • 50694d6 fix(kv): address bonk review comments on local tag cache
  • 549a350 chore: merge main, resolve conflict (ExecutionContextLike)
  • 6653e29 refactor(kv): reuse cached timestamps in invalidation check loop

📊 Changes

2 files changed (+311 additions, -13 deletions)

View changed files

📝 packages/vinext/src/cloudflare/kv-cache-handler.ts (+80 -13)
📝 tests/kv-cache-handler.test.ts (+231 -0)

📄 Description

Closes #439

Summary

  • Adds a local in-memory Map with 5-second TTL to KVCacheHandler that caches tag invalidation timestamps
  • Eliminates redundant kv.get() calls for recently-seen tags — an entry with 20 tags no longer triggers 20 KV round-trips on every cache hit within the TTL window
  • revalidateTag() updates the local cache immediately so invalidations are reflected without waiting for TTL expiry
  • resetRequestCache() clears the local cache for per-request isolation

Test plan

  • 6 new test cases in tests/kv-cache-handler.test.ts covering:
    • Cached tags skip KV on second get()
    • revalidateTag() updates local cache (no KV GET for invalidated tags)
    • TTL expiry triggers fresh KV fetch (fake timers)
    • Tag invalidation works end-to-end with local cache
    • Uncached tags are still fetched from KV (partial cache hit)
    • NaN tag timestamp in local cache treated as invalidation
  • All 36 tests pass (31 existing + 5 new behavioral tests)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/cloudflare/vinext/pull/433 **Author:** [@Divkix](https://github.com/Divkix) **Created:** 3/11/2026 **Status:** ✅ Merged **Merged:** 3/11/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `perf/kv-tag-batching` --- ### 📝 Commits (5) - [`24b27ba`](https://github.com/cloudflare/vinext/commit/24b27ba98af5a69ea6816be241ab72b09593b666) perf(kv): add local in-memory tag cache to reduce KV round-trips - [`94f07b7`](https://github.com/cloudflare/vinext/commit/94f07b7219e013a898734a90a055fbee0edae3dd) fix(test): guard fake timers with try/finally to prevent leaks - [`50694d6`](https://github.com/cloudflare/vinext/commit/50694d62455ac4a48a6a912654d961098a9c7155) fix(kv): address bonk review comments on local tag cache - [`549a350`](https://github.com/cloudflare/vinext/commit/549a3508969642402caff1159c8d816857e95741) chore: merge main, resolve conflict (ExecutionContextLike) - [`6653e29`](https://github.com/cloudflare/vinext/commit/6653e2960d6822831e7ce1e1f1b184ad57adc46d) refactor(kv): reuse cached timestamps in invalidation check loop ### 📊 Changes **2 files changed** (+311 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/cloudflare/kv-cache-handler.ts` (+80 -13) 📝 `tests/kv-cache-handler.test.ts` (+231 -0) </details> ### 📄 Description Closes #439 ## Summary - Adds a local in-memory `Map` with 5-second TTL to `KVCacheHandler` that caches tag invalidation timestamps - Eliminates redundant `kv.get()` calls for recently-seen tags — an entry with 20 tags no longer triggers 20 KV round-trips on every cache hit within the TTL window - `revalidateTag()` updates the local cache immediately so invalidations are reflected without waiting for TTL expiry - `resetRequestCache()` clears the local cache for per-request isolation ## Test plan - [x] 6 new test cases in `tests/kv-cache-handler.test.ts` covering: - Cached tags skip KV on second `get()` - `revalidateTag()` updates local cache (no KV GET for invalidated tags) - TTL expiry triggers fresh KV fetch (fake timers) - Tag invalidation works end-to-end with local cache - Uncached tags are still fetched from KV (partial cache hit) - NaN tag timestamp in local cache treated as invalidation - [x] All 36 tests pass (31 existing + 5 new behavioral tests) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 13:08:48 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#570
No description provided.