From 1a0d001c7b7fa098bc71953e6618fb5e90440f83 Mon Sep 17 00:00:00 2001 From: Antoine Boucher Date: Sat, 2 Mar 2024 18:49:43 -0500 Subject: [PATCH] Update app.py --- api/app.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/api/app.py b/api/app.py index 4a02736..30259bf 100644 --- a/api/app.py +++ b/api/app.py @@ -3,6 +3,9 @@ from fastapi.responses import HTMLResponse from fastapi.templating import Jinja2Templates from fastapi.security import HTTPBasic, HTTPBasicCredentials +import hashlib +from cachetools import LRUCache +from cachetools.sync import RLock import os import asyncio @@ -693,7 +696,7 @@ class APIResponse(BaseModel): data: Optional[dict] = None -user_instances_cache = {} +user_instances_cache = LRUCache(maxsize=100, lock=RLock()) async def get_user_hash(credentials: HTTPBasicCredentials): """ @@ -719,7 +722,7 @@ async def get_current_user(credentials: HTTPBasicCredentials = Depends(security) # Store the authenticated user instance in the cache user_instances_cache[user_hash] = user except Exception as e: - __LOGGER.error(f"Authentication failed: {e}") + print(f"Authentication failed: {e}") # Adjust the logging mechanism as needed raise HTTPException(status_code=401, detail="Authentication failed") return user