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