# mcollina/async-cache-dedupe architecture

> Architecture diagram of the GitHub repository mcollina/async-cache-dedupe, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mcollina/async-cache-dedupe
- Repository: https://github.com/mcollina/async-cache-dedupe

## Overview

#### 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`](https://github.com/mcollina/async-cache-dedupe/blob/main/index.js) (Package entry), [`src/cache.js`](https://github.com/mcollina/async-cache-dedupe/blob/main/src/cache.js) (Cache orchestrator), [`src/storage/index.js`](https://github.com/mcollina/async-cache-dedupe/blob/main/src/storage/index.js) (Storage factory), [`src/storage/interface.js`](https://github.com/mcollina/async-cache-dedupe/blob/main/src/storage/interface.js) (Storage contract)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public API"]
  node_package_entry["Package entry<br/>CommonJS entry<br/>[index.js]"]
  node_type_declarations["Type declarations<br/>TypeScript API<br/>[index.d.ts]"]
end

subgraph group_core["Cache Engine"]
  node_cache["Cache orchestrator<br/>cache engine<br/>[cache.js]"]
  node_resolver_api["Dynamic resolvers<br/>resolver surface<br/>[cache.js]"]
  node_key_policy["Keys and policies<br/>policy layer<br/>[util.js]"]
  node_inflight["In-flight dedupe<br/>promise coordination<br/>[cache.js]"]
  node_expiration["Hit, stale, and refresh<br/>cache lifecycle<br/>[cache.js]"]
  node_references["Reference invalidation<br/>invalidation flow<br/>[cache.js]"]
  node_symbols["Internal symbols<br/>internal state<br/>[symbol.js]"]
end

subgraph group_storage["Storage Backends"]
  node_storage_factory{{"Storage factory<br/>backend selection<br/>[index.js]"}}
  node_storage_contract["Storage contract<br/>async interface<br/>[interface.js]"]
  node_memory_storage[("Memory storage<br/>local backend<br/>[memory.js]")]
  node_redis_storage[("Redis storage<br/>distributed backend<br/>[redis.js]")]
  node_redis_fixture["Redis fixture<br/>Docker service<br/>[docker-compose.yml]"]
end

subgraph group_runtime["Runtime Targets"]
  node_node_consumers(("Node consumers<br/>runtime target<br/>[index.js]"))
  node_browser_consumers(("Browser and worker consumers<br/>runtime target<br/>[index.html]"))
  node_cloudflare_fixture["Worker portability fixture<br/>runtime fixture<br/>[worker.mjs]"]
end

node_package_entry -->|"exports"| node_cache
node_package_entry -->|"exports"| node_storage_factory
node_type_declarations -.->|"declares"| node_package_entry
node_cache -->|"defines"| node_resolver_api
node_resolver_api -->|"serializes args"| node_key_policy
node_resolver_api -->|"invokes"| node_inflight
node_inflight -->|"on cache lookup"| node_expiration
node_key_policy -->|"supplies TTL and stale policy"| node_expiration
node_expiration -->|"get, set, refresh"| node_storage_contract
node_cache -.->|"uses"| node_symbols
node_cache -->|"records fetch references"| node_references
node_references -->|"invalidates entries"| node_storage_contract
node_storage_factory -->|"requires"| node_storage_contract
node_storage_factory -->|"default backend"| node_memory_storage
node_storage_factory -->|"selects"| node_redis_storage
node_redis_storage -.->|"connects to"| node_redis_fixture
node_node_consumers -->|"imports"| node_package_entry
node_browser_consumers -->|"bundles"| node_package_entry
node_browser_consumers -->|"uses"| node_memory_storage
node_cloudflare_fixture -.->|"validates"| node_memory_storage

