-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |