|
@@ -1,50 +1,28 @@ |
|
|
import time |
|
|
import time |
|
|
from collections import OrderedDict |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExpiredDict(OrderedDict): |
|
|
|
|
|
|
|
|
class ExpiredDict(dict): |
|
|
def __init__(self, expires_in_seconds): |
|
|
def __init__(self, expires_in_seconds): |
|
|
super().__init__() |
|
|
super().__init__() |
|
|
# 存储键值对的生存时间 |
|
|
|
|
|
self.expires_in_seconds = expires_in_seconds |
|
|
self.expires_in_seconds = expires_in_seconds |
|
|
# 获取当前时间的函数 |
|
|
|
|
|
self.now = time.monotonic |
|
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, key): |
|
|
def __getitem__(self, key): |
|
|
# 检查键是否存在 |
|
|
|
|
|
if key not in self: |
|
|
|
|
|
raise KeyError(key) |
|
|
|
|
|
# 获取值和过期时间 |
|
|
|
|
|
value, expiry_time = self[key] |
|
|
|
|
|
# 如果过期时间早于当前时间,删除该键值对并引发 KeyError |
|
|
|
|
|
if expiry_time is not None and self.now() > expiry_time: |
|
|
|
|
|
|
|
|
value, expiry_time = super().__getitem__(key) |
|
|
|
|
|
# 如果元素已过期,则从字典中删除该元素并抛出 KeyError 异常 |
|
|
|
|
|
if time.monotonic() > expiry_time: |
|
|
del self[key] |
|
|
del self[key] |
|
|
raise KeyError(key) |
|
|
|
|
|
# 如果存活时间不为 None,更新该键值对的过期时间 |
|
|
|
|
|
if self.expires_in_seconds is not None: |
|
|
|
|
|
self[key] = value, self.now() + self.expires_in_seconds |
|
|
|
|
|
# 删除过期的键值对 |
|
|
|
|
|
self._delete_expired_items() |
|
|
|
|
|
# 返回值 |
|
|
|
|
|
|
|
|
raise KeyError("expired {}".format(key)) |
|
|
return value |
|
|
return value |
|
|
|
|
|
|
|
|
def __setitem__(self, key, value): |
|
|
def __setitem__(self, key, value): |
|
|
# 如果存活时间不为 None,设置该键值对的过期时间 |
|
|
|
|
|
if self.expires_in_seconds is not None: |
|
|
|
|
|
self[key] = value, self.now() + self.expires_in_seconds |
|
|
|
|
|
|
|
|
# 如果元素已存在,则不需要重新计算过期时间 |
|
|
|
|
|
if key in self: |
|
|
|
|
|
super().__setitem__(key, (value, self[key][1])) |
|
|
else: |
|
|
else: |
|
|
self[key] = value, None |
|
|
|
|
|
# 删除过期的键值对 |
|
|
|
|
|
self._delete_expired_items() |
|
|
|
|
|
|
|
|
super().__setitem__(key, (value, time.monotonic() + self.expires_in_seconds)) |
|
|
|
|
|
|
|
|
def get(self, key, default=None): |
|
|
def get(self, key, default=None): |
|
|
try: |
|
|
try: |
|
|
return self[key] |
|
|
return self[key] |
|
|
except KeyError: |
|
|
except KeyError: |
|
|
return default |
|
|
return default |
|
|
|
|
|
|
|
|
def _delete_expired_items(self): |
|
|
|
|
|
# 遍历所有键值对,删除过期的键值对 |
|
|
|
|
|
for key, (value, expiry_time) in list(self.items()): |
|
|
|
|
|
if expiry_time is not None and self.now() > expiry_time: |
|
|
|
|
|
del self[key] |
|
|
|