@@ -78,27 +78,15 @@ export function clearCache() {
78
78
resourceSpecificCacheStores . clear ( ) ;
79
79
}
80
80
81
- export class InMemoryInterpreterSpecificCache < T > {
82
- private readonly resource : Resource ;
83
- private readonly args : any [ ] ;
84
- constructor (
85
- private readonly keyPrefix : string ,
86
- protected readonly expiryDurationMs : number ,
87
- args : [ Uri | undefined , ...any [ ] ] ,
88
- private readonly vscode : VSCodeType = require ( 'vscode' )
89
- ) {
90
- this . resource = args [ 0 ] ;
91
- this . args = args . slice ( 1 ) ;
81
+ export class InMemoryCache < T > {
82
+ private readonly _store = new Map < string , CacheData > ( ) ;
83
+ protected get store ( ) : Map < string , CacheData > {
84
+ return this . _store ;
92
85
}
86
+ constructor ( protected readonly expiryDurationMs : number , protected readonly cacheKey : string = '' ) { }
93
87
public get hasData ( ) {
94
- const store = getCacheStore ( this . resource , this . vscode ) ;
95
- const key = getCacheKeyFromFunctionArgs ( this . keyPrefix , this . args ) ;
96
- const data = store . get ( key ) ;
97
- if ( ! store . has ( key ) || ! data ) {
98
- return false ;
99
- }
100
- if ( this . hasExpired ( data . expiry ) ) {
101
- store . delete ( key ) ;
88
+ if ( ! this . store . get ( this . cacheKey ) || this . hasExpired ( this . store . get ( this . cacheKey ) ! . expiry ) ) {
89
+ this . store . delete ( this . cacheKey ) ;
102
90
return false ;
103
91
}
104
92
return true ;
@@ -107,33 +95,24 @@ export class InMemoryInterpreterSpecificCache<T> {
107
95
* Returns undefined if there is no data.
108
96
* Uses `hasData` to determine whether any cached data exists.
109
97
*
98
+ * @readonly
110
99
* @type {(T | undefined) }
111
- * @memberof InMemoryInterpreterSpecificCache
100
+ * @memberof InMemoryCache
112
101
*/
113
102
public get data ( ) : T | undefined {
114
- if ( ! this . hasData ) {
103
+ if ( ! this . hasData || ! this . store . has ( this . cacheKey ) ) {
115
104
return ;
116
105
}
117
- const store = getCacheStore ( this . resource , this . vscode ) ;
118
- const key = getCacheKeyFromFunctionArgs ( this . keyPrefix , this . args ) ;
119
- const data = store . get ( key ) ;
120
- if ( ! store . has ( key ) || ! data ) {
121
- return ;
122
- }
123
- return data . value as T ;
106
+ return this . store . get ( this . cacheKey ) ?. value as T ;
124
107
}
125
108
public set data ( value : T | undefined ) {
126
- const store = getCacheStore ( this . resource , this . vscode ) ;
127
- const key = getCacheKeyFromFunctionArgs ( this . keyPrefix , this . args ) ;
128
- store . set ( key , {
109
+ this . store . set ( this . cacheKey , {
129
110
expiry : this . calculateExpiry ( ) ,
130
111
value
131
112
} ) ;
132
113
}
133
114
public clear ( ) {
134
- const store = getCacheStore ( this . resource , this . vscode ) ;
135
- const key = getCacheKeyFromFunctionArgs ( this . keyPrefix , this . args ) ;
136
- store . delete ( key ) ;
115
+ this . store . clear ( ) ;
137
116
}
138
117
139
118
/**
@@ -144,7 +123,7 @@ export class InMemoryInterpreterSpecificCache<T> {
144
123
* @returns true if the data expired, false otherwise.
145
124
*/
146
125
protected hasExpired ( expiry : number ) : boolean {
147
- return expiry < Date . now ( ) ;
126
+ return expiry <= Date . now ( ) ;
148
127
}
149
128
150
129
/**
@@ -158,58 +137,13 @@ export class InMemoryInterpreterSpecificCache<T> {
158
137
}
159
138
}
160
139
161
- export class InMemoryCache < T > {
162
- private readonly _data : { data ?: T ; hasData : boolean ; expiry ?: number } = { hasData : false } ;
163
- constructor ( protected readonly expiryDurationMs : number ) { }
164
- public get hasData ( ) {
165
- if ( ! this . _data . hasData || ! this . _data . expiry || this . hasExpired ( this . _data . expiry ) ) {
166
- this . _data . data = undefined ;
167
- this . _data . expiry = undefined ;
168
- this . _data . hasData = false ;
169
- return false ;
170
- }
171
- return true ;
172
- }
173
- /**
174
- * Returns undefined if there is no data.
175
- * Uses `hasData` to determine whether any cached data exists.
176
- *
177
- * @readonly
178
- * @type {(T | undefined) }
179
- * @memberof InMemoryCache
180
- */
181
- public get data ( ) : T | undefined {
182
- return this . hasData ? this . _data . data : undefined ;
183
- }
184
- public set data ( value : T | undefined ) {
185
- this . _data . expiry = this . calculateExpiry ( ) ;
186
- this . _data . data = value ;
187
- this . _data . hasData = true ;
188
- }
189
- public clear ( ) {
190
- this . _data . data = undefined ;
191
- this . _data . expiry = undefined ;
192
- this . _data . hasData = false ;
193
- }
194
-
195
- /**
196
- * Has this data expired?
197
- * (protected class member to allow for reliable non-data-time-based testing)
198
- *
199
- * @param expiry The date to be tested for expiry.
200
- * @returns true if the data expired, false otherwise.
201
- */
202
- protected hasExpired ( expiry : number ) : boolean {
203
- return expiry <= Date . now ( ) ;
140
+ export class InMemoryInterpreterSpecificCache < T > extends InMemoryCache < T > {
141
+ private readonly resource : Resource ;
142
+ protected get store ( ) {
143
+ return getCacheStore ( this . resource , this . vscode ) ;
204
144
}
205
-
206
- /**
207
- * When should this data item expire?
208
- * (protected class method to allow for reliable non-data-time-based testing)
209
- *
210
- * @returns number representing the expiry time for this item.
211
- */
212
- protected calculateExpiry ( ) : number {
213
- return Date . now ( ) + this . expiryDurationMs ;
145
+ constructor ( keyPrefix : string , expiryDurationMs : number , args : [ Uri | undefined , ...any [ ] ] , private readonly vscode : VSCodeType = require ( 'vscode' ) ) {
146
+ super ( expiryDurationMs , getCacheKeyFromFunctionArgs ( keyPrefix , args . slice ( 1 ) ) ) ;
147
+ this . resource = args [ 0 ] ;
214
148
}
215
149
}
0 commit comments