Commit 8b817b0
authored
feat(ramps-controller): add request caching with TTL, deduplication, and abort support (#7536)
## Explanation
**Current state:** The RampsController has a simple `updateGeolocation`
method that fetches geolocation from the RampsService and stores it in
state. There's no caching, deduplication, or request lifecycle
management. When multiple UI components need the same data, each
triggers a separate API call, leading to redundant network requests and
inconsistent loading states across the app.
**Solution:** This PR introduces a request caching infrastructure to
RampsController that:
- **Caches responses with TTL** - Requests are cached by a key (method +
params) with configurable time-to-live (default 15 minutes). Subsequent
calls return cached data if still valid.
- **Deduplicates concurrent requests** - If multiple callers request the
same data simultaneously, they share a single in-flight promise rather
than making duplicate API calls.
- **Supports abort** - In-flight requests can be aborted by cache key,
useful for cleanup on component unmount.
- **Tracks request state** - Each cached request stores its status
(idle/loading/success/error), data, error message, and timestamps. This
state is exposed via Redux for UI consumption.
- **Evicts oldest entries** - Cache is capped at a configurable max size
(default 250), evicting oldest entries when full.
The `updateGeolocation` method now uses this infrastructure, serving as
the first implementation and a pattern for future controller methods.
**Non-obvious details:**
- The `requests` state field is marked `persist: false` since request
cache should not survive app restarts.
- `RequestState.status` uses a template literal type (``
`${RequestStatus}` ``) rather than the enum directly to satisfy the
controller's `StateConstraint` JSON compatibility requirement.
- Type assertions are used in `#updateRequestState` to work around
immer's `Draft` type causing deep instantiation errors—this pattern
exists elsewhere in the codebase.
## References
- Related mobile PR:
MetaMask/metamask-mobile#24129
## Checklist
- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've communicated my changes to consumers by [updating changelogs
for packages I've
changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs)
- [ ] I've introduced [breaking
changes](https://github.com/MetaMask/core/tree/main/docs/breaking-changes.md)
in this PR and have prepared draft pull requests for clients and
consumer packages to resolve them
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Introduces a request caching system with TTL, deduplication, aborting,
and eviction, and updates RampsController (incl. updateGeolocation) to
use it with new state and APIs.
>
> - **RampsController**:
> - Add `requests` cache to state (non-persisted) and metadata; reset on
init.
> - New options: `requestCacheTTL`, `requestCacheMaxSize`.
> - New APIs: `executeRequest` (cached/deduped with TTL, error
handling), `abortRequest`, `getRequestState`.
> - Cache eviction by oldest `timestamp` when exceeding
`requestCacheMaxSize`.
> - Update `updateGeolocation` to use caching via `createCacheKey` and
return `string`.
> - **Caching Infrastructure** (`src/RequestCache.ts`):
> - Add `RequestStatus`, `RequestState`, cache utils (`createCacheKey`,
`isCacheExpired`, `createLoadingState`, `createSuccessState`,
`createErrorState`), defaults (`DEFAULT_REQUEST_CACHE_TTL`,
`DEFAULT_REQUEST_CACHE_MAX_SIZE`).
> - **Exports** (`src/index.ts`):
> - Re-export new controller options/APIs and caching types/utilities.
> - **Tests**:
> - Add comprehensive tests for controller caching behavior, abort flow,
eviction, and cache utils.
> - **Changelog**:
> - Note addition of request caching with TTL, deduplication, and abort
support.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
a7497e4. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent c17a0d8 commit 8b817b0
File tree
6 files changed
+925
-12
lines changed- packages/ramps-controller
- src
6 files changed
+925
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
10 | 14 | | |
11 | 15 | | |
12 | 16 | | |
| |||
0 commit comments