Skip to content

Commit 1926e04

Browse files
committed
Add a new protected method to LRUDiskCache for building the LRU data
1 parent d9594b4 commit 1926e04

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

src/caches/LRUDiskCache.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import { outputJSON, readJSON, remove } from 'fs-extra'
55
import LRU from 'lru-cache'
66
import { join } from 'path'
77
import ReadWriteLock from 'rwlock'
8+
import { LocalCacheOptions } from '../HttpClient'
9+
10+
export type LRUData = Record<string, unknown> & { timeOfDeath: number }
811

912
export class LRUDiskCache<V> implements CacheLayer<string, V>{
1013

1114
private lock: ReadWriteLock
1215
private disposed: number
1316
private hits = 0
1417
private total = 0
15-
private lruStorage: LRU<string, number>
18+
private lruStorage: LRU<string, LRUData>
1619
private keyToBeDeleted: string
1720

1821
constructor(private cachePath: string, options: LRUDiskCacheOptions, private readFile=readJSON, private writeFile=outputJSON) {
@@ -33,8 +36,17 @@ export class LRUDiskCache<V> implements CacheLayer<string, V>{
3336
noDisposeOnSet: true,
3437
}
3538

36-
this.lruStorage = new LRU<string, number>(lruOptions)
39+
this.lruStorage = new LRU<string, LRUData>(lruOptions)
40+
41+
}
3742

43+
/**
44+
* Builds the data object that will be stored at the LRU memory storage.
45+
* Subclasses that need to store more than just the time of death should
46+
* override this.
47+
*/
48+
protected buildLruData(timeOfDeath: number, localCacheOptions?: LocalCacheOptions): LRUData {
49+
return { timeOfDeath }
3850
}
3951

4052
public has = (key: string): boolean => this.lruStorage.has(key)
@@ -57,9 +69,9 @@ export class LRUDiskCache<V> implements CacheLayer<string, V>{
5769
}
5870

5971
public get = async (key: string): Promise<V | void> => {
60-
const timeOfDeath = this.lruStorage.get(key)
72+
const lruData = this.lruStorage.get(key)
6173
this.total += 1
62-
if (timeOfDeath === undefined) {
74+
if (lruData === undefined) {
6375

6476
// if it is an outdated file when stale=false
6577
if (this.keyToBeDeleted) {
@@ -85,23 +97,18 @@ export class LRUDiskCache<V> implements CacheLayer<string, V>{
8597
})
8698

8799
// if it is an outdated file when stale=true
88-
if (timeOfDeath < Date.now()) {
100+
if (lruData.timeOfDeath < Date.now()) {
89101
this.lruStorage.del(key)
90102
await this.deleteFile(key)
91103
}
92104

93105
return data
94106
}
95107

96-
public set = async (key: string, value: V, maxAge?: number): Promise<boolean> => {
97-
let timeOfDeath = NaN
98-
if (maxAge) {
99-
timeOfDeath = maxAge + Date.now()
100-
this.lruStorage.set(key, timeOfDeath, maxAge)
101-
}
102-
else {
103-
this.lruStorage.set(key, NaN)
104-
}
108+
public set = async (key: string, value: V, maxAge?: number, localCacheOptions?: LocalCacheOptions): Promise<boolean> => {
109+
let timeOfDeath = maxAge ? maxAge + Date.now() : NaN
110+
const lruData = this.buildLruData(timeOfDeath, localCacheOptions)
111+
this.lruStorage.set(key, lruData, maxAge ? maxAge : undefined)
105112

106113
if (this.keyToBeDeleted && this.keyToBeDeleted !== key) {
107114
await this.deleteFile(this.keyToBeDeleted)

0 commit comments

Comments
 (0)