gacha 缓存 12 小时卡池数据

This commit is contained in:
omg-xtao 2022-10-01 14:56:15 +08:00 committed by GitHub
parent 30cd89714f
commit 1b5a969b96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,3 +1,4 @@
import time
import httpx
from modules.apihelper.base import BaseResponseData
@ -15,15 +16,28 @@ class GachaInfo:
'User-Agent': self.USER_AGENT,
}
self.client = httpx.AsyncClient(headers=self.headers)
self.cache = {}
self.cache_ttl = 12 * 60 * 60
async def get_gacha_list_info(self) -> BaseResponseData:
if self.cache.get("time", 0) + self.cache_ttl < time.time():
self.cache.clear()
cache = self.cache.get("gacha_list_info")
if cache is not None:
return BaseResponseData(cache)
req = await self.client.get(self.GACHA_LIST_URL)
if req.is_error:
return BaseResponseData(error_message="请求错误")
self.cache["gacha_list_info"] = req.json()
self.cache["time"] = time.time()
return BaseResponseData(req.json())
async def get_gacha_info(self, gacha_id: str) -> dict:
cache = self.cache.get(gacha_id)
if cache is not None:
return cache
req = await self.client.get(self.GACHA_INFO_URL % gacha_id)
if req.is_error:
return {}
self.cache[gacha_id] = req.json()
return req.json()