How mcollina/async-cache-dedupe works
Purpose and Public Entry Points
async-cache-dedupe is a JavaScript library for caching async resolver functions while deduplicating concurrent calls for the same serialized arguments. The package entry point is index.js; public TypeScript declarations are in types/index.d.ts. Primary API surfaces are cache creation/definition and standalone storage creation.
Paths: index.js, types/index.d.ts
Cache Orchestrator
The core cache implementation manages named resolvers dynamically exposed as cache[name]. It merges cache-level and resolver-level policies, serializes arguments into stable keys, coordinates in-flight promises, and triggers hit/miss/dedupe/error hooks. A resolver can have result-dependent TTL/stale policies and a custom argument serializer or result transformer.
Paths: src/cache.js, src/util.js, src/symbol.js
Fetch, Deduplication, and Expiration Flow
A caller invokes a defined resolver with arguments. The cache derives a stable key, reads the configured storage, and:
• returns a valid entry on hit;
• serves stale data while initiating/maintaining refresh behavior when within stale lifetime;
• invokes the original async resolver on miss;
• shares that in-flight promise among identical concurrent requests.
On successful resolution, the result is transformed/serialized and persisted according to TTL; cache-disabled (ttl: 0) calls remain deduplicated only during execution.
Paths: src/cache.js, src/util.js
Storage Boundary and Extension Contract
Storage is an asynchronous abstraction behind cache operations: get/set/remove/clear, TTL and existence checks, refresh, reference invalidation, and Redis GC where supported. The factory selects built-in memory/Redis implementations or accepts a user-supplied implementation conforming to the interface. This boundary isolates cache policy from persistence and enables per-resolver storage selection.
Paths: src/storage/index.js, src/storage/interface.js, src/storage/memory.js
Built-in Persistence Backends
Memory storage is the default, browser-compatible local backend with bounded entries per resolver and optional reference invalidation. Redis storage supports shared/distributed caches through an externally supplied ioredis-compatible client. Redis tracks reference keys with TTL and exposes lazy or strict garbage collection to prune stale reference-to-entry links.
Paths: src/storage/memory.js, src/storage/redis.js, utils/docker-compose.yml
Reference-Based Invalidation and Clearing
Resolvers may derive references from (args, key, result) after a successful fetch. When invalidation is enabled, storage records these references so invalidateAll can remove every related cached entry, including wildcard matches. clear supports global, resolver-scoped, and argument-scoped eviction; this is distinct from TTL expiration and stale refresh behavior.
Paths: src/cache.js, src/storage/interface.js, src/storage/redis.js
Runtime and Distribution Boundaries
The library supports Node.js CommonJS and ESM consumers, plus browser bundles using memory storage only. Redis is server-side and requires a caller-owned client rather than creating network infrastructure itself. Browser compatibility is validated through bundler fixture configurations; Cloudflare-oriented coverage indicates memory storage portability in worker-like runtimes.
Paths: index.js, src/storage/memory.js, test/browser/fixtures/index.html, test/cloudflare/helpers/worker.mjs
Main entry points: index.js Package entry · cache.js Cache orchestrator · storage/index.js Storage factory · interface.js Storage contract
Components 17
Public API
- Package entry · Exports cache and storage creation APIs.
index.js - Type declarations
types/index.d.ts
Cache Engine
- Cache orchestrator · Defines named async resolvers and applies cache policy.
src/cache.js - Dynamic resolvers · Defined resolvers are exposed as cache[name].
src/cache.js - Keys and policies · Builds stable argument keys and resolves TTL, stale, and transform policies.
src/util.js - In-flight dedupe · Shares identical calls until their resolver promise settles.
src/cache.js - Hit, stale, and refresh · Serves valid or stale entries and refreshes expired data.
src/cache.js - Reference invalidation · Maps result-derived references to cached entries for targeted eviction.
src/cache.js - Internal symbols
src/symbol.js
Storage Backends
- Storage factory · Selects built-ins or accepts a custom storage implementation.
src/storage/index.js - Storage contract · Defines entry, TTL, refresh, clear, and invalidation operations.
src/storage/interface.js - Memory storage · Default browser-compatible bounded per-resolver storage.
src/storage/memory.js - Redis storage · Uses a caller-owned ioredis-compatible client and reference GC.
src/storage/redis.js - Redis fixture · Local Redis integration environment.
utils/docker-compose.yml
Runtime Targets
- Node consumers · Consume CommonJS or ESM package output; Redis is supported here.
index.js - Browser and worker consumers · Use memory storage only.
test/browser/fixtures/index.html - Worker portability fixture
test/cloudflare/helpers/worker.mjs
Connections 20
- Package entry → Cache orchestrator: exports
- Package entry → Storage factory: exports
- Type declarations → Package entry: declares
- Cache orchestrator → Dynamic resolvers: defines
- Dynamic resolvers → Keys and policies: serializes args
- Dynamic resolvers → In-flight dedupe: invokes
- In-flight dedupe → Hit, stale, and refresh: on cache lookup
- Keys and policies → Hit, stale, and refresh: supplies TTL and stale policy
- Hit, stale, and refresh → Storage contract: get, set, refresh
- Cache orchestrator → Internal symbols: uses
- Cache orchestrator → Reference invalidation: records fetch references
- Reference invalidation → Storage contract: invalidates entries
- Storage factory → Storage contract: requires
- Storage factory → Memory storage: default backend
- Storage factory → Redis storage: selects
- Redis storage → Redis fixture: connects to
- Node consumers → Package entry: imports
- Browser and worker consumers → Package entry: bundles
- Browser and worker consumers → Memory storage: uses
- Worker portability fixture → Memory storage: validates