更新Redis配置
This commit is contained in:
parent
9372cb353d
commit
22a5af4ecb
|
|
@ -0,0 +1,8 @@
|
|||
from fastapi import APIRouter
|
||||
from app.core.redis import redis_client
|
||||
|
||||
redis_test_router = APIRouter()
|
||||
|
||||
@redis_test_router.get("/ping-redis")
|
||||
async def ping_redis():
|
||||
return {"pong": await redis_client.ping()}
|
||||
|
|
@ -1,18 +1,30 @@
|
|||
import redis.asyncio as redis
|
||||
from typing import AsyncGenerator
|
||||
from typing import AsyncGenerator, Optional
|
||||
|
||||
# 全局 Redis 客户端
|
||||
redis_client: redis.Redis
|
||||
redis_client: Optional[redis.Redis] = None
|
||||
|
||||
# 初始化 Redis(应用启动时调用)
|
||||
async def init_redis_pool():
|
||||
async def init_redis():
|
||||
global redis_client
|
||||
redis_client = await redis.Redis(
|
||||
host="localhost",
|
||||
port=6379,
|
||||
decode_responses=True, # 返回 str 而不是 Bytes
|
||||
)
|
||||
if redis_client is None:
|
||||
redis_client = await redis.Redis(
|
||||
host="localhost",
|
||||
port=6379,
|
||||
decode_responses=True, # 返回 str 而不是 Bytes
|
||||
)
|
||||
await redis_client.ping()
|
||||
|
||||
async def close_redis():
|
||||
global redis_client
|
||||
if redis_client:
|
||||
try:
|
||||
await redis_client.close()
|
||||
except Exception:
|
||||
pass
|
||||
redis_client = None
|
||||
|
||||
# FastAPI 依赖注入用的获取方法
|
||||
async def get_redis() -> AsyncGenerator[redis.Redis, None]:
|
||||
assert redis_client is not None, "Redis 未初始化"
|
||||
yield redis_client
|
||||
|
|
@ -12,7 +12,7 @@ from app.models.base import ReservedWords, User
|
|||
|
||||
from settings import settings
|
||||
|
||||
redis_client = redis.Redis(host="localhost", port=6379, decode_responses=True)
|
||||
redis_client = redis.Redis(host="127.0.0.1", port=6379, decode_responses=True)
|
||||
ALGORITHM = "HS256"
|
||||
|
||||
|
||||
|
|
|
|||
14
main.py
14
main.py
|
|
@ -5,20 +5,24 @@ from fastapi.middleware.cors import CORSMiddleware
|
|||
import uvicorn
|
||||
from tortoise.contrib.fastapi import register_tortoise
|
||||
|
||||
from app.api.redis_test import redis_test_router
|
||||
from app.utils import redis_client
|
||||
from settings import TORTOISE_ORM
|
||||
from app.api.users import users_router
|
||||
from app.api.admin.router import admin_router
|
||||
from app.api.search import dict_search
|
||||
from app.core.redis import init_redis_pool
|
||||
from app.core.redis import init_redis, close_redis
|
||||
import app.models.signals
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
await init_redis_pool()
|
||||
yield
|
||||
await redis_client.close() # 清理资源
|
||||
# ---- startup ----
|
||||
await init_redis()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
await close_redis()
|
||||
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
|
@ -48,5 +52,7 @@ app.include_router(users_router, tags=["User API"], prefix="/users")
|
|||
app.include_router(admin_router, tags=["Administrator API"], prefix="/admin")
|
||||
app.include_router(dict_search, tags=["Dictionary Search API"])
|
||||
|
||||
app.include_router(redis_test_router, tags=["Redis Test-Only API"])
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)
|
||||
|
|
|
|||
Loading…
Reference in New Issue