-
-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathlru-cache.js
More file actions
36 lines (35 loc) · 904 Bytes
/
lru-cache.js
File metadata and controls
36 lines (35 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* @param {number} capacity
*/
var LRUCache = function (capacity) {
this.map = new Map() // map默认记住插入的顺序
this.max = capacity // 最大数量
}
/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function (key) {
const value = this.map.get(key) || -1
if (value !== -1) {
this.map.delete(key) // 删除更新插入顺序
this.map.set(key, value)
}
return value
}
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function (key, value) {
if (this.map.has(key)) {
this.map.delete(key) // 删除更新插入顺序
}
this.map.set(key, value)
if (this.max < this.map.size) {
const mapKeys = this.map.keys() // 获取遍历值
const oldKey = mapKeys.next().value // map插入顺序 默认第一个即最早插入的值
this.map.delete(oldKey) // 删除最早的值
}
}