@@ -452,31 +452,38 @@ class LRUCache {
452452 this . #setItemTTL = ( index , ttl , start = this . #perf. now ( ) ) => {
453453 starts [ index ] = ttl !== 0 ? start : 0 ;
454454 ttls [ index ] = ttl ;
455- // clear out the purge timer if we're setting TTL to 0, and
456- // previously had a ttl purge timer running, so it doesn't
457- // fire unnecessarily.
458- if ( purgeTimers ?. [ index ] ) {
459- clearTimeout ( purgeTimers [ index ] ) ;
460- purgeTimers [ index ] = undefined ;
461- }
462- if ( ttl !== 0 && purgeTimers ) {
463- const t = setTimeout ( ( ) => {
464- if ( this . #isStale( index ) ) {
465- this . #delete( this . #keyList[ index ] , 'expire' ) ;
466- }
467- } , ttl + 1 ) ;
468- // unref() not supported on all platforms
469- /* c8 ignore start */
470- if ( t . unref ) {
471- t . unref ( ) ;
472- }
473- /* c8 ignore stop */
474- purgeTimers [ index ] = t ;
475- }
455+ setPurgetTimer ( index , ttl ) ;
476456 } ;
477457 this . #updateItemAge = index => {
478458 starts [ index ] = ttls [ index ] !== 0 ? this . #perf. now ( ) : 0 ;
459+ setPurgetTimer ( index , ttls [ index ] ) ;
479460 } ;
461+ // clear out the purge timer if we're setting TTL to 0, and
462+ // previously had a ttl purge timer running, so it doesn't
463+ // fire unnecessarily. Don't need to do this if we're not doing
464+ // autopurge.
465+ const setPurgetTimer = ! this . ttlAutopurge ?
466+ ( ) => { }
467+ : ( index , ttl ) => {
468+ if ( purgeTimers ?. [ index ] ) {
469+ clearTimeout ( purgeTimers [ index ] ) ;
470+ purgeTimers [ index ] = undefined ;
471+ }
472+ if ( ttl && ttl !== 0 && purgeTimers ) {
473+ const t = setTimeout ( ( ) => {
474+ if ( this . #isStale( index ) ) {
475+ this . #delete( this . #keyList[ index ] , 'expire' ) ;
476+ }
477+ } , ttl + 1 ) ;
478+ // unref() not supported on all platforms
479+ /* c8 ignore start */
480+ if ( t . unref ) {
481+ t . unref ( ) ;
482+ }
483+ /* c8 ignore stop */
484+ purgeTimers [ index ] = t ;
485+ }
486+ } ;
480487 this . #statusTTL = ( status , index ) => {
481488 if ( ttls [ index ] ) {
482489 const ttl = ttls [ index ] ;
@@ -1219,8 +1226,7 @@ class LRUCache {
12191226 if ( this . #valList[ index ] === p ) {
12201227 // if we allow stale on fetch rejections, then we need to ensure that
12211228 // the stale value is not removed from the cache when the fetch fails.
1222- const del = ! noDelete ||
1223- ! proceed && bf . __staleWhileFetching === undefined ;
1229+ const del = ! noDelete || ( ! proceed && bf . __staleWhileFetching === undefined ) ;
12241230 if ( del ) {
12251231 this . #delete( k , 'fetch' ) ;
12261232 }
0 commit comments