Skip to content

Commit ad8ff11

Browse files
mbtoolswraithgar
authored andcommitted
fix: use internal cache implementation
This change removes the dependency on the `lru-cache` package. It is replaced by a comparable, but much simpler class in this package.
1 parent 3fabe4d commit ad8ff11

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed

classes/range.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ class Range {
198198

199199
module.exports = Range
200200

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

204204
const parseOptions = require('../internal/parse-options')
205205
const Comparator = require('./comparator')

internal/lrucache.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class LRUCache {
2+
constructor () {
3+
this.max = 1000
4+
this.map = new Map()
5+
}
6+
7+
get (key) {
8+
const value = this.map.get(key)
9+
if (value === undefined) {
10+
return undefined
11+
} else {
12+
// Remove the key from the map and add it to the end
13+
this.map.delete(key)
14+
this.map.set(key, value)
15+
return value
16+
}
17+
}
18+
19+
delete (key) {
20+
if (this.map.has(key)) {
21+
this.map.delete(key)
22+
return true
23+
} else {
24+
return false
25+
}
26+
}
27+
28+
set (key, value) {
29+
const deleted = this.delete(key)
30+
31+
if (!deleted && value !== undefined) {
32+
// If cache is full, delete the least recently used item
33+
if (this.map.size >= this.max) {
34+
const firstKey = this.map.keys().next().value
35+
this.delete(firstKey)
36+
}
37+
38+
this.map.set(key, value)
39+
}
40+
41+
return this
42+
}
43+
}
44+
45+
module.exports = LRUCache

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
"engines": {
4949
"node": ">=10"
5050
},
51+
"dependencies": {
52+
"lru-cache": "^6.0.0"
53+
},
5154
"author": "GitHub Inc.",
5255
"templateOSS": {
5356
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",

test/classes/range.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,13 @@ test('missing range parameter in range intersect', (t) => {
105105
'throws type error')
106106
t.end()
107107
})
108+
109+
test('cache', (t) => {
110+
const cached = Symbol('cached')
111+
const r1 = new Range('1.0.0')
112+
r1.set[0][cached] = true
113+
const r2 = new Range('1.0.0')
114+
t.equal(r1.set[0][cached], true)
115+
t.equal(r2.set[0][cached], true) // Will be true, showing it's cached.
116+
t.end()
117+
})

test/internal/lrucache.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { test } = require('tap')
2+
const LRUCache = require('../../internal/lrucache')
3+
4+
test('basic cache operation', t => {
5+
const c = new LRUCache()
6+
const max = 1000
7+
8+
for (let i = 0; i < max; i++) {
9+
t.equal(c.set(i, i), c)
10+
}
11+
for (let i = 0; i < max; i++) {
12+
t.equal(c.get(i), i)
13+
}
14+
c.set(1001, 1001)
15+
// lru item should be gone
16+
t.equal(c.get(0), undefined)
17+
c.set(42, undefined)
18+
t.end()
19+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy