@@ -198,11 +198,17 @@ export class Cache extends EventEmitter implements ICache {
198198 * @returns The fetched or cached value
199199 */
200200 fetch < T > ( key : Key , ttlOrValue : number | string | ( ( ) => T ) | T , value ?: ( ( ) => T ) | T ) : T {
201- // check if cache is hit
202- if ( this . has ( key ) ) {
203- return this . get < T > ( key ) as T
201+ // Inline cache hit check to avoid double lookup
202+ const keyStr = typeof key === 'string' ? key : key . toString ( )
203+ const data = this . data [ keyStr ]
204+
205+ if ( data !== undefined && ( data . t === 0 || data . t >= Date . now ( ) ) ) {
206+ this . stats . hits ++
207+ return this . options . useClones ? fastClone ( data . v ) : data . v
204208 }
205209
210+ this . stats . misses ++
211+
206212 let ttl : number | string | undefined
207213 let valueToFetch : ( ( ) => T ) | T
208214
@@ -313,29 +319,29 @@ export class Cache extends EventEmitter implements ICache {
313319 const keysArray = Array . isArray ( keys ) ? keys : [ keys ]
314320
315321 let delCount = 0
316- for ( const key of keysArray ) {
322+ for ( let i = 0 , len = keysArray . length ; i < len ; i ++ ) {
323+ const key = keysArray [ i ]
324+
317325 // handle invalid key types
318326 const err = this . _isInvalidKey ( key )
319327 if ( err )
320328 throw err
321329
322- const keyStr = key . toString ( )
330+ const keyStr = typeof key === 'string' ? key : key . toString ( )
323331 // only delete if existent
324- if ( this . data [ keyStr ] ) {
325- // calc the stats
326- this . stats . vsize -= this . _getValLength ( this . _unwrap ( this . data [ keyStr ] , false ) )
327- this . stats . ksize -= this . _getKeyLength ( key )
332+ const data = this . data [ keyStr ]
333+ if ( data ) {
334+ // calc the stats - inline for performance
335+ this . stats . vsize -= this . _getValLength ( data . v )
336+ this . stats . ksize -= keyStr . length
328337 this . stats . keys --
329338 delCount ++
330339
331- // save old value for event
332- const oldVal = this . data [ keyStr ]
333-
334340 // delete the value
335341 delete this . data [ keyStr ]
336342
337343 // emit delete event
338- this . emit ( 'del' , key , oldVal . v )
344+ this . emit ( 'del' , key , data . v )
339345 }
340346 }
341347
@@ -575,29 +581,27 @@ export class Cache extends EventEmitter implements ICache {
575581 const now = Date . now ( )
576582 let livetime = 0
577583
578- const ttlMultiplicator = 1000
579-
580584 // use given ttl
581585 if ( ttl === 0 ) {
582586 livetime = 0
583587 }
584588 else if ( ttl ) {
585- livetime = now + ( ttl * ttlMultiplicator )
589+ livetime = now + ( ttl * 1000 )
586590 }
587591 else {
588592 // use standard ttl
589593 if ( this . options . stdTTL === 0 ) {
590594 livetime = this . options . stdTTL as number
591595 }
592596 else {
593- livetime = now + ( ( this . options . stdTTL as number ) * ttlMultiplicator )
597+ livetime = now + ( ( this . options . stdTTL as number ) * 1000 )
594598 }
595599 }
596600
597601 // return the wrapped value
598602 return {
599603 t : livetime ,
600- v : asClone ? clone ( value ) : value ,
604+ v : asClone ? fastClone ( value ) : value ,
601605 }
602606 }
603607
@@ -610,7 +614,7 @@ export class Cache extends EventEmitter implements ICache {
610614 }
611615
612616 if ( value . v !== undefined ) {
613- return asClone ? clone ( value . v ) : value . v
617+ return asClone ? fastClone ( value . v ) : value . v
614618 }
615619
616620 return null as any
0 commit comments