Skip to content

Add opt-in client-side API caching for safe GET requests - #11

Merged
Micfinch merged 5 commits into
mainfrom
copilot/implement-api-caching-layer
Sep 27, 2026
Merged

Micfinch merged 5 commits into
mainfrom
copilot/implement-api-caching-layer

Conversation

Copilot AI commented Sep 21, 2026 •

Copy link
Copy Markdown

This adds a client-side caching layer to the Axios-based Mempool client without changing default behavior: caching remains off unless the caller explicitly enables it. The implementation is scoped to safe/idempotent GETs, isolates cache entries by client configuration, and avoids caching authorization-bearing requests unless the caller deliberately opts into an isolated store.

  • Cache model

    • Adds cache configuration to mempoolJS(...) with:
      • enabled
      • ttlMs (number or function)
      • maxEntries
      • store
      • allowAuthorizedRequests
      • shouldCache
    • Keeps caching disabled by default.
    • Exposes client.cache.clear() only when caching is enabled.
  • Storage + isolation

    • Adds a bounded in-memory default store for browser/Node usage.
    • Adds mempoolJS.createMemoryCacheStore() for explicit isolated stores.
    • Keys are normalized and include client/baseURL context so separate hosts/networks/configurations do not share entries accidentally.
    • Absolute URLs are also isolated from each other when using shared stores.
  • Request behavior

    • Caches only GET requests.
    • Never caches POST/mutation requests.
    • Coalesces concurrent identical GET misses into a single upstream request.
    • Applies TTL expiry and evicts old entries from the bounded memory store.
    • Does not cache failures.
    • Preserves Axios response shape for cache hits, including request.
  • Security guards

    • Skips caching by default for requests carrying authorization, cookie, proxy-authorization, set-cookie, x-api-key, or auth.
    • Allows those requests only when the caller explicitly enables them and provides a store configuration intended for isolation.
    • Omits sensitive header values from cache keys.
  • Docs

    • Updates README.md with cache configuration examples, TTL guidance, limitations, and security notes.

Example:

import mempoolJS from '@mempool/mempool.js';

const client = mempoolJS({
  hostname: 'mempool.space',
  cache: {
    enabled: true,
    ttlMs: 30_000,
    maxEntries: 100,
  },
});

const tx = await client.bitcoin.transactions.getTx({ txid: '...' });

await client.cache?.clear();

Copilot AI and others added 2 commits September 21, 2026 22:49
Co-authored-by: Micfinch <142565130+Micfinch@users.noreply.github.com>
Co-authored-by: Micfinch <142565130+Micfinch@users.noreply.github.com>
Copilot AI changed the title [WIP] Add opt-in API caching layer for Mempool client Add opt-in client-side API caching for safe GET requests Sep 21, 2026
Copilot AI requested a review from Micfinch September 21, 2026 22:56
@Micfinch
Micfinch marked this pull request as ready for review September 23, 2026 14:39
@Micfinch

Copy link
Copy Markdown
Owner

@copilot please fix the merge conflicts in this pull request.

…-caching-layer

# Conflicts:
#	src/index.ts

Co-authored-by: Micfinch <142565130+Micfinch@users.noreply.github.com>

Copilot AI commented Sep 23, 2026

Copy link
Copy Markdown
Author

@copilot please fix the merge conflicts in this pull request.

Fixed in 8d661ff. I merged the latest main into this branch, resolved the src/index.ts conflict, and validated the merged result with the build plus targeted cache/default/address tests.

@Micfinch

Copy link
Copy Markdown
Owner

@copilot please fix the merge conflicts in this pull request.

@Micfinch

Copy link
Copy Markdown
Owner

@copilot please fix the merge conflicts in this pull request.

…-caching-layer

# Conflicts:
#	src/services/api/index.ts

Co-authored-by: Micfinch <142565130+Micfinch@users.noreply.github.com>
@Micfinch

Copy link
Copy Markdown
Owner

@copilot please fix the merge conflicts in this pull request.

Copilot AI commented Sep 27, 2026

Copy link
Copy Markdown
Author

@copilot please fix the merge conflicts in this pull request.

Fixed in e504801. I merged the latest main into this branch, resolved the src/services/api/index.ts conflict, and validated the merged result with the build plus targeted cache/default/address checks.

Copilot AI commented Sep 27, 2026 •

Copy link
Copy Markdown
Author

@copilot please fix the merge conflicts in this pull request.

The pull request is currently mergeable and I do not see any remaining merge conflicts to resolve.

@copilot please fix the merge conflicts in this pull request.

The pull request is currently mergeable and I do not see any remaining merge conflicts to resolve.

@Micfinch
Micfinch merged commit 559810f into main Sep 27, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants