Skip to content

Dispose not called on the last stale item in a cache with purgeStale or clear #203

@mkomaiha

Description

@mkomaiha

Clear function gets called in purgeStale when the size of the cache is 1 and the clear dispose only loops rIndexes when disposing so stale items are not disposed. How can I handle the clean up of the last item in the cache if it is stale?

node-lru-cache/index.js

Lines 543 to 553 in da932f7

clear () {
if (this.dispose !== LRUCache.prototype.dispose) {
for (const index of this.rindexes()) {
this.dispose(this.valList[index], this.keyList[index], 'delete')
}
}
if (this.disposeAfter) {
for (const index of this.rindexes()) {
this.disposed.push([this.valList[index], this.keyList[index], 'delete'])
}
}

const LRU = require("lru-cache");
const LRU_OPTIONS = {
  max: 10000,
  ttl: 1000 * 5, // 5 sec ttl
  dispose: (val, key) => {
    console.log("DISPOSE ME", key);
  },
  disposeAfter: (val, key) => {
    console.log("DISPOSE after", key);
  },
};
const LRU_CACHE = new LRU(LRU_OPTIONS);
console.log("Setting TEST");
LRU_CACHE.set("TEST", { msg: "test" });

setTimeout(() => {
  console.log("Ending program");
  LRU_CACHE.clear();
}, 1000 * 10); // Keep program alive for 10 seconds

// Output from code:
// Setting TEST
// Ending program

// Output expected code:
// Setting TEST
// DISPOSE ME TEST
// DISPOSE after TEST
// Ending program

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions