Cache configuration
Set up cache with exact runtime options, typed JavaScript/TypeScript examples and lifecycle policies.
Before you configure it#
Install the runtime and the packages used by this feature in the application directory. Keep the dependency lockfile committed. Configure only services the application actually uses; a public package name is not automatic service registration.
npm install zeruxjs @zeruxjs/cacheJavaScript application setup#
/** @type {import('zeruxjs').ZeruxConfig} */
export default {
cache: {
connector: 'memory',
options: { maxEntries: 2000 },
prefix: 'catalog:v1',
defaultTtlSeconds: 60,
startupTimeoutMs: 3000,
required: false,
},
};This example is a server configuration. Supply the referenced environment values through the server process before starting it. The documentation site's static exporter does not start this service merely because it imports a config file.
What each setting does#
Runtime setup uses connector, whereas standalone createCacheManager uses helper. For memory caching, set connector: 'memory'. For Redis, install the adapter and use connector: '@zeruxjs/cache-redis' with options: { url: process.env.REDIS_URL }. An in-memory cache is isolated to one process and disappears on restart; Redis infrastructure is managed by the deployment.
prefix separates logical namespaces and data revisions. Application keys remain unprefixed at the call site; include a server-verified tenant or permission boundary when values depend on identity. defaultTtlSeconds uses seconds, not milliseconds. Set an explicit TTL on entries whose freshness differs from the namespace default. A cache miss returns undefined; it is not the same as a cached null value.
The default startup timeout is 3,000 milliseconds. Only required: true makes connector startup/timeout failure fatal. An optional cache may be unavailable, so a cache-backed application must still be able to fetch authoritative data. An import failure or invalid manager export is outside that optional connector failure handler and can still reject startup.
TypeScript configuration#
For a TypeScript application, use zerux.config.ts. Validate the service object separately so errors point to its option rather than a large application object. NonNullable removes the optional top-level absence; it does not invent defaults or validate external data at runtime.
import type { ZeruxConfig } from 'zeruxjs';
type Options = NonNullable<ZeruxConfig['cache']>;
const options = {
connector: 'memory',
options: { maxEntries: 2000 },
prefix: 'catalog:v1',
defaultTtlSeconds: 60,
startupTimeoutMs: 3000,
required: false,
} satisfies Options;
export default { cache: options } satisfies ZeruxConfig;In JavaScript, the JSDoc annotation provides editor assistance. In TypeScript, satisfies checks the shape while preserving inferred literal values. Neither changes the runtime behavior or establishes a network connection during a typecheck. See TypeScript application development for strict settings and validation boundaries.
Lifecycle and failure policy#
The runtime owns a configured cache manager; do not close it after each response. A standalone manager must be closed when its owner stops. Cache initialization runs after database initialization.
Namespace clearing is not a transaction. In Redis, namespace clearing scans matching keys and can race with concurrent writes. Never treat a prefixed clear as a substitute for tenant authorization. Avoid unprefixed clearing on shared Redis, and do not enable allowFlushDb except with a deliberately dedicated instance.
Verify a real integration#
- Start the application with just this feature and its declared dependencies configured.
- Check service readiness, then perform one operation through the application API.
- Test a failure path such as unavailable infrastructure or a rejected input.
- Restart the runtime and verify that owned resources close and state behaves according to the chosen storage model.
- Repeat with the production build and the real deployment identity; a successful static docs build does not test external services.
Declared options#
The following table is captured from the source contract used by runtime bootstrap. Optional means a property may be omitted; it does not mean every combination is meaningful. Callback and nested adapter shapes must satisfy the full declared type.
| Option | Accepted value | Runtime behavior |
|---|---|---|
connector | string | Runtime adapter name; absent means unconfigured |
options | Record<string, unknown> | Forwarded to the adapter |
prefix | string | Cache manager namespace |
defaultTtlSeconds | number | Default entry TTL in seconds |
startupTimeoutMs | number | Positive timeout; otherwise 3000 ms |
required | boolean | Exactly true makes connector startup failure fatal |