Skip to content

Commit 2931903

Browse files
authored
Create design-authentication-manager.py
1 parent da06b2c commit 2931903

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Time: ctor: O(1)
2+
# generate: O(1), amortized
3+
# renew: O(1), amortized
4+
# count: O(1), amortized
5+
6+
import collections
7+
8+
9+
class AuthenticationManager(object):
10+
11+
def __init__(self, timeToLive):
12+
"""
13+
:type timeToLive: int
14+
"""
15+
self.__time = timeToLive
16+
self.__lookup = collections.OrderedDict()
17+
18+
def __evict(self, currentTime):
19+
while self.__lookup and next(self.__lookup.itervalues()) <= currentTime:
20+
self.__lookup.popitem(last=False)
21+
22+
def generate(self, tokenId, currentTime):
23+
"""
24+
:type tokenId: str
25+
:type currentTime: int
26+
:rtype: None
27+
"""
28+
self.__evict(currentTime)
29+
self.__lookup[tokenId] = currentTime + self.__time
30+
31+
32+
def renew(self, tokenId, currentTime):
33+
"""
34+
:type tokenId: str
35+
:type currentTime: int
36+
:rtype: None
37+
"""
38+
self.__evict(currentTime)
39+
if tokenId not in self.__lookup:
40+
return
41+
del self.__lookup[tokenId]
42+
self.__lookup[tokenId] = currentTime + self.__time
43+
44+
def countUnexpiredTokens(self, currentTime):
45+
"""
46+
:type currentTime: int
47+
:rtype: int
48+
"""
49+
self.__evict(currentTime)
50+
return len(self.__lookup)

0 commit comments

Comments
 (0)