@@ -35,6 +35,7 @@ export class NodeCacheStore<T> extends Hookified {
3535 private _useClones : boolean ;
3636 private _deleteOnExpire : boolean ;
3737 private _keys = new Set < string > ( ) ;
38+ private _values = new Map < string , T > ( ) ;
3839 private _ttls = new Map < string , number > ( ) ;
3940 private _intervalId : number | NodeJS . Timeout = 0 ;
4041
@@ -128,19 +129,17 @@ export class NodeCacheStore<T> extends Hookified {
128129 const finalTtl = this . resolveTtl ( ttl ) ;
129130 const valueToStore = this . _useClones ? this . clone ( value ) : value ;
130131
131- const isOverwrite = this . _keys . has ( keyValue ) ;
132+ const isOverwrite = this . _values . has ( keyValue ) ;
132133 this . _keys . add ( keyValue ) ;
133134 if ( isOverwrite ) {
134- const oldValue = await this . _keyv . get ( keyValue ) ;
135+ const oldValue = this . _values . get ( keyValue ) as T ;
135136 this . _stats . decreaseKSize ( keyValue ) ;
136- if ( oldValue !== undefined ) {
137- this . _stats . decreaseVSize ( oldValue ) ;
138- }
139-
137+ this . _stats . decreaseVSize ( oldValue ) ;
140138 this . _stats . decreaseCount ( ) ;
141139 }
142140
143141 await this . _keyv . set ( keyValue , valueToStore as T , finalTtl ) ;
142+ this . _values . set ( keyValue , value ) ;
144143
145144 this . trackExpiration ( keyValue , finalTtl ) ;
146145
@@ -221,13 +220,15 @@ export class NodeCacheStore<T> extends Hookified {
221220 */
222221 public async del ( key : string | number ) : Promise < boolean > {
223222 const keyValue = key . toString ( ) ;
224- const value = await this . _keyv . get ( keyValue ) ;
223+ const hadValue = this . _values . has ( keyValue ) ;
224+ const value = hadValue ? ( this . _values . get ( keyValue ) as T ) : undefined ;
225225 const result = await this . _keyv . delete ( keyValue ) ;
226226 if ( result ) {
227227 this . _keys . delete ( keyValue ) ;
228+ this . _values . delete ( keyValue ) ;
228229 this . _ttls . delete ( keyValue ) ;
229230 this . _stats . decreaseKSize ( keyValue ) ;
230- if ( value !== undefined ) {
231+ if ( hadValue ) {
231232 this . _stats . decreaseVSize ( value ) ;
232233 }
233234
@@ -256,6 +257,7 @@ export class NodeCacheStore<T> extends Hookified {
256257 public async clear ( ) : Promise < void > {
257258 await this . _keyv . clear ( ) ;
258259 this . _keys . clear ( ) ;
260+ this . _values . clear ( ) ;
259261 this . _ttls . clear ( ) ;
260262 this . _stats . resetStoreValues ( ) ;
261263 }
@@ -267,6 +269,7 @@ export class NodeCacheStore<T> extends Hookified {
267269 public async flushAll ( ) : Promise < void > {
268270 await this . _keyv . clear ( ) ;
269271 this . _keys . clear ( ) ;
272+ this . _values . clear ( ) ;
270273 this . _ttls . clear ( ) ;
271274 this . _stats = new Stats ( { enabled : true } ) ;
272275 this . emit ( "flush" ) ;
@@ -283,8 +286,8 @@ export class NodeCacheStore<T> extends Hookified {
283286 ttl ?: number | string ,
284287 ) : Promise < boolean > {
285288 const keyValue = key . toString ( ) ;
286- const item = await this . _keyv . get ( keyValue ) ;
287- if ( item !== undefined ) {
289+ if ( this . _values . has ( keyValue ) ) {
290+ const item = this . _values . get ( keyValue ) as T ;
288291 const finalTtl = this . resolveTtl ( ttl ) ;
289292 await this . _keyv . set ( keyValue , item , finalTtl ) ;
290293 this . trackExpiration ( keyValue , finalTtl ) ;
@@ -361,9 +364,10 @@ export class NodeCacheStore<T> extends Hookified {
361364 }
362365
363366 const result = await this . _keyv . get < V > ( keyValue ) ;
364- if ( result !== undefined ) {
367+ if ( this . _values . has ( keyValue ) ) {
365368 await this . _keyv . delete ( keyValue ) ;
366369 this . _keys . delete ( keyValue ) ;
370+ this . _values . delete ( keyValue ) ;
367371 this . _ttls . delete ( keyValue ) ;
368372 this . _stats . incrementHits ( ) ;
369373 this . _stats . decreaseKSize ( keyValue ) ;
@@ -490,7 +494,8 @@ export class NodeCacheStore<T> extends Hookified {
490494 return ;
491495 }
492496
493- const value = await this . _keyv . get ( key ) ;
497+ const hadValue = this . _values . has ( key ) ;
498+ const value = hadValue ? this . _values . get ( key ) : undefined ;
494499
495500 /* v8 ignore next 3 -- @preserve: race condition guard when key is refreshed during async get */
496501 if ( ! this . isExpired ( key ) ) {
@@ -502,11 +507,11 @@ export class NodeCacheStore<T> extends Hookified {
502507 if ( this . _deleteOnExpire ) {
503508 await this . _keyv . delete ( key ) ;
504509 this . _keys . delete ( key ) ;
510+ this . _values . delete ( key ) ;
505511 this . _ttls . delete ( key ) ;
506512 if ( wasTracked ) {
507513 this . _stats . decreaseKSize ( key ) ;
508- /* v8 ignore next 3 -- @preserve: value is typically undefined when Keyv also expires the item */
509- if ( value !== undefined ) {
514+ if ( hadValue ) {
510515 this . _stats . decreaseVSize ( value ) ;
511516 }
512517
0 commit comments