From 1b5a969b96d961f6408d20c9e3a82a97b11ded29 Mon Sep 17 00:00:00 2001 From: omg-xtao <100690902+omg-xtao@users.noreply.github.com> Date: Sat, 1 Oct 2022 14:56:15 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20gacha=20=E7=BC=93=E5=AD=98=2012=20?= =?UTF-8?q?=E5=B0=8F=E6=97=B6=E5=8D=A1=E6=B1=A0=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/apihelper/gacha.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/modules/apihelper/gacha.py b/modules/apihelper/gacha.py index caab717b..6378cb5e 100644 --- a/modules/apihelper/gacha.py +++ b/modules/apihelper/gacha.py @@ -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()