Skip to content

perf(kv): reduce KV round-trips on cache hit via local tag cache #439

@Divkix

Description

@Divkix

Problem

When KVCacheHandler.get() gets a cache hit, it validates every tag by issuing one kv.get() per tag in parallel (Promise.all). For entries with 20 tags, that's 20 KV round-trips per cache hit. Same pattern in revalidateTag() — N parallel PUTs.

Solution

Add a local in-memory Map<string, { timestamp, fetchedAt }> with a 5-second TTL that caches tag invalidation timestamps. Within the TTL window, tag checks are served from memory with zero I/O. After TTL expiry, the next request re-fetches from KV.

Key behaviors:

  • revalidateTag() updates the local cache immediately so invalidations are reflected without waiting for TTL expiry
  • resetRequestCache() clears the local cache for per-request isolation
  • NaN tag timestamps are cached and correctly treated as invalidation
  • Only uncached/expired tags trigger KV reads (partial cache hits work)

Implementation

PR: #433

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions