LeetCode: Design Authentication Manager Solution

1/**
2 * @param {number} timeToLive
3 */
4var AuthenticationManager = function (timeToLive) {
5 this.map = new Map()
6 this.timeToLive = timeToLive
7}
8
9/**
10 * @param {string} tokenId
11 * @param {number} currentTime
12 * @return {void}
13 */
14AuthenticationManager.prototype.generate = function (tokenId, currentTime) {
15 this.map.set(tokenId, currentTime)
16}
17
18/**
19 * @param {string} tokenId
20 * @param {number} currentTime
21 * @return {void}
22 */
23AuthenticationManager.prototype.renew = function (tokenId, currentTime) {
24 if (this.map.get(tokenId) + this.timeToLive > currentTime) {
25 this.map.set(tokenId, currentTime)
26 }
27}
28
29/**
30 * @param {number} currentTime
31 * @return {number}
32 */
33AuthenticationManager.prototype.countUnexpiredTokens = function (currentTime) {
34 return Array.from(this.map).filter(
35 ([token, time]) => time + this.timeToLive > currentTime
36 ).length
37}

Comments

Loading comments...