Registry / database / layered-cache

layered-cache

JSON →
library4.0.2jsnpmunverified

A hierarchical cache manager for Node.js that chains multiple cache layers (e.g., in-memory LRU, database, remote server) with automatic fallback and propagation. v4.0.2 is the current stable release, with infrequent updates. Key differentiator: supports arbitrary key types, optional multi-get/set, and a customizable not-found check. The library is ESM-only and requires Node >=10.

npm install layered-cache
INSTALL
IMPORT
SIG · LAYERED-CACHE
L
layered-cache
databasejavascriptv4.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LCache
import LCache from 'layered-cache'
const LCache = require('layered-cache')
ESM-only; CommonJS require will fail.
InterfaceLayer (type)
import type { InterfaceLayer } from 'layered-cache'
import { InterfaceLayer } from 'layered-cache'
InterfaceLayer is a TypeScript interface, not a runtime value. Use type import.
Layer (type)
import type { Layer } from 'layered-cache'
import { Layer } from 'layered-cache'
Layer is a TypeScript type alias for the simple object layer definition.

Demonstrates creating a three-layered cache with in-memory LRU, async DB layer, and remote fallback.

import LRU from 'lru-cache' import LCache from 'layered-cache' const lru = new LRU({ max: 100 }) const cache = new LCache([ lru, { get: async (key) => { // simulate DB query return null }, set: async (key, value) => { // save to DB } }, { get: async (key) => `remote-${key}` } ]) const value = await cache.get('foo') console.log(value) // 'remote-foo' // After first get, LRU and DB layer have the value. const cached = await cache.get('foo') console.log(cached) // 'remote-foo' (from LRU)
Debug
Known issues
breakingThe library is ESM-only since v4.0.0. CommonJS require() will throw an error.
fix
Migrate to ESM by using import statements or dynamic import().
affects: >=4.0.0
gotchaValues equal to undefined or null are treated as 'not found' by default. This can cause unexpected cache misses.
fix
Provide a custom isNotFound option to override the default behavior.
affects: all
gotchaThe interfaces 'InterfaceLayer' and 'Layer' are TypeScript-only; they are not runtime values. Attempting to import them as values will produce undefined.
fix
Use `import type { InterfaceLayer } from 'layered-cache'`.
affects: >=4.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Using require() with ESM-only package layered-cache v4+.
fix
Replace require() with import or use dynamic import().
TypeError: LCache is not a constructor
Importing the default export incorrectly, e.g., named import { LCache }.
fix
Use `import LCache from 'layered-cache'`.
Error: A layer must have at least one of methods: 'get' or 'mget'
Providing a layer that implements neither get nor mget.
fix
Ensure every layer implements get() or mget().
Upgrade
Version history
4.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
layered-cache — npm install layered-cache · libregistry