1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class AuthenticationManager { public: int timeToLive; unordered_map<string, int> map;
AuthenticationManager(int timeToLive) : timeToLive(timeToLive) {}
void generate(string tokenId, int currentTime) { map[tokenId] = currentTime; }
void renew(string tokenId, int currentTime) { if (map.find(tokenId) != map.end() && currentTime < map[tokenId] + timeToLive) map[tokenId] = currentTime; }
int countUnexpiredTokens(int currentTime) { int cnt = 0; for (auto &[token, time]: map) { if (time + timeToLive > currentTime) cnt += 1; } return cnt; } };
|