|
|
|
@ -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}") |
|
|
|
|
|
|
|
|
|