Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions classes/lrucache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class LRUCache {
Comment thread
mbtools marked this conversation as resolved.
constructor (max = 0) {
Comment thread
mbtools marked this conversation as resolved.
Outdated
if (!Number.isInteger(max) || max < 0) {
throw new TypeError('max must be a nonnegative integer')
}

this.max = max
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
}
}

has (key) {
return this.map.has(key)
}

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
}

clear () {
this.map.clear()
}

capacity () {
return this.max
}

size () {
return this.map.size
}

* entries () {
for (const [key, value] of this.map) {
yield [key, value]
}
}
}

module.exports = LRUCache
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('./lrucache')
const cache = new LRU(1000)

const parseOptions = require('../internal/parse-options')
const Comparator = require('./comparator')
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@
"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
Loading