更好的缓存,扬掉global,重构代码,整理聊天逻辑 (#16)

* 实现缓存装饰器,优化数据获取和存储逻辑

* 重构代码,准备将聊天请求逻辑移入MarshoHandler

* 记录点(

* unfinished

* 🎨 重写基本完毕

* 移除未使用import,添加漏掉的换行
This commit is contained in:
Akarin~
2025-02-24 01:19:26 +08:00
committed by GitHub
parent 3436390f4b
commit aa53643aae
9 changed files with 344 additions and 251 deletions

39
nonebot_plugin_marshoai/cache/decos.py vendored Normal file
View File

@ -0,0 +1,39 @@
from ..models import Cache
cache = Cache()
def from_cache(key):
"""
当缓存中有数据时,直接返回缓存中的数据,否则执行函数并将结果存入缓存
"""
def decorator(func):
async def wrapper(*args, **kwargs):
cached = cache.get(key)
if cached:
return cached
else:
result = await func(*args, **kwargs)
cache.set(key, result)
return result
return wrapper
return decorator
def update_to_cache(key):
"""
执行函数并将结果存入缓存
"""
def decorator(func):
async def wrapper(*args, **kwargs):
result = await func(*args, **kwargs)
cache.set(key, result)
return result
return wrapper
return decorator