mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-15 09:10:00 +08:00
✨ 格式化db_context (#1980)
* ✨ 格式化db_context * 🔥 移除旧db-context * ⚡ 添加旧版本兼容
This commit is contained in:
Vendored
+25
-34
@@ -63,6 +63,7 @@ from functools import wraps
|
||||
from typing import Any, ClassVar, Generic, TypeVar, get_type_hints
|
||||
|
||||
from aiocache import Cache as AioCache
|
||||
from aiocache import SimpleMemoryCache
|
||||
from aiocache.base import BaseCache
|
||||
from aiocache.serializers import JsonSerializer
|
||||
import nonebot
|
||||
@@ -392,22 +393,14 @@ class CacheManager:
|
||||
def cache_backend(self) -> BaseCache | AioCache:
|
||||
"""获取缓存后端"""
|
||||
if self._cache_backend is None:
|
||||
try:
|
||||
from aiocache import RedisCache, SimpleMemoryCache
|
||||
ttl = cache_config.redis_expire
|
||||
if cache_config.cache_mode == CacheMode.NONE:
|
||||
ttl = 0
|
||||
logger.info("缓存功能已禁用,使用非持久化内存缓存", LOG_COMMAND)
|
||||
elif cache_config.cache_mode == CacheMode.REDIS and cache_config.redis_host:
|
||||
try:
|
||||
from aiocache import RedisCache
|
||||
|
||||
if cache_config.cache_mode == CacheMode.NONE:
|
||||
# 使用内存缓存但禁用持久化
|
||||
self._cache_backend = SimpleMemoryCache(
|
||||
serializer=JsonSerializer(),
|
||||
namespace=CACHE_KEY_PREFIX,
|
||||
timeout=30,
|
||||
ttl=0, # 设置为0,不缓存
|
||||
)
|
||||
logger.info("缓存功能已禁用,使用非持久化内存缓存", LOG_COMMAND)
|
||||
elif (
|
||||
cache_config.cache_mode == CacheMode.REDIS
|
||||
and cache_config.redis_host
|
||||
):
|
||||
# 使用Redis缓存
|
||||
self._cache_backend = RedisCache(
|
||||
serializer=JsonSerializer(),
|
||||
@@ -419,27 +412,25 @@ class CacheManager:
|
||||
password=cache_config.redis_password,
|
||||
)
|
||||
logger.info(
|
||||
f"使用Redis缓存,地址: {cache_config.redis_host}", LOG_COMMAND
|
||||
f"使用Redis缓存,地址: {cache_config.redis_host}",
|
||||
LOG_COMMAND,
|
||||
)
|
||||
else:
|
||||
# 默认使用内存缓存
|
||||
self._cache_backend = SimpleMemoryCache(
|
||||
serializer=JsonSerializer(),
|
||||
namespace=CACHE_KEY_PREFIX,
|
||||
timeout=30,
|
||||
ttl=cache_config.redis_expire,
|
||||
return self._cache_backend
|
||||
except ImportError as e:
|
||||
logger.error(
|
||||
"导入aiocache[redis]失败,将默认使用内存缓存...",
|
||||
LOG_COMMAND,
|
||||
e=e,
|
||||
)
|
||||
logger.info("使用内存缓存", LOG_COMMAND)
|
||||
except ImportError:
|
||||
logger.error("导入aiocache模块失败,使用内存缓存", LOG_COMMAND)
|
||||
# 使用内存缓存
|
||||
self._cache_backend = AioCache(
|
||||
cache_class=AioCache.MEMORY,
|
||||
serializer=JsonSerializer(),
|
||||
namespace=CACHE_KEY_PREFIX,
|
||||
timeout=30,
|
||||
ttl=cache_config.redis_expire,
|
||||
)
|
||||
else:
|
||||
logger.info("使用内存缓存", LOG_COMMAND)
|
||||
# 默认使用内存缓存
|
||||
self._cache_backend = SimpleMemoryCache(
|
||||
serializer=JsonSerializer(),
|
||||
namespace=CACHE_KEY_PREFIX,
|
||||
timeout=30,
|
||||
ttl=ttl,
|
||||
)
|
||||
return self._cache_backend
|
||||
|
||||
@property
|
||||
|
||||
+7
-14
@@ -54,11 +54,7 @@ class CacheDict:
|
||||
key: 字典键
|
||||
value: 字典值
|
||||
"""
|
||||
# 计算过期时间
|
||||
expire_time = 0
|
||||
if self.expire > 0:
|
||||
expire_time = time.time() + self.expire
|
||||
|
||||
expire_time = time.time() + self.expire if self.expire > 0 else 0
|
||||
self._data[key] = CacheData(value=value, expire_time=expire_time)
|
||||
|
||||
def __delitem__(self, key: str) -> None:
|
||||
@@ -274,12 +270,11 @@ class CacheList:
|
||||
self.clear()
|
||||
raise IndexError(f"列表索引 {index} 超出范围")
|
||||
|
||||
if 0 <= index < len(self._data):
|
||||
del self._data[index]
|
||||
# 更新过期时间
|
||||
self._update_expire_time()
|
||||
else:
|
||||
if not 0 <= index < len(self._data):
|
||||
raise IndexError(f"列表索引 {index} 超出范围")
|
||||
del self._data[index]
|
||||
# 更新过期时间
|
||||
self._update_expire_time()
|
||||
|
||||
def __len__(self) -> int:
|
||||
"""获取列表长度
|
||||
@@ -427,6 +422,7 @@ class CacheList:
|
||||
self.clear()
|
||||
return 0
|
||||
|
||||
# sourcery skip: simplify-constant-sum
|
||||
return sum(1 for item in self._data if item.value == value)
|
||||
|
||||
def _is_expired(self) -> bool:
|
||||
@@ -435,10 +431,7 @@ class CacheList:
|
||||
|
||||
def _update_expire_time(self) -> None:
|
||||
"""更新过期时间"""
|
||||
if self.expire > 0:
|
||||
self._expire_time = time.time() + self.expire
|
||||
else:
|
||||
self._expire_time = 0
|
||||
self._expire_time = time.time() + self.expire if self.expire > 0 else 0
|
||||
|
||||
def __str__(self) -> str:
|
||||
"""字符串表示
|
||||
|
||||
Reference in New Issue
Block a user