Installation
Install lazy-layers-cache and check Node.js, module, and TypeScript requirements.
LazyLayers 0.5.3 supports Node.js 20 or 22+. It ships ESM, CommonJS, and TypeScript declarations. You can run an L1-only cache without Redis or a broker.
Install
npm install lazy-layers-cacheThe package includes ioredis, amqplib, the modular NATS clients, and the LZ4 and Snappy codecs as dependencies. setupCache can create the Redis client for you. If your application imports a client directly to supply its own connection, declare that client as a direct application dependency too.
Choose a module format
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:
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
- Quickstart shows reads, invalidation, and cleanup.
- API reference describes
setupCacheoptions and ownership.