main
zc 1 month ago
parent 5ca4280d7d
commit ec0d6fd7f6
  1. 3
      app/__init__.py
  2. 30
      app/api/chat/ai/chat_router.py
  3. 58
      app/api/chat/ai/chat_service.py

@ -74,7 +74,8 @@ async def lifespan(app: FastAPI):
socket_keepalive=True,
retry_on_timeout=True,
# 连接池参数
max_connections=128, # Redis连接池最大连接数
max_connections=512, # Redis连接池最大连接数
health_check_interval=30
)
await app.state.redis_client.ping()

@ -267,12 +267,14 @@ async def get_question(request: Request, req: AllScenicFlowRequest):
# Redis 缓存查询
cache_key = "quick_questions"
redis_client = request.app.state.redis_client
try:
redis_client = request.app.state.redis_client
cached = await redis_client.get(cache_key)
if cached:
# 缓存命中,直接返回缓存数据
return json.loads(cached)
async with redis_client.connection() as redis_con:
cached = await redis_con.get(cache_key)
if cached:
# 缓存命中,直接返回缓存数据
return json.loads(cached)
except Exception as e:
print(f"[Redis] 查询缓存失败: {e}")
@ -295,7 +297,8 @@ async def get_question(request: Request, req: AllScenicFlowRequest):
# 将结果存入Redis缓存,过期时间10分钟
try:
await redis_client.setex(cache_key, 600, json.dumps(result))
async with redis_client.connection() as redis_con:
await redis_con.setex(cache_key, 600, json.dumps(result))
except Exception as e:
print(f"[Redis] 写缓存失败: {e}")
@ -323,12 +326,14 @@ class HotQuestionResponse(BaseModel):
async def get_hot_questions(request: Request, req: AllScenicFlowRequest):
# Redis 缓存查询
cache_key = "hot_questions"
redis_client = request.app.state.redis_client
try:
redis_client = request.app.state.redis_client
cached = await redis_client.get(cache_key)
if cached:
# 缓存命中,直接返回缓存数据
return json.loads(cached)
async with redis_client.connection() as redis_con:
cached = await redis_con.get(cache_key)
if cached:
# 缓存命中,直接返回缓存数据
return json.loads(cached)
except Exception as e:
print(f"[Redis] 查询缓存失败: {e}")
@ -362,7 +367,8 @@ async def get_hot_questions(request: Request, req: AllScenicFlowRequest):
# 将结果存入Redis缓存,过期时间10分钟
try:
await redis_client.setex(cache_key, 600, json.dumps(result))
async with redis_client.connection() as redis_con:
await redis_con.setex(cache_key, 600, json.dumps(result))
except Exception as e:
print(f"[Redis] 写缓存失败: {e}")

@ -312,15 +312,16 @@ async def query_flow(request: Request, spot: str) -> str:
return "**未找到景区信息,请检查名称是否正确。\n\n(内容由AI生成,仅供参考)"
cache_key = f"flow:{spot}"
redis_client = request.app.state.redis_client
# Step 1: Redis 缓存查询
print(f"Querying Redis cache for key: {cache_key}")
try:
redis_client = request.app.state.redis_client
cached = await redis_client.get(cache_key)
if cached:
print(f"Found cached data for key: {cache_key}")
return cached
async with redis_client.connection() as conn:
cached = await conn.get(cache_key)
if cached:
print(f"Found cached data for key: {cache_key}")
return cached
except Exception as e:
print(f"[Redis] 查询缓存失败: {e}")
@ -402,7 +403,8 @@ async def query_flow(request: Request, spot: str) -> str:
result += f"注:全部内容输出完以后,最后输出一段固定内容,内容为:<hr class=\"keliu\" data-id=\"{scenic_id}\"/>;"
try:
await redis_client.setex(cache_key, 120, result)
async with redis_client.connection() as conn:
await conn.setex(cache_key, 120, result)
except Exception as e:
print(f"[Redis] 写缓存失败: {e}")
@ -493,12 +495,13 @@ async def get_all_scenic_flow_data(request: Request) -> list:
"""
# Redis 缓存查询
cache_key = "all_scenic_flow_data"
redis_client = request.app.state.redis_client
try:
redis_client = request.app.state.redis_client
cached = await redis_client.get(cache_key)
if cached:
# 缓存命中,直接返回缓存数据
return json.loads(cached)
async with redis_client.connection() as redis_con:
cached = await redis_con.get(cache_key)
if cached:
# 缓存命中,直接返回缓存数据
return json.loads(cached)
except Exception as e:
print(f"[Redis] 查询缓存失败: {e}")
@ -553,7 +556,8 @@ async def get_all_scenic_flow_data(request: Request) -> list:
# 将结果存入Redis缓存,过期时间1分钟
try:
await redis_client.setex(cache_key, 60, json.dumps(result))
async with redis_client.connection() as redis_con:
await redis_con.setex(cache_key, 60, json.dumps(result))
except Exception as e:
print(f"[Redis] 写缓存失败: {e}")
@ -570,12 +574,14 @@ async def get_scenic_detail_data(request: Request, id: int) -> dict:
"""
# Redis 缓存查询
cache_key = f"scenic_detail:{id}"
redis_client = request.app.state.redis_client
try:
redis_client = request.app.state.redis_client
cached = await redis_client.get(cache_key)
if cached:
# 缓存命中,直接返回缓存数据
return json.loads(cached)
async with redis_client.connection() as redis_con:
cached = await redis_con.get(cache_key)
if cached:
# 缓存命中,直接返回缓存数据
return json.loads(cached)
except Exception as e:
print(f"[Redis] 查询缓存失败: {e}")
@ -641,7 +647,8 @@ async def get_scenic_detail_data(request: Request, id: int) -> dict:
# 将结果存入Redis缓存,过期时间1分钟
try:
await redis_client.setex(cache_key, 60, json.dumps(result))
async with redis_client.connection() as redis_con:
await redis_con.setex(cache_key, 60, json.dumps(result))
except Exception as e:
print(f"[Redis] 写缓存失败: {e}")
@ -667,12 +674,14 @@ async def get_scenic_parking_data(request: Request, scenic_id: int, distance: in
"""
# Redis 缓存查询
cache_key = f"scenic_parking:{scenic_id}:{distance}"
redis_client = request.app.state.redis_client
try:
redis_client = request.app.state.redis_client
cached = await redis_client.get(cache_key)
if cached:
# 缓存命中,直接返回缓存数据
return json.loads(cached)
async with redis_client.connection() as redis_con:
cached = await redis_con.get(cache_key)
if cached:
# 缓存命中,直接返回缓存数据
return json.loads(cached)
except Exception as e:
print(f"[Redis] 查询缓存失败: {e}")
@ -744,7 +753,8 @@ async def get_scenic_parking_data(request: Request, scenic_id: int, distance: in
# 将结果存入Redis缓存,过期时间1分钟
try:
await redis_client.setex(cache_key, 60, json.dumps(result))
async with redis_client.connection() as redis_con:
await redis_con.setex(cache_key, 60, json.dumps(result))
except Exception as e:
print(f"[Redis] 写缓存失败: {e}")

Loading…
Cancel
Save