1
+ /**
2
+ * @param {number } timeToLive
3
+ */
4
+ var AuthenticationManager = function ( timeToLive ) {
5
+ this . timeToLive = timeToLive ;
6
+ this . map = new Map ( ) ; // tokenId -> liveTime (latest)
7
+ } ;
8
+
9
+ /**
10
+ * @param {string } tokenId
11
+ * @param {number } currentTime
12
+ * @return {void }
13
+ */
14
+ AuthenticationManager . prototype . generate = function ( tokenId , currentTime ) {
15
+ this . map . set ( tokenId , currentTime + this . timeToLive ) ;
16
+ } ;
17
+
18
+ /**
19
+ * @param {string } tokenId
20
+ * @param {number } currentTime
21
+ * @return {void }
22
+ */
23
+ AuthenticationManager . prototype . renew = function ( tokenId , currentTime ) {
24
+ const liveTime = this . map . get ( tokenId ) ;
25
+ if ( this . map . has ( tokenId ) && liveTime > currentTime ) {
26
+ this . map . set ( tokenId , currentTime + this . timeToLive ) ;
27
+ }
28
+ } ;
29
+
30
+ /**
31
+ * @param {number } currentTime
32
+ * @return {number }
33
+ */
34
+ AuthenticationManager . prototype . countUnexpiredTokens = function ( currentTime ) {
35
+ let count = 0 ;
36
+ this . map . forEach ( ( item ) => {
37
+ if ( item > currentTime ) {
38
+ count ++ ;
39
+ } else {
40
+ this . map . delete ( key ) ; // 移除过期的 token
41
+ }
42
+ } ) ;
43
+ return count ;
44
+ } ;
45
+
46
+
47
+ /**
48
+ * Your AuthenticationManager object will be instantiated and called as such:
49
+ * var obj = new AuthenticationManager(timeToLive)
50
+ * obj.generate(tokenId,currentTime)
51
+ * obj.renew(tokenId,currentTime)
52
+ * var param_3 = obj.countUnexpiredTokens(currentTime)
53
+ */
0 commit comments