Skip to content

Commit 8b817b0

Browse files
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

6 files changed

+925
-12
lines changed

packages/ramps-controller/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add request caching infrastructure with TTL, deduplication, and abort support ([#7536](https://github.com/MetaMask/core/pull/7536))
13+
1014
### Changed
1115

1216
- Bump `@metamask/controller-utils` from `^11.16.0` to `^11.17.0` ([#7534](https://github.com/MetaMask/core/pull/7534))

0 commit comments

Comments
 (0)