-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy path1797.设计一个验证系统.js
64 lines (57 loc) · 1.42 KB
/
1797.设计一个验证系统.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* @lc app=leetcode.cn id=1797 lang=javascript
*
* [1797] 设计一个验证系统
*/
// @lc code=start
/**
* @param {number} timeToLive
*/
var AuthenticationManager = function(timeToLive) {
this.timeToLive = timeToLive
this.manager = {}
// this.manager = new Map() //管理所有token {}
};
/**
* @param {string} tokenId
* @param {number} currentTime
* @return {void}
*/
AuthenticationManager.prototype.generate = function(tokenId, currentTime) {
this.manager[tokenId] = currentTime
// this.manager.set(tokenId,currentTime)
};
/**
* @param {string} tokenId
* @param {number} currentTime
* @return {void}
*/
AuthenticationManager.prototype.renew = function(tokenId, currentTime) {
if(tokenId in this.manager){
let time = this.manager[tokenId]
if(time+this.timeToLive >currentTime){
this.manager[tokenId] = currentTime
}
}
};
/**
* @param {number} currentTime
* @return {number}
*/
AuthenticationManager.prototype.countUnexpiredTokens = function(currentTime) {
let result = 0
for(const time of Object.values(this.manager)){
if(time + this.timeToLive>currentTime){
result+=1
}
}
return result
};
/**
* Your AuthenticationManager object will be instantiated and called as such:
* var obj = new AuthenticationManager(timeToLive)
* obj.generate(tokenId,currentTime)
* obj.renew(tokenId,currentTime)
* var param_3 = obj.countUnexpiredTokens(currentTime)
*/
// @lc code=end