This commit is contained in:
HuangHai
2026-01-21 08:41:47 +08:00
parent 306a3a4127
commit e51dc18d06
40 changed files with 1309 additions and 965 deletions

View File

@@ -98,6 +98,14 @@ class RedisKit:
return False
async def set_data(self, key, value, expire=None):
"""
异步保存数据到Redis
Args:
key (str): Redis键名
value (any): 要保存的值
expire (int, optional): 过期时间(秒)
"""
try:
await self._ensure_pool()
if expire:
@@ -108,6 +116,34 @@ class RedisKit:
except Exception as e:
logger.error(f"保存数据到Redis失败(key={key}): {e}")
return False
async def close(self):
"""
关闭Redis连接池
"""
if RedisKit._redis_pool is not None:
try:
# redis-py 4.x+ supports close()
await asyncio.to_thread(RedisKit._redis_pool.close)
RedisKit._redis_pool = None
logger.info("Redis连接池已关闭")
except Exception as e:
logger.error(f"关闭Redis连接池失败: {e}")
async def delete_data(self, key):
"""
异步删除Redis中的数据
Args:
key (str): Redis键名
"""
try:
await self._ensure_pool()
await asyncio.to_thread(RedisKit._redis_pool.delete, key)
return True
except Exception as e:
logger.error(f"从Redis删除数据失败(key={key}): {e}")
return False
# ȫ<><C8AB>ʵ<EFBFBD><CAB5>
# ȫ<><C8AB>ʵ<EFBFBD><CAB5>
redisKit = RedisKit()