level 4: redis cache etc
This commit is contained in:
+7
-2
@@ -10,6 +10,7 @@ from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
|
||||
from .cache import SessionCache, open_cache
|
||||
from .config import load_settings
|
||||
from .db import open_pool
|
||||
from .errors import ApiError
|
||||
@@ -58,12 +59,16 @@ _OPENAPI_PATH = Path(__file__).resolve().parents[1] / 'contracts' / 'openapi.jso
|
||||
async def lifespan(app: FastAPI):
|
||||
settings = load_settings()
|
||||
pool = await open_pool(settings.database_url)
|
||||
cache = SessionCache(open_cache(settings.redis_url))
|
||||
try:
|
||||
await apply_migrations(pool)
|
||||
app.state.store = Store(pool, settings.session_ttl_seconds)
|
||||
app.state.store = Store(pool, cache, settings.session_ttl_seconds)
|
||||
yield
|
||||
finally:
|
||||
await pool.close()
|
||||
try:
|
||||
await cache.close()
|
||||
finally:
|
||||
await pool.close()
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
|
||||
Reference in New Issue
Block a user