@unikvs/vercel-runtime-cache
How to use @unikvs/vercel-runtime-cache, which stores values in the Vercel runtime cache.
Overview
@unikvs/vercel-runtime-cache is storage that stores values in the Vercel runtime cache.
This storage auto-detects the runtime cache and falls back to an in-process Map when none is found. In both cases entries are evicted by TTL and maximum count.
nameisVercelRuntimeCache.- When you share a backend, set a
namespace. Whennamespaceis set,cleardeletes only the keys tracked by the current instance; keys written by other instances or processes in the same namespace are not removed. maxEntrieseviction and expired-key removal rely on per-instance tracking. A shared backend may therefore hold more thanmaxEntrieskeys.- Backend eviction is oldest-written (FIFO), while fallback eviction is least-recently-used (LRU).
- When
namespaceis empty and the backend providesclear,cleardeletes the entire backend. isOpenis alwaystrue.open()/close()only check for interruption.deletethrowsKeyNotFoundErrorfor a missing key.- Supports streamed reading and writing.
Install it with the following command:
npm install @unikvs/vercel-runtime-cachepnpm add @unikvs/vercel-runtime-cacheyarn add @unikvs/vercel-runtime-cachebun add @unikvs/vercel-runtime-cachenub add @unikvs/vercel-runtime-cacheaube add @unikvs/vercel-runtime-cacheUsage
VercelRuntimeCache implements IStorage. Pass it to UniKvs via persist().
import { VercelRuntimeCache } from "@unikvs/vercel-runtime-cache";
VercelRuntimeCache
Storage that uses the Vercel Runtime Cache.
The constructor arguments are as follows:
ttlSeconds?number
The entry lifetime (in seconds).
number60maxEntries?number
The maximum number of entries to keep. Unlimited when omitted. Evicts oldest-written entries (FIFO) on the backend and least-recently-used entries (LRU) on the fallback.
numbernamespace?string
The namespace prepended to backend keys. Use it when multiple storages share the same backend.
string""clone?<T>(value: T) => T
The function used to clone values. Uses structuredClone by default.
<T>(value: T) => Tbackend?VercelRuntimeCacheBackend
The backend to use. When omitted, it is auto-detected and falls back to an in-process Map when none is found.
VercelRuntimeCacheBackendnow?() => number
The function returning the current time. Use it to fix the time in tests.
() => numberThe following example saves a value and reads it back:
import { VercelRuntimeCache } from "@unikvs/vercel-runtime-cache";
import * as unikvs from "unikvs";
const kvs = new unikvs.Builder<{
foo: unikvs.Value<string>;
}>()
.persist(new VercelRuntimeCache())
.build();
await kvs.open();
await kvs.set("foo", "hello");
const data = await kvs.get("foo");
console.log(data); // "hello"
await kvs.close();
Data types
| Operation | Data type |
|---|---|
write / read |
Any value (strings, objects, etc.) |
getWritable / getReadable chunks |
Uint8Array<ArrayBuffer> |
- Any value can be stored. By default values are cloned with
structuredClone, so handle values thatstructuredClonecan clone. - Expired entries are treated as missing and swept on read.
- The stream from
getWritableaccumulatesUint8Array<ArrayBuffer>chunks and stores them together onclose. getReadablecan only be used when the stored value is aUint8Array<ArrayBuffer>.
Errors
This package throws the following errors.
InvalidTtlSecondsError
Meaning
Thrown at construction when ttlSeconds is not a finite number greater than 0.
How to fix
Specify a finite number greater than 0 for ttlSeconds.
InvalidMaxEntriesError
Meaning
Thrown at construction when maxEntries is not an integer greater than or equal to 0.
How to fix
Specify an integer greater than or equal to 0 for maxEntries.
InvalidNamespaceError
Meaning
Thrown at construction when namespace is not a string.
How to fix
Specify a string for namespace.
InvalidChunkTypeError
Meaning
Thrown when a chunk other than Uint8Array<ArrayBuffer> is written to the stream from getWritable, or when the getReadable target value is not a Uint8Array<ArrayBuffer>.
How to fix
Use Uint8Array<ArrayBuffer> for values handled as streams.
KeyNotFoundError
Meaning
Thrown by read / delete when the specified key does not exist or has expired. A shared @unikvs/core error.
How to fix
Check with exists before reading.
Example
The following example catches KeyNotFoundError.
import { KeyNotFoundError } from "@unikvs/vercel-runtime-cache";
try {
const data = await kvs.get("missing-key");
} catch (ex) {
if (ex instanceof KeyNotFoundError) {
console.log("Key not found");
} else {
throw ex;
}
}