Files

24 lines
656 B
Python

from fastapi import APIRouter, HTTPException
import asyncio
from redis.asyncio import Redis
from app.config import settings
from app.utils.redis import get_redis
router = APIRouter(prefix="/test", tags=["test"])
@router.get("/index")
async def test():
redis = None
try:
redis = get_redis()
await redis.set("test", "123", ex=60)
value = await redis.get("test")
return {"message": "redis测试成功", "value": value}
except Exception as e:
if redis:
try:
await redis.close()
except:
pass
return {"message": f"redis测试失败: {str(e)}"}