From e1bc81c9e135e394e225e498d10b412fe4ed6469 Mon Sep 17 00:00:00 2001 From: Asankilp Date: Sat, 22 Feb 2025 13:06:06 +0800 Subject: [PATCH] pre implement cache --- nonebot_plugin_marshoai/decos.py | 15 +++++++++++++++ nonebot_plugin_marshoai/instances.py | 3 ++- nonebot_plugin_marshoai/models.py | 19 +++++++++++++++++++ nonebot_plugin_marshoai/util.py | 1 + 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 nonebot_plugin_marshoai/decos.py diff --git a/nonebot_plugin_marshoai/decos.py b/nonebot_plugin_marshoai/decos.py new file mode 100644 index 00000000..66a12e30 --- /dev/null +++ b/nonebot_plugin_marshoai/decos.py @@ -0,0 +1,15 @@ +from .instances import cache + + +def from_cache(key): + def decorator(func): + def wrapper(*args, **kwargs): + cached = cache.get(key) + if cached: + return cached + else: + result = func(*args, **kwargs) + cache.set(key, result) + return result + + return wrapper diff --git a/nonebot_plugin_marshoai/instances.py b/nonebot_plugin_marshoai/instances.py index d5aaf709..d450d2d2 100644 --- a/nonebot_plugin_marshoai/instances.py +++ b/nonebot_plugin_marshoai/instances.py @@ -3,7 +3,7 @@ from nonebot import get_driver from openai import AsyncOpenAI from .config import config -from .models import MarshoContext, MarshoTools +from .models import Cache, MarshoContext, MarshoTools driver = get_driver() @@ -11,6 +11,7 @@ command_start = driver.config.command_start model_name = config.marshoai_default_model context = MarshoContext() tools = MarshoTools() +cache = Cache() token = config.marshoai_token endpoint = config.marshoai_azure_endpoint # client = ChatCompletionsClient(endpoint=endpoint, credential=AzureKeyCredential(token)) diff --git a/nonebot_plugin_marshoai/models.py b/nonebot_plugin_marshoai/models.py index 47f02c85..701f76a5 100755 --- a/nonebot_plugin_marshoai/models.py +++ b/nonebot_plugin_marshoai/models.py @@ -11,6 +11,25 @@ from nonebot import logger from .config import config +class Cache: + """ + 缓存类 + """ + + def __init__(self): + self.cache = {} + + def get(self, key): + if key in self.cache: + return self.cache[key] + else: + self.cache[key] = None + return None + + def set(self, key, value): + self.cache[key] = value + + class MarshoContext: """ Marsho 的上下文类 diff --git a/nonebot_plugin_marshoai/util.py b/nonebot_plugin_marshoai/util.py index bceef64a..0fbd6ec0 100755 --- a/nonebot_plugin_marshoai/util.py +++ b/nonebot_plugin_marshoai/util.py @@ -23,6 +23,7 @@ from ._types import DeveloperMessage from .config import config from .constants import CODE_BLOCK_PATTERN, IMG_LATEX_PATTERN, OPENAI_NEW_MODELS from .deal_latex import ConvertLatex +from .instances import cache nickname_json = None # 记录昵称 praises_json = None # 记录夸赞名单