1.config/cache_conf.py# 设置 和 读取字符串 和 列表或字典[{}] # 读取字符串 async def get_cache(key: str): # return await redis_client.get(key) try: return await redis_client.get(key) except Exception as e: print(f获取缓存失败{e}) return None # 读取列表或字典 async def get_json_cache(key: str): try: data await redis_client.get(key) if data: return json.loads(data) # 序列化 return None except Exception as e: print(f获取 JSON 缓存失败{e}) return None # 设置缓存 setex(key, expire, value) async def set_cache(key: str, value: Any, expire: int 3600): try: if isinstance(value, (dict, list)): # 转字符串再存 value json.dumps(value, ensure_asciiFalse) # 中文正常保存 await redis_client.setex(key, expire, value) return True except Exception as e: print(f设置缓存失败{e}) return False