mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-14 13:42:56 +08:00
🐛 优化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
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:
parent
4ab9382205
commit
b505307f2f
14
zhenxun/services/cache/cache_containers.py
vendored
14
zhenxun/services/cache/cache_containers.py
vendored
@ -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):
|
||||
"""设置字典项
|
||||
|
||||
Loading…
Reference in New Issue
Block a user