generated from apollographql/typescript-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace infinite memoization with concurrent deduplication (#100)
RESTDataSource contained a memoization cache (separate from the HTTP-header-sensitive shared cache) which caches all GET requests forever. This wasn't even documented for the first four years of RESTDataSource's existence and led to a lot of confusion. In this new major version, this cache will instead by default only de-duplicate *concurrent* GET requests. This also introduces `deduplicationPolicyFor()` which lets you tune how de-duplication works, including restoring v4 semantics (see the changeset for code snippets). Fixes #40. Fixes #72. Fixes #73. Fixes #46. Fixes #39 (though it "introduces" #102). Somewhat addresses #65 but only for the de-duplication cache (we should still allow direct control over the cache key for the HTTP). Co-authored-by: Trevor Scheer <trevor.scheer@gmail.com>
- Loading branch information
1 parent
4a249ec
commit 2e51657
Showing
4 changed files
with
341 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
'@apollo/datasource-rest': major | ||
--- | ||
|
||
Instead of memoizing GET requests forever in memory, only apply de-duplication during the lifetime of the original request. Replace the `memoizeGetRequests` field with a `requestDeduplicationPolicyFor()` method to determine how de-duplication works per request. | ||
|
||
To restore the surprising infinite-unconditional-cache behavior of previous versions, use this implementation of `requestDeduplicationPolicyFor()` (which replaces `deduplicate-during-request-lifetime` with `deduplicate-until-invalidated`): | ||
|
||
```ts | ||
override protected requestDeduplicationPolicyFor( | ||
url: URL, | ||
request: RequestOptions, | ||
): RequestDeduplicationPolicy { | ||
const cacheKey = this.cacheKeyFor(url, request); | ||
if (request.method === 'GET') { | ||
return { | ||
policy: 'deduplicate-until-invalidated', | ||
deduplicationKey: `${request.method} ${cacheKey}`, | ||
}; | ||
} else { | ||
return { | ||
policy: 'do-not-deduplicate', | ||
invalidateDeduplicationKeys: [`GET ${cacheKey}`], | ||
}; | ||
} | ||
} | ||
``` | ||
|
||
To restore the behavior of `memoizeGetRequests = false`, use this implementation of `requestDeduplicationPolicyFor()`: | ||
|
||
```ts | ||
protected override requestDeduplicationPolicyFor() { | ||
return { policy: 'do-not-deduplicate' } as const; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.