Registry / storage / async-cache-dedupe

async-cache-dedupe

JSON →
library3.4.0jsnpmunverified

An asynchronous caching library with built-in deduplication, ensuring each resource is fetched only once concurrently. Version 3.4.0 is the latest stable, actively maintained. It supports memory and Redis storage backends, with optional TTL, stale-while-revalidate, invalidation, and custom serialization via transformers. Differentiators include automatic deduplication of in-flight requests, safe key serialization via safe-stable-stringify, and seamless integration with GraphQL and other async contexts.

npm install async-cache-dedupe
INSTALL
IMPORT
SIG · ASYNC-CACHE-DEDUPE
A
async-cache-dedupe
storagejavascriptv3.4.0
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.

createCache
import { createCache } from 'async-cache-dedupe'
const createCache = require('async-cache-dedupe')
Package provides named ESM export; CommonJS users must use `const { createCache } = require('async-cache-dedupe')`.
createCache (CJS)
const { createCache } = require('async-cache-dedupe')
const createCache = require('async-cache-dedupe')
CommonJS destructure is required; package does not export via module.exports directly.
Cache type
import type { Cache } from 'async-cache-dedupe'
TypeScript users can import the Cache type for type annotations.

Creates a memory cache with 5 second TTL and stale window, defines an async function, and calls it with deduplication.

import { createCache } from 'async-cache-dedupe'; const cache = createCache({ ttl: 5, stale: 5, storage: { type: 'memory' } }); cache.define('fetchSomething', async (key: number) => { console.log('query', key); return { key }; }); const result = await cache.fetchSomething(42); console.log(result); // { key: 42 }
Debug
Known issues
breakingVersion 3.0.0 removed the default export; now only named exports are provided.
fix
Replace `import createCache from 'async-cache-dedupe'` with `import { createCache } from 'async-cache-dedupe'`. For CommonJS, use `const { createCache } = require('async-cache-dedupe')`.
affects: >=3.0.0
breakingIn version 3.0.0, the `opts` parameter for `cache.define` now accepts `ttl`, `stale`, and other options; these were previously part of the createCache config only.
fix
Move per-function TTL/stale options to the `define` call: `cache.define('fn', { ttl: 10 }, async (...args) => {...})`.
affects: >=3.0.0
deprecatedThe `onDedupe`, `onError`, `onHit`, `onMiss` callbacks are deprecated in favor of using `pino` logger integration.
fix
Use the `log` option in storage configuration with a pino-compatible logger instance.
affects: >=3.0.0
breakingVersion 2.x required a different storage format; the `storage` option now expects an object with `type` and `options`.
fix
Update storage config from `{ type: 'redis', client: ... }` to `{ type: 'redis', options: { client: ..., invalidation: ... } }`.
affects: >=3.0.0
gotchaThe cache key is computed using `safe-stable-stringify` on arguments. Mutating arguments after a call will not affect the key but may lead to stale data if the argument object content changes.
fix
Ensure arguments passed to cached functions are immutable or use primitive types for keys.
affects: all
gotchaWhen using Redis storage, the `client` option must be an `ioredis` client; other Redis clients may not work correctly.
fix
Install `ioredis` and create a client with `new Redis()`.
affects: all
deprecatedThe `size` option for memory storage was removed in v3. Use `max` instead.
fix
Replace `storage.options.size` with `storage.options.max`.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: createCache is not a function
Importing default export instead of named export in ESM or CommonJS.
fix
Use `import { createCache } from 'async-cache-dedupe'` or `const { createCache } = require('async-cache-dedupe')`.
Error: storage.type must be memory or redis
Providing storage option with an incorrect type or format.
fix
Set storage object as `{ type: 'memory' }` or `{ type: 'redis', options: { client: ... } }`.
Error: Redis client is required for redis storage
Missing `client` option in redis storage config.
fix
Add a valid ioredis client: `{ type: 'redis', options: { client: new (require('ioredis'))() } }`.
TypeError: cache.define is not a function
Calling `define` on the cache before it is fully created or using an incorrect reference.
fix
Ensure `createCache` returns an object and you call `define` on it: `const cache = createCache(...); cache.define(...)`.
Error: Invalid transformer: must have serialize and deserialize methods
Providing a transformer object missing required methods.
fix
Ensure transformer has both `serialize` and `deserialize` functions.
Upgrade
Version history
3.4.0latest on npm
Audit
Dependencies
safe-stable-stringifyrequiredUsed for deterministic cache key generation from arguments.
pinooptionalOptional logger interface for cache events (hit, miss, error).
Agent activity
46 hits · last 30 days
node
42
Resources