Skip to content

Commit b245c64

Browse files
author
JiayuXu
committed
feat: 生命周期初始化连接 Redis
1 parent 12a804b commit b245c64

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

src/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,25 @@
1616

1717
from core.dependency import get_current_username
1818
from core.exceptions import SettingNotFound
19-
from core.init_app import (
20-
init_data,
21-
make_middlewares,
22-
register_exceptions,
23-
register_routers,
24-
)
19+
from core.init_app import init_data, make_middlewares, register_exceptions, register_routers
2520

2621
try:
2722
from settings.config import settings
2823
except ImportError as e:
2924
raise SettingNotFound("Can not import settings") from e
3025

26+
from utils.cache import cache_manager
27+
3128

3229
@asynccontextmanager
3330
async def lifespan(app: FastAPI):
31+
await cache_manager.connect()
3432
await init_data()
35-
yield
36-
await Tortoise.close_connections()
33+
try:
34+
yield
35+
finally:
36+
await cache_manager.disconnect()
37+
await Tortoise.close_connections()
3738

3839

3940
def create_app() -> FastAPI:

0 commit comments

Comments
 (0)