Skip setting to fetch cache when not modified (#66055)

To avoid extra network hops we can compare existing cache entries we've
already fetched and see if the revalidated value matches and if it does
we can avoid sending the set request with the identical data.
This commit is contained in:
JJ Kasper 2024-05-22 13:54:16 -05:00 committed by GitHub
parent ad8d1c27ff
commit b17ca02695
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -297,6 +297,26 @@ export default class FetchCache implements CacheHandler {
public async set(...args: Parameters<CacheHandler['set']>) {
const [key, data, ctx] = args
const newValue = data?.kind === 'FETCH' ? data.data : undefined
const existingCache = memoryCache?.get(key)
const existingValue = existingCache?.value
if (
existingValue?.kind === 'FETCH' &&
Object.keys(existingValue.data).every(
(field) =>
JSON.stringify(
(existingValue.data as Record<string, string | Object>)[field]
) ===
JSON.stringify((newValue as Record<string, string | Object>)[field])
)
) {
if (this.debug) {
console.log(`skipping cache set for ${key} as not modified`)
}
return
}
const { fetchCache, fetchIdx, fetchUrl, tags } = ctx
if (!fetchCache) return