LazyLayersv9.0.0

Installation

Install lazy-layers-cache and check Node.js, module, and TypeScript requirements.

Install with npm install lazy-layers-cache. Version 9.0.0 supports Node.js 22 or later, ships ESM, CommonJS and TypeScript declarations, and has one runtime dependency, msgpackr. You can run an L1-only cache without Redis or a broker.

Install

npm
npm install lazy-layers-cache

Integrations are optional peer dependencies. Install the ones you use; importing the package never loads them, and a missing one raises MissingPeerDependencyError with the install command.

You useAlso install
Redis L2 or Redis Pub/Sub, including setupCache({ redis })ioredis
RabbitMQEventBusamqplib
NatsEventBus@nats-io/transport-node @nats-io/jetstream
LZ4 or Snappy compression (recommended with Redis)lz4-napi snappy

Use Node.js 22+ with NATS: its current transitive dependency declares that minimum.

Without lz4-napi, values that would use LZ4 fall back to Snappy, then Zstandard (Node 22.15+), then no compression. setupCache waits for the native codecs to load; when you construct caches yourself, await nativeCodecsReady once at startup or pass the modules to registerNativeCodecs.

Choose a module format

ESM: cache.mjs
import { setupCache } from 'lazy-layers-cache'

const cache = await setupCache({ namespace: 'demo', redis: false })
try {
  console.log(await cache.getOrSet('greeting', async () => 'Hello'))
} finally {
  await cache.close()
}

Run node cache.mjs or node cache.cjs. Both print Hello. For ESM in .js files, set "type": "module" in your application's package.json.

TypeScript

Import types from the package without installing a separate @types package. Set the key and value types once when you create a cache:

src/cache/index.ts
import { setupCache } from 'lazy-layers-cache'

interface User {
  id: string
  name: string
}

export const cache = await setupCache<string, User>({
  namespace: 'users-api',
  redis: false,
})

getOrSet now returns Promise<User | undefined> and checks the loader's return type. For a Node.js ESM project, use "module": "NodeNext" and "moduleResolution": "NodeNext" with "type": "module". Keep the .js extension in relative imports that will run as compiled JavaScript.

Connect shared infrastructure

Managed Redis setup requires Redis 6 or newer, a reachable REDIS_URL, and access to the commands used by the cache and event bus. Set redis: { required: true } when your deployment depends on shared caching so missing configuration fails at startup.

See production setup for Redis ACLs and upgrade guidance. RabbitMQ and NATS are alternative event buses, not requirements for starting with Redis.

Where to next

On this page