From e071b6c1b46d3c5aa0e9e06dea4a4fd6bdf8d1c2 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Sat, 25 Mar 2023 01:15:56 +0800 Subject: [PATCH] fix: time type bug --- common/expired_dict.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/common/expired_dict.py b/common/expired_dict.py index 15cdd75..a86923b 100644 --- a/common/expired_dict.py +++ b/common/expired_dict.py @@ -1,6 +1,6 @@ -import time from datetime import datetime, timedelta + class ExpiredDict(dict): def __init__(self, expires_in_seconds): super().__init__() @@ -8,8 +8,7 @@ class ExpiredDict(dict): def __getitem__(self, key): value, expiry_time = super().__getitem__(key) - # 如果元素已过期,则从字典中删除该元素并抛出 KeyError 异常 - if time.monotonic() > expiry_time: + if datetime.now() > expiry_time: del self[key] raise KeyError("expired {}".format(key)) self.__setitem__(key, value) @@ -24,20 +23,20 @@ class ExpiredDict(dict): return self[key] except KeyError: return default - + def __contains__(self, key): try: self[key] return True except KeyError: return False - + def keys(self): - keys=list(super().keys()) + keys = list(super().keys()) return [key for key in keys if key in self] - + def items(self): return [(key, self[key]) for key in self.keys()] - + def __iter__(self): - return self.keys().__iter__() + return self.keys().__iter__() \ No newline at end of file