Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: isaacs/node-lru-cache
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v7.2.0
Choose a base ref
...
head repository: isaacs/node-lru-cache
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v7.2.1
Choose a head ref
  • 4 commits
  • 6 files changed
  • 1 contributor

Commits on Feb 9, 2022

  1. Copy the full SHA
    873a22e View commit details
  2. test for pop()

    isaacs committed Feb 9, 2022
    Copy the full SHA
    38dde3f View commit details
  3. add publishConfig tag

    isaacs committed Feb 9, 2022
    Copy the full SHA
    75d54ba View commit details
  4. 7.2.1

    isaacs committed Feb 9, 2022
    Copy the full SHA
    f01a2da View commit details
Showing with 87 additions and 4 deletions.
  1. +3 −1 index.js
  2. +2 −2 package-lock.json
  3. +4 −1 package.json
  4. +11 −0 test/basic.js
  5. +53 −0 test/load-check.js
  6. +14 −0 test/pop.js
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -119,6 +119,7 @@ class LRUCache {
this.head = 0
this.tail = 0
this.free = new Stack(max)
this.initialFill = 1
this.size = 0

if (typeof dispose === 'function') {
@@ -391,7 +392,7 @@ class LRUCache {
return this.free.pop()
}
// initial fill, just keep writing down the list
return this.tail + 1
return this.initialFill++
}

pop () {
@@ -516,6 +517,7 @@ class LRUCache {
}
this.head = 0
this.tail = 0
this.initialFill = 1
this.free.length = 0
this.calculatedSize = 0
this.size = 0
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name": "lru-cache",
"description": "A cache object that deletes the least-recently-used items.",
"version": "7.2.0",
"version": "7.2.1",
"publishConfig": {
"tag": "v7.2-backport"
},
"author": "Isaac Z. Schlueter <i@izs.me>",
"keywords": [
"mru",
11 changes: 11 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -130,3 +130,14 @@ t.test('peek does not disturb order', t => {
t.strictSame([...c.values()], [4, 3, 2, 1, 0])
t.end()
})

t.test('re-use key before initial fill completed', t => {
const c = new LRU({ max: 5 })
c.set(0, 0)
c.set(1, 1)
c.set(2, 2)
c.set(1, 2)
c.set(3, 3)
t.same([...c.entries()], [ [ 3, 3 ], [ 1, 2 ], [ 2, 2 ], [ 0, 0 ] ])
t.end()
})
53 changes: 53 additions & 0 deletions test/load-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
process.env.TAP_BAIL = '1'
const t = require('tap')
const LRU = require('../')
const max = 10000
const cache = new LRU({ max })

const crypto = require('crypto')
const getVal = () => [
crypto.randomBytes(12).toString('hex'),
crypto.randomBytes(12).toString('hex'),
crypto.randomBytes(12).toString('hex'),
crypto.randomBytes(12).toString('hex'),
]

const seeds = new Array(max * 3)
// fill up the cache to start
for (let i = 0; i < max * 3; i++) {
const v = getVal()
seeds[i] = [v.join(':'), v]
}
t.pass('generated seed data')

const verifyCache = () => {
// walk down the internal list ensuring that every key is the key to that
// index in the keyMap, and the value matches.
for (const [k, i] of (cache.map || cache.keyMap).entries()) {
const v = cache.valList[i]
const key = cache.keyList[i]
if (k !== key) {
t.equal(k, key, 'key at proper index', { k, i })
}
if (v.join(':') !== k) {
t.equal(k, v.join(':'), 'proper value at index', { v, i })
}
}
}

let cycles = 0
const cycleLength = Math.floor(max / 100)
while (cycles < max * 5) {
const r = Math.floor(Math.random() * seeds.length)
const seed = seeds[r]
const v = cache.get(seed[0])
if (v === undefined) {
cache.set(seed[0], seed[1])
} else {
t.equal(v.join(':'), seed[0], 'correct get ' + cycles, { seed, v })
}
if (++cycles % cycleLength === 0) {
verifyCache()
t.pass('cycle check ' + cycles)
}
}
14 changes: 14 additions & 0 deletions test/pop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const t = require('tap')
const LRU = require('../')
const cache = new LRU({ max: 5 })
for (let i = 0; i < 5; i++) {
cache.set(i, i)
}
cache.get(2)
const popped = []
let p
do {
p = cache.pop()
popped.push(p)
} while (p !== undefined)
t.same(popped, [0, 1, 3, 4, 2, undefined])