click node_package_entry "https://github.com/mcollina/async-cache-dedupe/blob/main/index.js"
click node_type_declarations "https://github.com/mcollina/async-cache-dedupe/blob/main/types/index.d.ts"
click node_cache "https://github.com/mcollina/async-cache-dedupe/blob/main/src/cache.js"
click node_resolver_api "https://github.com/mcollina/async-cache-dedupe/blob/main/src/cache.js"
click node_key_policy "https://github.com/mcollina/async-cache-dedupe/blob/main/src/util.js"
click node_inflight "https://github.com/mcollina/async-cache-dedupe/blob/main/src/cache.js"
click node_expiration "https://github.com/mcollina/async-cache-dedupe/blob/main/src/cache.js"
click node_references "https://github.com/mcollina/async-cache-dedupe/blob/main/src/cache.js"
click node_symbols "https://github.com/mcollina/async-cache-dedupe/blob/main/src/symbol.js"
click node_storage_factory "https://github.com/mcollina/async-cache-dedupe/blob/main/src/storage/index.js"
click node_storage_contract "https://github.com/mcollina/async-cache-dedupe/blob/main/src/storage/interface.js"
click node_memory_storage "https://github.com/mcollina/async-cache-dedupe/blob/main/src/storage/memory.js"
click node_redis_storage "https://github.com/mcollina/async-cache-dedupe/blob/main/src/storage/redis.js"
click node_redis_fixture "https://github.com/mcollina/async-cache-dedupe/blob/main/utils/docker-compose.yml"
click node_node_consumers "https://github.com/mcollina/async-cache-dedupe/blob/main/index.js"
click node_browser_consumers "https://github.com/mcollina/async-cache-dedupe/blob/main/test/browser/fixtures/index.html"
click node_cloudflare_fixture "https://github.com/mcollina/async-cache-dedupe/blob/main/test/cloudflare/helpers/worker.mjs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_package_entry,node_type_declarations toneBlue
class node_cache,node_resolver_api,node_key_policy,node_inflight,node_expiration,node_references,node_symbols toneAmber
class node_storage_factory,node_storage_contract,node_memory_storage,node_redis_storage,node_redis_fixture toneMint
class node_node_consumers,node_browser_consumers,node_cloudflare_fixture toneRose
```

## Components

### Public API

- [Package entry](https://github.com/mcollina/async-cache-dedupe/blob/main/index.js): CommonJS entry · Exports cache and storage creation APIs. · `index.js`
- [Type declarations](https://github.com/mcollina/async-cache-dedupe/blob/main/types/index.d.ts): TypeScript API · `types/index.d.ts`

### Cache Engine

- [Cache orchestrator](https://github.com/mcollina/async-cache-dedupe/blob/main/src/cache.js): cache engine · Defines named async resolvers and applies cache policy. · `src/cache.js`
- [Dynamic resolvers](https://github.com/mcollina/async-cache-dedupe/blob/main/src/cache.js): resolver surface · Defined resolvers are exposed as cache[name]. · `src/cache.js`
- [Keys and policies](https://github.com/mcollina/async-cache-dedupe/blob/main/src/util.js): policy layer · Builds stable argument keys and resolves TTL, stale, and transform policies. · `src/util.js`
- [In-flight dedupe](https://github.com/mcollina/async-cache-dedupe/blob/main/src/cache.js): promise coordination · Shares identical calls until their resolver promise settles. · `src/cache.js`
- [Hit, stale, and refresh](https://github.com/mcollina/async-cache-dedupe/blob/main/src/cache.js): cache lifecycle · Serves valid or stale entries and refreshes expired data. · `src/cache.js`
- [Reference invalidation](https://github.com/mcollina/async-cache-dedupe/blob/main/src/cache.js): invalidation flow · Maps result-derived references to cached entries for targeted eviction. · `src/cache.js`
- [Internal symbols](https://github.com/mcollina/async-cache-dedupe/blob/main/src/symbol.js): internal state · `src/symbol.js`

### Storage Backends

- [Storage factory](https://github.com/mcollina/async-cache-dedupe/blob/main/src/storage/index.js): backend selection · Selects built-ins or accepts a custom storage implementation. · `src/storage/index.js`
- [Storage contract](https://github.com/mcollina/async-cache-dedupe/blob/main/src/storage/interface.js): async interface · Defines entry, TTL, refresh, clear, and invalidation operations. · `src/storage/interface.js`
- [Memory storage](https://github.com/mcollina/async-cache-dedupe/blob/main/src/storage/memory.js): local backend · Default browser-compatible bounded per-resolver storage. · `src/storage/memory.js`
- [Redis storage](https://github.com/mcollina/async-cache-dedupe/blob/main/src/storage/redis.js): distributed backend · Uses a caller-owned ioredis-compatible client and reference GC. · `src/storage/redis.js`
- [Redis fixture](https://github.com/mcollina/async-cache-dedupe/blob/main/utils/docker-compose.yml): Docker service · Local Redis integration environment. · `utils/docker-compose.yml`

### Runtime Targets

- [Node consumers](https://github.com/mcollina/async-cache-dedupe/blob/main/index.js): runtime target · Consume CommonJS or ESM package output; Redis is supported here. · `index.js`
- [Browser and worker consumers](https://github.com/mcollina/async-cache-dedupe/blob/main/test/browser/fixtures/index.html): runtime target · Use memory storage only. · `test/browser/fixtures/index.html`
- [Worker portability fixture](https://github.com/mcollina/async-cache-dedupe/blob/main/test/cloudflare/helpers/worker.mjs): runtime fixture · `test/cloudflare/helpers/worker.mjs`

## Connections

- 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
