使用内存换速度

This commit is contained in:
远野千束 2024-04-30 15:23:12 +08:00
parent 61d91ea0a9
commit 148d671b5d
2 changed files with 37 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import time
import nonebot import nonebot
import psutil import psutil
from cpuinfo import cpuinfo from cpuinfo import cpuinfo
from nonebot import require
from liteyuki.utils import __NAME__, __VERSION__ from liteyuki.utils import __NAME__, __VERSION__
from liteyuki.utils.base.config import get_config from liteyuki.utils.base.config import get_config
from liteyuki.utils.base.data_manager import TempConfig, common_db 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.base.resource import get_loaded_resource_packs, get_path
from liteyuki.utils.message.html_tool import template2image from liteyuki.utils.message.html_tool import template2image
require("nonebot_plugin_apscheduler")
from nonebot_plugin_apscheduler import scheduler
protocol_names = { protocol_names = {
0: "iPad", 0: "iPad",
1: "Android Phone", 1: "Android Phone",
@ -51,8 +55,26 @@ data
- name: str - name: str
- percent: float - percent: float
- total: int - 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: 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 debug=True
) )
else: 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: def get_local_data(lang_code) -> dict:

View File

@ -28,12 +28,16 @@ status_alc = on_alconna(
@status_alc.handle() @status_alc.handle()
async def _(event: T_MessageEvent, bot: T_Bot): async def _(event: T_MessageEvent, bot: T_Bot):
ulang = get_user_lang(event.user_id) ulang = get_user_lang(event.user_id)
if ulang.lang_code in status_card_cache:
image = status_card_cache[ulang.lang_code]
else:
image = await generate_status_card( image = await generate_status_card(
bot=await get_bots_data(), bot=await get_bots_data(),
hardware=await get_hardware_data(), hardware=await get_hardware_data(),
liteyuki=await get_liteyuki_data(), liteyuki=await get_liteyuki_data(),
lang=ulang.lang_code, lang=ulang.lang_code,
bot_id=bot.self_id bot_id=bot.self_id,
use_cache=True
) )
await status_alc.finish(UniMessage.image(raw=image)) await status_alc.finish(UniMessage.image(raw=image))