-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
268303c
commit 8d161a4
Showing
12 changed files
with
67 additions
and
434 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
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
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.