@@ -5,14 +5,17 @@ import { outputJSON, readJSON, remove } from 'fs-extra'
5
5
import LRU from 'lru-cache'
6
6
import { join } from 'path'
7
7
import ReadWriteLock from 'rwlock'
8
+ import { LocalCacheOptions } from '../HttpClient'
9
+
10
+ export type LRUData = Record < string , unknown > & { timeOfDeath : number }
8
11
9
12
export class LRUDiskCache < V > implements CacheLayer < string , V > {
10
13
11
14
private lock : ReadWriteLock
12
15
private disposed : number
13
16
private hits = 0
14
17
private total = 0
15
- private lruStorage : LRU < string , number >
18
+ private lruStorage : LRU < string , LRUData >
16
19
private keyToBeDeleted : string
17
20
18
21
constructor ( private cachePath : string , options : LRUDiskCacheOptions , private readFile = readJSON , private writeFile = outputJSON ) {
@@ -33,8 +36,17 @@ export class LRUDiskCache<V> implements CacheLayer<string, V>{
33
36
noDisposeOnSet : true ,
34
37
}
35
38
36
- this . lruStorage = new LRU < string , number > ( lruOptions )
39
+ this . lruStorage = new LRU < string , LRUData > ( lruOptions )
40
+
41
+ }
37
42
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 }
38
50
}
39
51
40
52
public has = ( key : string ) : boolean => this . lruStorage . has ( key )
@@ -57,9 +69,9 @@ export class LRUDiskCache<V> implements CacheLayer<string, V>{
57
69
}
58
70
59
71
public get = async ( key : string ) : Promise < V | void > => {
60
- const timeOfDeath = this . lruStorage . get ( key )
72
+ const lruData = this . lruStorage . get ( key )
61
73
this . total += 1
62
- if ( timeOfDeath === undefined ) {
74
+ if ( lruData === undefined ) {
63
75
64
76
// if it is an outdated file when stale=false
65
77
if ( this . keyToBeDeleted ) {
@@ -85,23 +97,18 @@ export class LRUDiskCache<V> implements CacheLayer<string, V>{
85
97
} )
86
98
87
99
// if it is an outdated file when stale=true
88
- if ( timeOfDeath < Date . now ( ) ) {
100
+ if ( lruData . timeOfDeath < Date . now ( ) ) {
89
101
this . lruStorage . del ( key )
90
102
await this . deleteFile ( key )
91
103
}
92
104
93
105
return data
94
106
}
95
107
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 )
105
112
106
113
if ( this . keyToBeDeleted && this . keyToBeDeleted !== key ) {
107
114
await this . deleteFile ( this . keyToBeDeleted )
0 commit comments