Skip to content

Commit bf2a84e

Browse files
authored
Merge pull request #549 from vtex/perf/removeUnusedDiskWrite
Stop updating client cache when revalidated and still expired
2 parents 99186f4 + 90feaf3 commit bf2a84e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Changed
11+
- Stop updating client cache when revalidated and still expired
12+
1013
## [6.45.24] - 2023-10-05
1114
### Added
1215

1316
- Allow disabling memoization for all requests of a client
17+
1418
## [6.45.23] - 2023-10-04
1519

1620
### Fixed

src/HttpClient/middlewares/cache.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,16 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
198198
? (data as Buffer).toString(responseEncoding)
199199
: data
200200

201-
const expiration = Date.now() + (maxAge - currentAge) * 1000
201+
const now = Date.now()
202+
const expiration = now + (maxAge - currentAge) * 1000
203+
204+
const alreadyExpired = expiration <= now
205+
const reusingRevalidatedCache = cached && (ctx.response === cached.response)
206+
const shouldSkipCacheUpdate = alreadyExpired && reusingRevalidatedCache
207+
if (shouldSkipCacheUpdate) {
208+
return
209+
}
210+
202211
await storage.set(setKey, {
203212
etag,
204213
expiration,

0 commit comments

Comments
 (0)