diff --git a/cashu/mint/app.py b/cashu/mint/app.py index f7eccfc3..ad3f6059 100644 --- a/cashu/mint/app.py +++ b/cashu/mint/app.py @@ -14,6 +14,10 @@ from .router_deprecated import router_deprecated from .startup import shutdown_mint as shutdown_mint_init from .startup import start_mint_init +from contextlib import asynccontextmanager +from collections.abc import AsyncIterator +from fastapi_cache import FastAPICache +from fastapi_cache.backends.inmemory import InMemoryBackend if settings.debug_profiling: pass @@ -32,6 +36,13 @@ # yield # # shutdown routines here +@asynccontextmanager +async def lifespan(_: FastAPI) -> AsyncIterator[None]: + FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache") + await start_mint_init() + yield + await shutdown_mint_init() + await FastAPICache.clear() def create_app(config_object="core.settings") -> FastAPI: configure_logger() @@ -44,6 +55,7 @@ def create_app(config_object="core.settings") -> FastAPI: "name": "MIT License", "url": "https://raw.githubusercontent.com/cashubtc/cashu/main/LICENSE", }, + lifespan=lifespan if settings.mint_cache_activate else None, ) return app @@ -99,12 +111,12 @@ async def catch_exceptions(request: Request, call_next): app.include_router(router=router, tags=["Mint"]) app.include_router(router=router_deprecated, tags=["Deprecated"], deprecated=True) - +''' @app.on_event("startup") async def startup_mint(): - await start_mint_init() + @app.on_event("shutdown") async def shutdown_mint(): - await shutdown_mint_init() \ No newline at end of file +''' \ No newline at end of file diff --git a/cashu/mint/router.py b/cashu/mint/router.py index cfa815e5..34976050 100644 --- a/cashu/mint/router.py +++ b/cashu/mint/router.py @@ -4,8 +4,6 @@ from typing import Callable, Any, Optional, Tuple, Dict -from contextlib import asynccontextmanager -from collections.abc import AsyncIterator from fastapi import APIRouter, WebSocket from fastapi_cache.decorator import cache from starlette.requests import Request @@ -35,18 +33,12 @@ PostSwapRequest, PostSwapResponse, ) -from fastapi_cache import FastAPICache -from fastapi_cache.backends.inmemory import InMemoryBackend from ..core.settings import settings from ..mint.startup import ledger from .limit import limit_websocket, limiter -@asynccontextmanager -async def lifespan(_: APIRouter) -> AsyncIterator[None]: - FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache") - yield -router = APIRouter(lifespan=lifespan) if settings.mint_cache_activate else APIRouter() +router = APIRouter() @cache() async def get_cache():