Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lollerfirst committed Sep 24, 2024
1 parent c8ce417 commit b713235
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
8 changes: 5 additions & 3 deletions cashu/mint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
44 changes: 21 additions & 23 deletions cashu/mint/router.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit b713235

Please sign in to comment.