@n8io/baseline-cache
A typed cache that, by default, never takes your Project down with it. Every read is checked by the schema you give it, an entry in an older shape reads as a miss, and a store that fails reads as a miss too.
pnpm add @n8io/baseline-cache
Guarantees
| You get |
How |
| Typed reads |
Each get runs the stored JSON through your schema; the cache's type is the schema's output. |
| Old entries are misses, not crashes |
A value the schema refuses, such as one an older version of your Project wrote, reads as undefined. |
| Any schema library |
Any Standard Schema: a Zod, Valibot or ArkType schema goes in as it is. The Package imports none. |
| Up while the store is down |
By default get misses and set and delete resolve. Each failure counts on baseline.cache.store_errors, with one warning log per 30 s per cache. |
| This process never serves what it tried to replace |
By default, after a failed set or delete this process reads that id as a miss for ttlMs. Other processes may still serve the old entry until it expires. |
| Bounded staleness |
Entries expire after ttlMs, 60 s by default. |
| One store, many caches |
Open one CacheStore per process; keys are <prefix>:<id>. Swapping the backend touches one file. |
Example
import { cacheCreate } from "@n8io/baseline-cache";
import { z } from "zod";
const Foo = z.object({ createdAt: z.coerce.date(), name: z.string() });
const fooCache = cacheCreate(cacheStore, {
prefix: "foo",
schema: Foo,
ttlMs: 30_000,
});
await fooCache.set(foo.id, foo);
const cached = await fooCache.get(foo.id); // the Foo, or undefined
Entries are JSON, so a Date comes back as a string: z.coerce.date() turns it back, or every read would be a miss. In unit tests build the cache on cacheStoreMemoryCreate(). It stores JSON as the Redis store does. To test expiry, give the cache a short ttlMs and wait. The Package's own pnpm test needs Docker for Redis; pnpm test:unit runs the tests that need no Redis.
Reference
| Export |
Does |
cacheCreate(store, options), CacheOptions, CacheKeyedOptions, Cache<T>, CacheKeyed<T> |
A cache on a store, from { prefix, schema, keyOf?, ttlMs?, onStoreError? } (CacheKeyedOptions when keyOf is given, else CacheOptions). Keys are <prefix>:<id> and entries expire after ttlMs (default 60 s). schema is any Standard Schema: a Zod, Valibot or ArkType schema goes in as it is. A stored value that fails the schema reads as a miss. It keeps working when the store does not: see "When the store is down". Throws TypeError on a ttlMs that is not a positive integer, an empty prefix or an unknown onStoreError. Returns a Cache<T>: get(id), set(id, value) and delete(id); with keyOf: (value) => id, for values that carry their own id, a CacheKeyed<T>, the same but set(value) takes the value alone. |
CacheStore |
Where entries live, as JSON strings by key with a TTL. Open one per process and build every cache on it; a new backend is one more CacheStore. |
cacheStoreRedisCreate({ redisClient }), CacheStoreRedisOptions |
A store on a connected node-redis client. Each call fails after 1 s without an answer, which the cache on top treats like any other store failure. |
cacheStoreMemoryCreate() |
A store in process, for unit tests and memory://. Caches on one store share its keys, as on Redis, so give each its own prefix. |
Pick the store in one place, and name no backend anywhere else:
// src/cache-store-open.ts: the only file that knows the backend
import {
cacheStoreMemoryCreate,
cacheStoreRedisCreate,
} from "@n8io/baseline-cache";
import type { RedisClient } from "@n8io/baseline-redis";
export const cacheStoreOpen = ({ redis }: { redis: RedisClient | "memory" }) =>
redis === "memory"
? cacheStoreMemoryCreate()
: cacheStoreRedisCreate({ redisClient: redis });
The cache's value type is the schema's output: the Example's Foo gives a Cache<{ createdAt: Date; name: string }>, so get returns that or undefined. The Package does not import Zod. Any Standard Schema works the same way, for example Valibot's v.object({ name: v.string() }).
A cache is an optimization, so by default a store that fails or times out never fails the caller:
get is a miss, so the caller reads the source of truth.
set and delete resolve.
- Every failure is counted on
baseline.cache.store_errors (by baseline.cache.operation and baseline.cache.prefix), and logged as one warning per 30 s per cache, so you can alert on an outage without a log line per call.
A set or delete that fails leaves the old entry in the store. The process that called it remembers the id for ttlMs and reads it as a miss, so it never serves the value it tried to replace. Other processes may.
Pass onStoreError: "throw" to get the store's error from every call instead, for a value you would rather fail on than read without the cache.
The cache does not see writes to the source of truth: you delete (or set) after each one. When that call is lost (the store is down, or the process dies between the commit and the call), other processes can serve the old value until its entry expires: at most ttlMs, 60 s by default. For a value that cannot be that stale, shorten ttlMs or do not cache it.
By the Template's convention: CACHE_URL (redis:// or rediss://, or memory:// for the memory store). You bring the schema library (the Template uses Zod), and the Redis store takes the client you pass it, so the Package needs neither zod nor redis.
pnpm test:mutation runs Stryker over src/cache.ts and src/cache-store-memory.ts with the unit tests (vitest.mutation.config.ts leaves out the Redis ones, which need Docker) and fails below a floor of 90%. The score today is 94.74% (108 killed, 5 survived, 1 not covered of 114 mutants). All six are equivalent: the sweep of expired marks in staleMark only frees memory (a mark that has ended already reads as a miss), and the catch in cacheJsonParse returns undefined either way. Raise the floor as tests allow; never lower it to pass. It is not in CI yet.
Needs peer @opentelemetry/api for the counter (it does nothing until your Project registers a meter provider); CACHE_URL by the Template's convention. Runs on Redis (any connected node-redis client, such as @n8io/baseline-redis's) or in memory.