From 148d671b5d900a798f6c9f39b758dad690617848 Mon Sep 17 00:00:00 2001 From: snowy Date: Tue, 30 Apr 2024 15:23:12 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E4=BD=BF=E7=94=A8=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=8D=A2=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liteyuki/plugins/liteyuki_status/api.py | 28 ++++++++++++++++++++-- liteyuki/plugins/liteyuki_status/status.py | 18 ++++++++------ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/liteyuki/plugins/liteyuki_status/api.py b/liteyuki/plugins/liteyuki_status/api.py index ba276a7..a0205dd 100644 --- a/liteyuki/plugins/liteyuki_status/api.py +++ b/liteyuki/plugins/liteyuki_status/api.py @@ -4,6 +4,7 @@ import time import nonebot import psutil from cpuinfo import cpuinfo +from nonebot import require from liteyuki.utils import __NAME__, __VERSION__ from liteyuki.utils.base.config import get_config from liteyuki.utils.base.data_manager import TempConfig, common_db @@ -11,6 +12,9 @@ from liteyuki.utils.base.language import Language from liteyuki.utils.base.resource import get_loaded_resource_packs, get_path from liteyuki.utils.message.html_tool import template2image +require("nonebot_plugin_apscheduler") +from nonebot_plugin_apscheduler import scheduler + protocol_names = { 0: "iPad", 1: "Android Phone", @@ -51,8 +55,26 @@ data - name: str - percent: float - total: int - """ +status_card_cache = {} # lang -> bytes + + +# 60s刷新一次 +@scheduler.scheduled_job("cron", second="*/40") +async def refresh_status_card(): + nonebot.logger.debug("Refreshing status card cache...") + global status_card_cache + bot_data = await get_bots_data() + hardware_data = await get_hardware_data() + liteyuki_data = await get_liteyuki_data() + for lang in status_card_cache.keys(): + status_card_cache[lang] = await generate_status_card( + bot_data, + hardware_data, + liteyuki_data, + lang=lang, + use_cache=False + ) async def generate_status_card(bot: dict, hardware: dict, liteyuki: dict, lang="zh-CN", bot_id="0", use_cache=False) -> bytes: @@ -70,7 +92,9 @@ async def generate_status_card(bot: dict, hardware: dict, liteyuki: dict, lang=" debug=True ) else: - pass + if lang not in status_card_cache: + status_card_cache[lang] = await generate_status_card(bot, hardware, liteyuki, lang=lang, bot_id=bot_id) + return status_card_cache[lang] def get_local_data(lang_code) -> dict: diff --git a/liteyuki/plugins/liteyuki_status/status.py b/liteyuki/plugins/liteyuki_status/status.py index a0615a1..13f2500 100644 --- a/liteyuki/plugins/liteyuki_status/status.py +++ b/liteyuki/plugins/liteyuki_status/status.py @@ -28,13 +28,17 @@ status_alc = on_alconna( @status_alc.handle() async def _(event: T_MessageEvent, bot: T_Bot): ulang = get_user_lang(event.user_id) - image = await generate_status_card( - bot=await get_bots_data(), - hardware=await get_hardware_data(), - liteyuki=await get_liteyuki_data(), - lang=ulang.lang_code, - bot_id=bot.self_id - ) + if ulang.lang_code in status_card_cache: + image = status_card_cache[ulang.lang_code] + else: + image = await generate_status_card( + bot=await get_bots_data(), + hardware=await get_hardware_data(), + liteyuki=await get_liteyuki_data(), + lang=ulang.lang_code, + bot_id=bot.self_id, + use_cache=True + ) await status_alc.finish(UniMessage.image(raw=image))