🐛 优化CacheDict类中的键存在性检查和获取逻辑,简化代码结构,提高可读性。 (#2037)
Some checks failed
检查bot是否运行正常 / bot check (push) Has been cancelled
CodeQL Code Security Analysis / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Has been cancelled
CodeQL Code Security Analysis / Analyze (${{ matrix.language }}) (none, python) (push) Has been cancelled
Sequential Lint and Type Check / ruff-call (push) Has been cancelled
Release Drafter / Update Release Draft (push) Has been cancelled
Force Sync to Aliyun / sync (push) Has been cancelled
Update Version / update-version (push) Has been cancelled
Sequential Lint and Type Check / pyright-call (push) Has been cancelled

This commit is contained in:
HibiKier 2025-08-29 16:07:04 +08:00 committed by GitHub
parent 4ab9382205
commit b505307f2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -80,11 +80,7 @@ class CacheDict(Generic[T]):
返回:
bool: 是否存在
"""
if key not in self._data:
return False
# 检查是否过期
return bool(self.expire_time(key))
return False if key not in self._data else bool(self.expire_time(key))
def get(self, key: str, default: Any = None) -> T | None:
"""获取字典项,如果不存在返回默认值
@ -96,8 +92,12 @@ class CacheDict(Generic[T]):
返回:
Any: 字典值或默认值
"""
value = self[key]
return default if value is None else value
if value := self._data.get(key):
if self.expire_time(key):
return default
if not value:
return default
return default if value.value is None else value.value
def set(self, key: str, value: Any, expire: int | None = None):
"""设置字典项