'commit'
This commit is contained in:
@@ -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()
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user