-
Notifications
You must be signed in to change notification settings - Fork 106
/
design-authentication-manager.py
51 lines (42 loc) · 1.29 KB
/
design-authentication-manager.py
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
# Time: ctor: O(1)
# generate: O(1), amortized
# renew: O(1), amortized
# count: O(1), amortized
# Space: O(n)
import collections
class AuthenticationManager(object):
def __init__(self, timeToLive):
"""
:type timeToLive: int
"""
self.__time = timeToLive
self.__lookup = collections.OrderedDict()
def __evict(self, currentTime):
while self.__lookup and next(self.__lookup.itervalues()) <= currentTime:
self.__lookup.popitem(last=False)
def generate(self, tokenId, currentTime):
"""
:type tokenId: str
:type currentTime: int
:rtype: None
"""
self.__evict(currentTime)
self.__lookup[tokenId] = currentTime + self.__time
def renew(self, tokenId, currentTime):
"""
:type tokenId: str
:type currentTime: int
:rtype: None
"""
self.__evict(currentTime)
if tokenId not in self.__lookup:
return
del self.__lookup[tokenId]
self.__lookup[tokenId] = currentTime + self.__time
def countUnexpiredTokens(self, currentTime):
"""
:type currentTime: int
:rtype: int
"""
self.__evict(currentTime)
return len(self.__lookup)