From c6b75d2311891c53a6cfca475a2f57672d4c32aa Mon Sep 17 00:00:00 2001 From: Daniel Zanzini Date: Tue, 6 Jun 2023 16:09:15 -0300 Subject: [PATCH 1/2] Simplifies logic of LRUDiskCache --- CHANGELOG.md | 9 ++++++++- src/caches/LRUDiskCache.ts | 20 ++------------------ 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bf4b8f9b..1dba40a68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Changed + +- Simplifies logic of LRUDiskCache. + ## [6.45.15] - 2023-02-01 + ### Changed + - Removed unnecessary worker signal logs ## [6.45.14] - 2023-02-01 @@ -23,7 +29,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Do not return empty content if no conflict is found. ## [6.45.13] - 2022-12-12 + ### Changed + - Deterministic cache for getWithBody - Remove socket metrics per origin @@ -1756,7 +1764,6 @@ instead - `HttpClient` now adds `'Accept-Encoding': 'gzip'` header by default. - [Unreleased]: https://github.com/vtex/node-vtex-api/compare/v6.45.15...HEAD [6.45.15]: https://github.com/vtex/node-vtex-api/compare/v6.45.14...v6.45.15 [6.45.14]: https://github.com/vtex/node-vtex-api/compare/v6.45.13...v6.45.14 diff --git a/src/caches/LRUDiskCache.ts b/src/caches/LRUDiskCache.ts index 63ae7fd79..9de795b70 100644 --- a/src/caches/LRUDiskCache.ts +++ b/src/caches/LRUDiskCache.ts @@ -13,18 +13,16 @@ export class LRUDiskCache implements CacheLayer{ private hits = 0 private total = 0 private lruStorage: LRU - private keyToBeDeleted: string constructor(private cachePath: string, options: LRUDiskCacheOptions, private readFile=readJSON, private writeFile=outputJSON) { this.hits = 0 this.total = 0 this.disposed = 0 - this.keyToBeDeleted = '' this.lock = new ReadWriteLock() const dispose = (key: string): void => { - this.keyToBeDeleted = key this.disposed += 1 + this.deleteFile(key) } const lruOptions = { @@ -59,18 +57,10 @@ export class LRUDiskCache implements CacheLayer{ public get = async (key: string): Promise => { const timeOfDeath = this.lruStorage.get(key) this.total += 1 - if (timeOfDeath === undefined) { - - // if it is an outdated file when stale=false - if (this.keyToBeDeleted) { - await this.deleteFile(key) - } - return undefined - } const pathKey = this.getPathKey(key) - const data = await new Promise(resolve => { + const data = await new Promise(resolve => { this.lock.readLock(key, async (release: () => void) => { try { const fileData = await this.readFile(pathKey) @@ -87,7 +77,6 @@ export class LRUDiskCache implements CacheLayer{ // if it is an outdated file when stale=true if (timeOfDeath < Date.now()) { this.lruStorage.del(key) - await this.deleteFile(key) } return data @@ -103,10 +92,6 @@ export class LRUDiskCache implements CacheLayer{ this.lruStorage.set(key, NaN) } - if (this.keyToBeDeleted && this.keyToBeDeleted !== key) { - await this.deleteFile(this.keyToBeDeleted) - } - const pathKey = this.getPathKey(key) const failure = await new Promise(resolve => { this.lock.writeLock(key, async (release: () => void) => { @@ -130,7 +115,6 @@ export class LRUDiskCache implements CacheLayer{ private deleteFile = async (key: string): Promise => { const pathKey = this.getPathKey(key) - this.keyToBeDeleted = '' const failure = new Promise(resolve => { this.lock.writeLock(key, async (release: () => void) => { try { From ecff6888ad1b38b6a3f6e5664474f394630ae9a8 Mon Sep 17 00:00:00 2001 From: Daniel Zanzini Date: Tue, 6 Jun 2023 16:39:30 -0300 Subject: [PATCH 2/2] Adds verification of variable --- src/caches/LRUDiskCache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/caches/LRUDiskCache.ts b/src/caches/LRUDiskCache.ts index 9de795b70..7241a7ac2 100644 --- a/src/caches/LRUDiskCache.ts +++ b/src/caches/LRUDiskCache.ts @@ -75,7 +75,7 @@ export class LRUDiskCache implements CacheLayer{ }) // if it is an outdated file when stale=true - if (timeOfDeath < Date.now()) { + if (timeOfDeath && timeOfDeath < Date.now()) { this.lruStorage.del(key) }