track age in arc master
authorJustin Wind <justin.wind+git@gmail.com>
Thu, 10 Jul 2025 23:12:06 +0000 (16:12 -0700)
committerJustin Wind <justin.wind+git@gmail.com>
Thu, 10 Jul 2025 23:12:06 +0000 (16:12 -0700)
lib/arc.js

index f78410924b7e303209da467e5444ab79216736e1..0281878c400b408f54452f4ad1b18564f230a861 100644 (file)
@@ -20,6 +20,7 @@ class ARCache extends EventEmitter {
   frequent; // Frequent LRU
   recentGhost; // Recent history LRU
   frequentGhost; // Frequent historic LRU
+  age; // Tally of total get attempts, for statistics
   eventData; // Object spread over all event data
 
   /**
@@ -85,19 +86,30 @@ class ARCache extends EventEmitter {
       capacity: frequentCapacity,
       evictDestination: this.frequentGhost,
     });
+
+    this.age = 0;
   }
 
 
+  /**
+   * @returns {number} capacity of the cache
+   */
   get capacity() {
     return this._capacity;
   }
 
 
+  /**
+   * @returns {number} capacity of the adaptive cache
+   */
   get adaptiveCapacity() {
     return this._adaptiveCapacity;
   }
 
 
+  /**
+   * @returns {number} entries in the cache
+   */
   get size() {
     return this.recent.size + this.frequent.size + this.recentGhost.size + this.frequentGhost.size;
   }
@@ -109,10 +121,12 @@ class ARCache extends EventEmitter {
    * @param {boolean} isGhost hit from ghost cache
    */
   _hit(entry, isGhost = false) {
+    this.age += 1;
     this.emit('hit', {
       ...this.eventData,
       key: entry.key,
       count: entry.count,
+      age: this.age - entry.ts,
       isGhost,
     });
   }
@@ -123,6 +137,7 @@ class ARCache extends EventEmitter {
    * @param {*} key key
    */
   _miss(key) {
+    this.age += 1;
     this.emit('miss', {
       ...this.eventData,
       key,