Skip to content

Commit

Permalink
asynccontextmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
lollerfirst committed Sep 30, 2024
1 parent 5e8da30 commit 659c60f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
18 changes: 15 additions & 3 deletions cashu/mint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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()
'''
10 changes: 1 addition & 9 deletions cashu/mint/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 659c60f

Please sign in to comment.