From e2fcf91535d0ea790fac79cc42ce75499ea61875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mai=CC=81ra=20Bello?= Date: Mon, 30 Oct 2023 15:02:15 -0300 Subject: [PATCH 1/5] Add a new protected method to LRUDiskCache for building the LRU data --- src/caches/LRUDiskCache.ts | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/caches/LRUDiskCache.ts b/src/caches/LRUDiskCache.ts index 63ae7fd79..4835b3719 100644 --- a/src/caches/LRUDiskCache.ts +++ b/src/caches/LRUDiskCache.ts @@ -5,6 +5,9 @@ import { outputJSON, readJSON, remove } from 'fs-extra' import LRU from 'lru-cache' import { join } from 'path' import ReadWriteLock from 'rwlock' +import { LocalCacheOptions } from '../HttpClient' + +export type LRUData = Record & { timeOfDeath: number } export class LRUDiskCache implements CacheLayer{ @@ -12,7 +15,7 @@ export class LRUDiskCache implements CacheLayer{ private disposed: number private hits = 0 private total = 0 - private lruStorage: LRU + private lruStorage: LRU private keyToBeDeleted: string constructor(private cachePath: string, options: LRUDiskCacheOptions, private readFile=readJSON, private writeFile=outputJSON) { @@ -33,8 +36,17 @@ export class LRUDiskCache implements CacheLayer{ noDisposeOnSet: true, } - this.lruStorage = new LRU(lruOptions) + this.lruStorage = new LRU(lruOptions) + + } + /** + * Builds the data object that will be stored at the LRU memory storage. + * Subclasses that need to store more than just the time of death should + * override this. + */ + protected buildLruData(timeOfDeath: number, localCacheOptions?: LocalCacheOptions): LRUData { + return { timeOfDeath } } public has = (key: string): boolean => this.lruStorage.has(key) @@ -57,9 +69,9 @@ export class LRUDiskCache implements CacheLayer{ } public get = async (key: string): Promise => { - const timeOfDeath = this.lruStorage.get(key) + const lruData = this.lruStorage.get(key) this.total += 1 - if (timeOfDeath === undefined) { + if (lruData === undefined) { // if it is an outdated file when stale=false if (this.keyToBeDeleted) { @@ -85,7 +97,7 @@ export class LRUDiskCache implements CacheLayer{ }) // if it is an outdated file when stale=true - if (timeOfDeath < Date.now()) { + if (lruData.timeOfDeath < Date.now()) { this.lruStorage.del(key) await this.deleteFile(key) } @@ -93,15 +105,10 @@ export class LRUDiskCache implements CacheLayer{ return data } - public set = async (key: string, value: V, maxAge?: number): Promise => { - let timeOfDeath = NaN - if (maxAge) { - timeOfDeath = maxAge + Date.now() - this.lruStorage.set(key, timeOfDeath, maxAge) - } - else { - this.lruStorage.set(key, NaN) - } + public set = async (key: string, value: V, maxAge?: number, localCacheOptions?: LocalCacheOptions): Promise => { + let timeOfDeath = maxAge ? maxAge + Date.now() : NaN + const lruData = this.buildLruData(timeOfDeath, localCacheOptions) + this.lruStorage.set(key, lruData, maxAge ? maxAge : undefined) if (this.keyToBeDeleted && this.keyToBeDeleted !== key) { await this.deleteFile(this.keyToBeDeleted) From 41453723fb60b0cddec84e6f68d16114b649007d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mai=CC=81ra=20Bello?= Date: Wed, 1 Nov 2023 10:45:12 -0300 Subject: [PATCH 2/5] Allow overriding functions of LRUDiskCache by subclasses --- src/caches/LRUDiskCache.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/caches/LRUDiskCache.ts b/src/caches/LRUDiskCache.ts index 4835b3719..f3895d9fb 100644 --- a/src/caches/LRUDiskCache.ts +++ b/src/caches/LRUDiskCache.ts @@ -45,11 +45,17 @@ export class LRUDiskCache implements CacheLayer{ * Subclasses that need to store more than just the time of death should * override this. */ - protected buildLruData(timeOfDeath: number, localCacheOptions?: LocalCacheOptions): LRUData { + protected buildLRUData(timeOfDeath: number, localCacheOptions?: LocalCacheOptions): LRUData { return { timeOfDeath } } - public has = (key: string): boolean => this.lruStorage.has(key) + protected getLRU() { + return this.lruStorage + } + + public has (key: string): boolean { + return this.lruStorage.has(key) + } public getStats = (name='disk-lru-cache'): LRUStats => { const stats = { @@ -68,7 +74,7 @@ export class LRUDiskCache implements CacheLayer{ return stats } - public get = async (key: string): Promise => { + public async get (key: string): Promise { const lruData = this.lruStorage.get(key) this.total += 1 if (lruData === undefined) { @@ -105,9 +111,9 @@ export class LRUDiskCache implements CacheLayer{ return data } - public set = async (key: string, value: V, maxAge?: number, localCacheOptions?: LocalCacheOptions): Promise => { + public async set (key: string, value: V, maxAge?: number, localCacheOptions?: LocalCacheOptions): Promise { let timeOfDeath = maxAge ? maxAge + Date.now() : NaN - const lruData = this.buildLruData(timeOfDeath, localCacheOptions) + const lruData = this.buildLRUData(timeOfDeath, localCacheOptions) this.lruStorage.set(key, lruData, maxAge ? maxAge : undefined) if (this.keyToBeDeleted && this.keyToBeDeleted !== key) { From 5c5983ecca7f5511deb53032e190ffa799f031da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mai=CC=81ra=20Bello?= Date: Fri, 3 Nov 2023 13:18:04 -0300 Subject: [PATCH 3/5] Allow accessing stats instance variables from subclasses --- src/caches/LRUDiskCache.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/caches/LRUDiskCache.ts b/src/caches/LRUDiskCache.ts index f3895d9fb..2dd57b0c2 100644 --- a/src/caches/LRUDiskCache.ts +++ b/src/caches/LRUDiskCache.ts @@ -12,9 +12,9 @@ export type LRUData = Record & { timeOfDeath: number } export class LRUDiskCache implements CacheLayer{ private lock: ReadWriteLock - private disposed: number - private hits = 0 - private total = 0 + protected disposed: number + protected hits = 0 + protected total = 0 private lruStorage: LRU private keyToBeDeleted: string From c867b0f561529da05cc5b359bbcdcbda062ffa91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mai=CC=81ra=20Bello?= Date: Wed, 8 Nov 2023 10:56:59 -0300 Subject: [PATCH 4/5] Fix lint --- src/caches/LRUDiskCache.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/caches/LRUDiskCache.ts b/src/caches/LRUDiskCache.ts index 2dd57b0c2..c55873066 100644 --- a/src/caches/LRUDiskCache.ts +++ b/src/caches/LRUDiskCache.ts @@ -11,10 +11,10 @@ export type LRUData = Record & { timeOfDeath: number } export class LRUDiskCache implements CacheLayer{ - private lock: ReadWriteLock protected disposed: number protected hits = 0 protected total = 0 + private lock: ReadWriteLock private lruStorage: LRU private keyToBeDeleted: string @@ -40,19 +40,6 @@ export class LRUDiskCache implements CacheLayer{ } - /** - * Builds the data object that will be stored at the LRU memory storage. - * Subclasses that need to store more than just the time of death should - * override this. - */ - protected buildLRUData(timeOfDeath: number, localCacheOptions?: LocalCacheOptions): LRUData { - return { timeOfDeath } - } - - protected getLRU() { - return this.lruStorage - } - public has (key: string): boolean { return this.lruStorage.has(key) } @@ -112,7 +99,7 @@ export class LRUDiskCache implements CacheLayer{ } public async set (key: string, value: V, maxAge?: number, localCacheOptions?: LocalCacheOptions): Promise { - let timeOfDeath = maxAge ? maxAge + Date.now() : NaN + const timeOfDeath = maxAge ? maxAge + Date.now() : NaN const lruData = this.buildLRUData(timeOfDeath, localCacheOptions) this.lruStorage.set(key, lruData, maxAge ? maxAge : undefined) @@ -137,6 +124,19 @@ export class LRUDiskCache implements CacheLayer{ return !failure } + /** + * Builds the data object that will be stored at the LRU memory storage. + * Subclasses that need to store more than just the time of death should + * override this. + */ + protected buildLRUData(timeOfDeath: number, localCacheOptions?: LocalCacheOptions): LRUData { + return { timeOfDeath } + } + + protected getLRU() { + return this.lruStorage + } + private getPathKey = (key: string): string => { return join(this.cachePath, key) } From 9c1ca534d667468132d64cd7486bcfebbb633483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mai=CC=81ra=20Bello?= Date: Wed, 8 Nov 2023 11:02:32 -0300 Subject: [PATCH 5/5] Add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d08b31da..fce6fa5d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] ### Added +- Change LRUDiskCache to allow subclasses to override functionality - Create new optional request option for customizing local cache behavior - Allow passing request config to VBase's getJSON method