更新Redis配置
This commit is contained in:
parent
d236602d56
commit
c1d9e1c3fa
|
|
@ -1,8 +1,9 @@
|
|||
from fastapi import APIRouter
|
||||
from app.core.redis import redis_client
|
||||
import redis
|
||||
from fastapi import APIRouter, Depends
|
||||
from app.core.redis import redis_client, get_redis
|
||||
|
||||
redis_test_router = APIRouter()
|
||||
|
||||
@redis_test_router.get("/ping-redis")
|
||||
async def ping_redis():
|
||||
return {"pong": await redis_client.ping()}
|
||||
async def ping_redis(r: redis.Redis = Depends(get_redis)):
|
||||
return {"pong": await r.ping()}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ async def init_redis():
|
|||
global redis_client
|
||||
if redis_client is None:
|
||||
redis_client = await redis.Redis(
|
||||
host="localhost",
|
||||
host="127.0.0.1",
|
||||
port=6379,
|
||||
decode_responses=True, # 返回 str 而不是 Bytes
|
||||
)
|
||||
|
|
@ -26,5 +26,7 @@ async def close_redis():
|
|||
|
||||
# FastAPI 依赖注入用的获取方法
|
||||
async def get_redis() -> AsyncGenerator[redis.Redis, None]:
|
||||
assert redis_client is not None, "Redis 未初始化"
|
||||
global redis_client
|
||||
if redis_client is None:
|
||||
await init_redis() # 懒加载,避免 NoneType
|
||||
yield redis_client
|
||||
|
|
@ -23,7 +23,7 @@ TORTOISE_ORM = {
|
|||
|
||||
ONLINE_SETTINGS = {
|
||||
'connections': {
|
||||
'default': 'mysql://root:@124.221.145.135:3306/test_db',
|
||||
'default': 'mysql://root:@127.0.0.1:3306/test_db',
|
||||
},
|
||||
'apps': {
|
||||
'models': {
|
||||
|
|
|
|||
Loading…
Reference in New Issue