diff --git a/zhenxun/services/cache.py b/zhenxun/services/cache.py index 071af501..2c74c07f 100644 --- a/zhenxun/services/cache.py +++ b/zhenxun/services/cache.py @@ -1,15 +1,14 @@ +import inspect from collections.abc import Callable from datetime import datetime from functools import wraps -import inspect from typing import Any, ClassVar, Generic, TypeVar +import nonebot from aiocache import Cache as AioCache - # from aiocache.backends.redis import RedisCache from aiocache.base import BaseCache from aiocache.serializers import JsonSerializer -import nonebot from nonebot.compat import model_dump from nonebot.utils import is_coroutine_callable from pydantic import BaseModel, PrivateAttr @@ -577,21 +576,24 @@ class CacheManager: expire: 过期时间(秒) """ - def wrapper(func: Callable): - _name = name.upper() - if _name in self._data: - raise DbCacheException(f"缓存 {name} 已存在") +def new(self, name: str, lazy_load: bool = True, expire: int = 600): + def wrapper(func: Callable): + _name = name.upper() + if _name in self._data: + raise DbCacheException(f"缓存 {name} 已存在") - self._data[_name] = CacheData( - name=_name, - func=func, - expire=expire, - lazy_load=lazy_load, - _cache=self._cache, - ) - return func - return wrapper + cache_data = CacheData( + name=_name, + func=func, + expire=expire, + lazy_load=lazy_load, + ) + cache_data._cache = self._cache + self._data[_name] = cache_data + return func + + return wrapper def listener(self, name: str): """创建缓存监听器"""