🔧 支持使用配置文件是否启用 genshin.py 模块的缓存

This commit is contained in:
洛水居室 2022-10-22 23:51:39 +08:00
parent bf90ca55f9
commit 291b21f674
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
3 changed files with 15 additions and 9 deletions

View File

@ -53,6 +53,9 @@ LOGGER_LOCALS_MAX_STRING=80
# CONNECT_TIMEOUT = 10 # CONNECT_TIMEOUT = 10
# POOL_TIMEOUT = 10 # POOL_TIMEOUT = 10
# genshin.py 缓存配置 可选配置项
# GENSHIN_TTL = 3600
# mtp 客户端 可选配置项 # mtp 客户端 可选配置项
# API_ID=12345 # API_ID=12345
# API_HASH="abcdefg" # API_HASH="abcdefg"

View File

@ -63,6 +63,8 @@ class BotConfig(BaseSettings):
connect_timeout: Optional[float] = None connect_timeout: Optional[float] = None
pool_timeout: Optional[float] = None pool_timeout: Optional[float] = None
genshin_ttl: Optional[int] = None
enka_network_api_agent: str = "" enka_network_api_agent: str = ""
pass_challenge_api: str = "" pass_challenge_api: str = ""
pass_challenge_app_key: str = "" pass_challenge_app_key: str = ""

View File

@ -18,6 +18,7 @@ from typing_extensions import ParamSpec
from core.base.redisdb import RedisDB from core.base.redisdb import RedisDB
from core.bot import bot from core.bot import bot
from core.config import config
from core.cookies.services import CookiesService, PublicCookiesService from core.cookies.services import CookiesService, PublicCookiesService
from core.error import ServiceNotFoundError from core.error import ServiceNotFoundError
from core.user.services import UserService from core.user.services import UserService
@ -47,8 +48,8 @@ public_cookies_service = cast(PublicCookiesService, public_cookies_service)
redis_db = bot.services.get(RedisDB) redis_db = bot.services.get(RedisDB)
redis_db = cast(RedisDB, redis_db) redis_db = cast(RedisDB, redis_db)
genshin_cache: Optional[genshin.RedisCache] = None genshin_cache: Optional[genshin.RedisCache] = None
if redis_db: if redis_db and config.genshin_ttl:
genshin_cache = genshin.RedisCache(redis_db.client) genshin_cache = genshin.RedisCache(redis_db.client, ttl=config.genshin_ttl)
REGION_MAP = { REGION_MAP = {
"1": RegionEnum.HYPERION, "1": RegionEnum.HYPERION,
@ -204,7 +205,7 @@ async def async_re_sub(
# noinspection PyCallingNonCallable # noinspection PyCallingNonCallable
replaced = repl(match) replaced = repl(match)
result += temp[: match.span(1)[0]] + (replaced or repl) result += temp[: match.span(1)[0]] + (replaced or repl)
temp = temp[match.span(1)[1] :] temp = temp[match.span(1)[1]:]
else: else:
while match := re.search(pattern, temp, flags=flags): while match := re.search(pattern, temp, flags=flags):
replaced = None replaced = None
@ -215,5 +216,5 @@ async def async_re_sub(
# noinspection PyCallingNonCallable # noinspection PyCallingNonCallable
replaced = repl(match) replaced = repl(match)
result += temp[: match.span(1)[0]] + (replaced or repl) result += temp[: match.span(1)[0]] + (replaced or repl)
temp = temp[match.span(1)[1] :] temp = temp[match.span(1)[1]:]
return result + temp return result + temp