Skip to content

Commit

Permalink
dispose stale entries on clear()
Browse files Browse the repository at this point in the history
Fix: #203
  • Loading branch information
isaacs committed Mar 5, 2022
1 parent da932f7 commit 31d439e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
26 changes: 10 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ class LRUCache {
removeItemSize (index) {}
addItemSize (index, v, k, size, sizeCalculation) {}

*indexes () {
*indexes ({ allowStale = this.allowStale } = {}) {
if (this.size) {
for (let i = this.tail; true; i = this.prev[i]) {
if (!this.isStale(i)) {
if (allowStale || !this.isStale(i)) {
yield i
}
if (i === this.head) {
Expand All @@ -251,10 +251,10 @@ class LRUCache {
}
}
}
*rindexes () {
*rindexes ({ allowStale = this.allowStale } = {}) {
if (this.size) {
for (let i = this.head; true; i = this.next[i]) {
if (!this.isStale(i)) {
if (allowStale || !this.isStale(i)) {
yield i
}
if (i === this.tail) {
Expand Down Expand Up @@ -313,16 +313,10 @@ class LRUCache {

purgeStale () {
let deleted = false
if (this.size) {
for (let i = this.head; true; i = this.next[i]) {
const b = i === this.tail
if (this.isStale(i)) {
this.delete(this.keyList[i])
deleted = true
}
if (b) {
break
}
for (const i of this.rindexes({ allowStale: true })) {
if (this.isStale(i)) {
this.delete(this.keyList[i])
deleted = true
}
}
return deleted
Expand Down Expand Up @@ -542,12 +536,12 @@ class LRUCache {

clear () {
if (this.dispose !== LRUCache.prototype.dispose) {
for (const index of this.rindexes()) {
for (const index of this.rindexes({ allowStale: true })) {
this.dispose(this.valList[index], this.keyList[index], 'delete')
}
}
if (this.disposeAfter) {
for (const index of this.rindexes()) {
for (const index of this.rindexes({ allowStale: true })) {
this.disposed.push([this.valList[index], this.keyList[index], 'delete'])
}
}
Expand Down
65 changes: 65 additions & 0 deletions test/ttl.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,71 @@ const runTests = (LRU, t) => {
t.end()
})

// https://github.com/isaacs/node-lru-cache/issues/203
t.test('indexes/rindexes can walk over stale entries', t => {
const c = new LRU({ max: 10, ttl: 10 })
for (let i = 0; i < 3; i++) {
c.set(i, i)
}
clock.advance(9)
for (let i = 3; i < 10; i++) {
c.set(i, i)
}
c.get(1)
c.get(3)
clock.advance(9)
const indexes = [...c.indexes()]
const indexesStale = [...c.indexes({ allowStale: true })]
const rindexes = [...c.rindexes()]
const rindexesStale = [...c.rindexes({ allowStale: true })]
t.same({
indexes,
indexesStale,
rindexes,
rindexesStale,
}, {
indexes: [
3, 9, 8, 7,
6, 5, 4
],
indexesStale: [
3, 1, 9, 8, 7,
6, 5, 4, 2, 0
],
rindexes: [
4, 5, 6, 7,
8, 9, 3
],
rindexesStale: [
0, 2, 4, 5, 6,
7, 8, 9, 1, 3
]
})
t.end()
})

// https://github.com/isaacs/node-lru-cache/issues/203
t.test('clear() disposes stale entries', t => {
const disposed = []
const disposedAfter = []
const c = new LRU({
max: 3,
ttl: 10,
dispose: (v, k) => disposed.push([v, k]),
disposeAfter: (v, k) => disposedAfter.push([v, k]),
})
for (let i = 0; i < 4; i++) {
c.set(i, i)
}
t.same(disposed, [[0, 0]])
t.same(disposedAfter, [[0, 0]])
clock.advance(20)
c.clear()
t.same(disposed, [[0, 0], [1, 1], [2, 2], [3, 3]])
t.same(disposedAfter, [[0, 0], [1, 1], [2, 2], [3, 3]])
t.end()
})

t.end()
}

Expand Down

0 comments on commit 31d439e

Please sign in to comment.