Skip to content

Commit

Permalink
Merge pull request #549 from vtex/perf/removeUnusedDiskWrite
Browse files Browse the repository at this point in the history
Stop updating client cache when revalidated and still expired
  • Loading branch information
filafb authored Oct 25, 2023
2 parents 99186f4 + 90feaf3 commit bf2a84e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed
- Stop updating client cache when revalidated and still expired

## [6.45.24] - 2023-10-05
### Added

- Allow disabling memoization for all requests of a client

## [6.45.23] - 2023-10-04

### Fixed
Expand Down
11 changes: 10 additions & 1 deletion src/HttpClient/middlewares/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,16 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => {
? (data as Buffer).toString(responseEncoding)
: data

const expiration = Date.now() + (maxAge - currentAge) * 1000
const now = Date.now()
const expiration = now + (maxAge - currentAge) * 1000

const alreadyExpired = expiration <= now
const reusingRevalidatedCache = cached && (ctx.response === cached.response)
const shouldSkipCacheUpdate = alreadyExpired && reusingRevalidatedCache
if (shouldSkipCacheUpdate) {
return
}

await storage.set(setKey, {
etag,
expiration,
Expand Down

0 comments on commit bf2a84e

Please sign in to comment.