@@ -4,7 +4,7 @@ import { CacheSPI } from '../cache-spi';
44import { Metrics } from '../metrics/metrics' ;
55import { CountMinSketch } from './sketch' ;
66
7- import { ON_REMOVE , ON_EVICT , TRIGGER_REMOVE , EVICT } from '../symbols' ;
7+ import { ON_REMOVE , ON_MAINTENANCE , TRIGGER_REMOVE , MAINTENANCE } from '../symbols' ;
88
99import { RemovalReason } from '../removal-reason' ;
1010import { CacheNode } from '../cache-node' ;
@@ -60,9 +60,9 @@ interface BoundedCacheData<K extends KeyType, V> {
6060 sketch : CountMinSketch ;
6161 sketchGrowLimit : number ;
6262
63- evictionTimeout : any ;
63+ maintenanceTimeout : any ;
6464 forceEvictionLimit : number ;
65- evictionInterval : number ;
65+ maintenanceInterval : number ;
6666
6767 window : CacheSection < K , V > ;
6868 protected : CacheSection < K , V > ;
@@ -89,7 +89,7 @@ export class BoundedCache<K extends KeyType, V> extends AbstractCache<K, V> impl
8989 private [ DATA ] : BoundedCacheData < K , V > ;
9090
9191 public [ ON_REMOVE ] ?: RemovalListener < K , V > ;
92- public [ ON_EVICT ] ?: ( ) => void ;
92+ public [ ON_MAINTENANCE ] ?: ( ) => void ;
9393
9494 constructor ( options : BoundedCacheOptions < K , V > ) {
9595 super ( ) ;
@@ -105,6 +105,8 @@ export class BoundedCache<K extends KeyType, V> extends AbstractCache<K, V> impl
105105 */
106106 const sketchWidth = options . weigher ? 256 : Math . max ( options . maxSize , 128 ) ;
107107
108+ this [ MAINTENANCE ] = this [ MAINTENANCE ] . bind ( this ) ;
109+
108110 this [ DATA ] = {
109111 maxSize : options . weigher ? - 1 : options . maxSize ,
110112 removalListener : options . removalListener || null ,
@@ -137,12 +139,12 @@ export class BoundedCache<K extends KeyType, V> extends AbstractCache<K, V> impl
137139 head : new BoundedNode < K , V > ( null , null ) ,
138140 } ,
139141
140- // Timeout used to schedule evictions
141- evictionTimeout : 0 ,
142+ // Timeout used to schedule maintenance
143+ maintenanceTimeout : null ,
142144 // The maximum size we can temporarily be grow before an eviction is forced
143145 forceEvictionLimit : options . maxSize + Math . max ( Math . floor ( options . maxSize * percentOverflow ) , 5 ) ,
144146 // The time to wait before an eviction is triggered by a set
145- evictionInterval : 5000
147+ maintenanceInterval : 5000
146148 } ;
147149 }
148150
@@ -221,9 +223,9 @@ export class BoundedCache<K extends KeyType, V> extends AbstractCache<K, V> impl
221223
222224 // Schedule eviction
223225 if ( data . weightedSize >= data . forceEvictionLimit ) {
224- this [ EVICT ] ( ) ;
225- } else if ( ! data . evictionTimeout ) {
226- data . evictionTimeout = setTimeout ( ( ) => this [ EVICT ] ( ) , data . evictionInterval ) ;
226+ this [ MAINTENANCE ] ( ) ;
227+ } else if ( ! data . maintenanceTimeout ) {
228+ data . maintenanceTimeout = setTimeout ( this [ MAINTENANCE ] , data . maintenanceInterval ) ;
227229 }
228230
229231 // Return the value we replaced
@@ -320,8 +322,8 @@ export class BoundedCache<K extends KeyType, V> extends AbstractCache<K, V> impl
320322
321323 this [ TRIGGER_REMOVE ] ( key , node . value , RemovalReason . EXPLICIT ) ;
322324
323- if ( ! data . evictionTimeout ) {
324- data . evictionTimeout = setTimeout ( ( ) => this [ EVICT ] ( ) , data . evictionInterval ) ;
325+ if ( ! data . maintenanceTimeout ) {
326+ data . maintenanceTimeout = setTimeout ( this [ MAINTENANCE ] , data . maintenanceInterval ) ;
325327 }
326328
327329 return node . value ;
@@ -356,19 +358,19 @@ export class BoundedCache<K extends KeyType, V> extends AbstractCache<K, V> impl
356358 data . protected . head . remove ( ) ;
357359 data . protected . size = 0 ;
358360
359- if ( data . evictionTimeout ) {
360- clearTimeout ( data . evictionTimeout ) ;
361- data . evictionTimeout = null ;
361+ if ( data . maintenanceTimeout ) {
362+ clearTimeout ( data . maintenanceTimeout ) ;
363+ data . maintenanceTimeout = null ;
362364 }
363365 }
364366
365367 public keys ( ) : K [ ] {
366- this [ EVICT ] ( ) ;
368+ this [ MAINTENANCE ] ( ) ;
367369 return Array . from ( this [ DATA ] . values . keys ( ) ) ;
368370 }
369371
370372 public cleanUp ( ) {
371- this [ EVICT ] ( ) ;
373+ this [ MAINTENANCE ] ( ) ;
372374 }
373375
374376 get metrics ( ) : Metrics {
@@ -390,7 +392,20 @@ export class BoundedCache<K extends KeyType, V> extends AbstractCache<K, V> impl
390392 }
391393 }
392394
393- private [ EVICT ] ( ) {
395+ private [ MAINTENANCE ] ( ) {
396+ /*
397+ * Trigger the onMaintenance listener if one exists. This is done
398+ * before eviction occurs so that extra layers have a chance to
399+ * apply their own eviction rules.
400+ *
401+ * This can be things such as things being removed because they have
402+ * been expired which in turn might cause eviction to be unnecessary.
403+ */
404+ const onMaintenance = this [ ON_MAINTENANCE ] ;
405+ if ( onMaintenance ) {
406+ onMaintenance ( ) ;
407+ }
408+
394409 const data = this [ DATA ] ;
395410
396411 /*
@@ -455,15 +470,9 @@ export class BoundedCache<K extends KeyType, V> extends AbstractCache<K, V> impl
455470 this [ TRIGGER_REMOVE ] ( toRemove . key , toRemove . value , RemovalReason . SIZE ) ;
456471 }
457472
458- // Trigger the onEvict listener if one exists
459- const onEvict = this [ ON_EVICT ] ;
460- if ( onEvict ) {
461- onEvict ( ) ;
462- }
463-
464- if ( data . evictionTimeout ) {
465- clearTimeout ( data . evictionTimeout ) ;
466- data . evictionTimeout = null ;
473+ if ( data . maintenanceTimeout ) {
474+ clearTimeout ( data . maintenanceTimeout ) ;
475+ data . maintenanceTimeout = null ;
467476 }
468477 }
469478}
0 commit comments