Skip to content

Commit

Permalink
fix: purgeStale lockup on list reordering
Browse files Browse the repository at this point in the history
Fix: ##209
  • Loading branch information
isaacs committed Mar 10, 2022
1 parent 04765f8 commit 212804e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,16 @@ class LRUCache {

purgeStale () {
let deleted = false
const toDelete = []
for (const i of this.rindexes({ allowStale: true })) {
if (this.isStale(i)) {
this.delete(this.keyList[i])
toDelete.push(this.keyList[i])
deleted = true
}
}
for (const k of toDelete) {
this.delete(k)
}
return deleted
}

Expand Down
17 changes: 17 additions & 0 deletions test/ttl.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,23 @@ const runTests = (LRU, t) => {
t.end()
})

t.test('purgeStale() lockup', t => {
const c = new LRU({
max: 3,
ttl: 10,
updateAgeOnGet: true,
})
c.set(1, 1)
c.set(2, 2)
c.set(3, 3)
clock.advance(5)
c.get(2)
clock.advance(15)
c.purgeStale()
t.pass('did not get locked up')
t.end()
})

t.end()
}

Expand Down

0 comments on commit 212804e

Please sign in to comment.