9fo912 / set.py
next-social's picture
add more
51507f3
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'))