Skip to content

Commit

Permalink
fix: use internal cache implementation
Browse files Browse the repository at this point in the history
This change removes the dependency on the `lru-cache` package. It is
replaced by a comparable, but much simpler class in this package.
  • Loading branch information
mbtools authored and wraithgar committed May 2, 2024
1 parent 3fabe4d commit ad8ff11
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
4 changes: 2 additions & 2 deletions classes/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ class Range {

module.exports = Range

const LRU = require('lru-cache')
const cache = new LRU({ max: 1000 })
const LRU = require('../internal/lrucache')
const cache = new LRU()

const parseOptions = require('../internal/parse-options')
const Comparator = require('./comparator')
Expand Down
45 changes: 45 additions & 0 deletions internal/lrucache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class LRUCache {
constructor () {
this.max = 1000
this.map = new Map()
}

get (key) {
const value = this.map.get(key)
if (value === undefined) {
return undefined
} else {
// Remove the key from the map and add it to the end
this.map.delete(key)
this.map.set(key, value)
return value
}
}

delete (key) {
if (this.map.has(key)) {
this.map.delete(key)
return true
} else {
return false
}
}

set (key, value) {
const deleted = this.delete(key)

if (!deleted && value !== undefined) {
// If cache is full, delete the least recently used item
if (this.map.size >= this.max) {
const firstKey = this.map.keys().next().value
this.delete(firstKey)
}

this.map.set(key, value)
}

return this
}
}

module.exports = LRUCache
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
"engines": {
"node": ">=10"
},
"dependencies": {
"lru-cache": "^6.0.0"
},
"author": "GitHub Inc.",
"templateOSS": {
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
Expand Down
10 changes: 10 additions & 0 deletions test/classes/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,13 @@ test('missing range parameter in range intersect', (t) => {
'throws type error')
t.end()
})

test('cache', (t) => {
const cached = Symbol('cached')
const r1 = new Range('1.0.0')
r1.set[0][cached] = true
const r2 = new Range('1.0.0')
t.equal(r1.set[0][cached], true)
t.equal(r2.set[0][cached], true) // Will be true, showing it's cached.
t.end()
})
19 changes: 19 additions & 0 deletions test/internal/lrucache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { test } = require('tap')
const LRUCache = require('../../internal/lrucache')

test('basic cache operation', t => {
const c = new LRUCache()
const max = 1000

for (let i = 0; i < max; i++) {
t.equal(c.set(i, i), c)
}
for (let i = 0; i < max; i++) {
t.equal(c.get(i), i)
}
c.set(1001, 1001)
// lru item should be gone
t.equal(c.get(0), undefined)
c.set(42, undefined)
t.end()
})

0 comments on commit ad8ff11

Please sign in to comment.