From b713235787ec25a09eb3f39a9812b4623879d23b Mon Sep 17 00:00:00 2001 From: lollerfirst Date: Tue, 24 Sep 2024 16:10:35 +0200 Subject: [PATCH] fix --- cashu/mint/app.py | 8 +++++--- cashu/mint/router.py | 44 +++++++++++++++++++++----------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/cashu/mint/app.py b/cashu/mint/app.py index d6e6e414..db706874 100644 --- a/cashu/mint/app.py +++ b/cashu/mint/app.py @@ -51,7 +51,7 @@ def create_app(config_object="core.settings") -> FastAPI: return app app = create_app() -FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache") + # Add middlewares add_middlewares(app) @@ -104,11 +104,13 @@ async def catch_exceptions(request: Request, call_next): @app.on_event("startup") async def startup_mint(): - FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache") + if settings.mint_cache_activate: + FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache") await start_mint_init() @app.on_event("shutdown") async def shutdown_mint(): - FastAPICache.clear() + if settings.mint_cache_activate: + await FastAPICache.clear() await shutdown_mint_init() \ No newline at end of file diff --git a/cashu/mint/router.py b/cashu/mint/router.py index 2cbf2c63..3c445e48 100644 --- a/cashu/mint/router.py +++ b/cashu/mint/router.py @@ -1,14 +1,14 @@ import asyncio import time +import hashlib + +from typing import Callable, Any, Optional, Tuple, Dict from contextlib import asynccontextmanager from fastapi import APIRouter, WebSocket -from fastapi_cache import FastAPICache -from fastapi_cache.backends.redis import RedisBackend -from fastapi_cache.backends.inmemory import InMemoryBackend +from fastapi_cache.decorator import cache from starlette.requests import Request from starlette.responses import Response -from redis import asyncio as aioredis from fastapi_cache.decorator import cache from loguru import logger @@ -44,28 +44,26 @@ async def get_cache(): return 1 + def request_key_builder( - func, + func: Callable[..., Any], namespace: str = "", *, - request: Request, - response: Response, - **kwargs, -): - if request.method.lower() == 'get': - return ":".join([ - namespace, - request.method.lower(), - request.url.path, - repr(sorted(request.query_params.items())) - ]) - elif request.method.lower() == 'post': - return ":".join([ - namespace, - request.method.lower(), - request.url.path, - repr(request.json()) - ]) + request: Optional[Request] = None, + response: Optional[Response] = None, + args: Tuple[Any, ...], + kwargs: Dict[str, Any], +) -> str: + cache_key = "" + if request and request.method.lower() == 'get': + cache_key = hashlib.md5( # noqa: S324 + f"{func.__module__}:{func.__name__}:{repr(sorted(request.query_params.items()))}:{args}:{kwargs}".encode() + ).hexdigest() + elif request and request.method.lower() == 'post': + cache_key = hashlib.md5( # noqa: S324 + f"{func.__module__}:{func.__name__}:{repr(request.json())}:{args}:{kwargs}".encode() + ).hexdigest() + return f"{namespace}:{cache_key}" @router.get( "/v1/info",