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