Spaces:
Runtime error
Runtime error
File size: 924 Bytes
51507f3 ad223ef 48b3618 ad223ef 51507f3 |
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 |
import time
from datetime import datetime, timedelta
from collections import defaultdict
class ExpiringMap:
def __init__(self):
self.data = defaultdict(dict)
def put(self, key, value, expire_time):
current_time = datetime.now()
expire_timestamp = current_time + timedelta(seconds=expire_time)
self.data[key] = {'value': value, 'expire_time': expire_timestamp}
def get(self, key):
current_time = datetime.now()
if key in self.data and self.data[key]['expire_time'] > current_time:
return self.data[key]['value']
else:
return None
def remove(self, key):
if key in self.data:
del self.data[key]
if __name__ == "__main__":
time_client_map = ExpiringMap()
time_client_map.put('a', 'b', 10)
time.sleep(5)
print(time_client_map.get('a'))
time.sleep(6)
print(time_client_map.get('a'))
|