Skip to content

PATTERN Cited by 1 source

Pluggable cache handler

Pattern

A framework's caching layer exposes a single setter (setCacheHandler(impl) or equivalent) that accepts any implementation of a small cache interface, letting the deployment pick the right storage trade-off without framework-level changes.

Forces

  • Different applications have different cache requirements — small vs large payloads, frequent vs infrequent revalidation, strong vs weak consistency, per-deployment region affinity.
  • Different platforms provide different cache primitivesCloudflare KV, R2, Cache API, Redis, S3, …
  • Framework shouldn't pick one; the deployment should pick.
  • Users should not have to re-implement the framework's caching semantics (revalidation windows, ISR, tagged invalidation) in each handler — the contract should be storage primitives (get(key), set(key, value, ttl), delete(key)) not caching-semantics primitives.

Solution shape

// Framework
interface CacheHandler {
  get(key): Promise<CacheEntry | null>;
  set(key, entry): Promise<void>;
  delete(key): Promise<void>;
}
setCacheHandler(impl: CacheHandler): void;

The framework implements the caching semantics (revalidation logic, ISR, invalidation); the handler implements only the storage layer.

Canonical application — vinext

import { KVCacheHandler } from "vinext/cloudflare";
import { setCacheHandler } from "next/cache";

setCacheHandler(new KVCacheHandler(env.MY_KV_NAMESPACE));

Named alternatives per the 2026-02-24 launch post:

  • KV (default via KVCacheHandler).
  • R2 — better for large cached payloads.
  • Cloudflare Cache API — forthcoming lower-config alternative.

Consequences

  • Deployment flexibility — each app picks the right storage primitive.
  • Framework stays agnostic to platform cache substrate.
  • Cache handlers are simple to write — the complex semantics stay in the framework.
  • Cross-platform portability — the same framework runs anywhere a CacheHandler can be implemented.
  • Requires the framework to define a stable cache handler contract it can evolve without breaking handlers.

Seen in

Last updated · 200 distilled / 1,178 read