Spaces:
Runtime error
Runtime error
File size: 701 Bytes
ad223ef 48b3618 ad223ef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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]
|