From 491fbc2de0174b83eefe9d02f294417873a4ed7d Mon Sep 17 00:00:00 2001 From: "herve.le-bars" Date: Fri, 25 Oct 2024 11:01:26 +0200 Subject: [PATCH] feat: redis caching service to container factory --- backend/bloom/container.py | 8 ++++++++ backend/bloom/routers/v1/vessels.py | 20 +++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/backend/bloom/container.py b/backend/bloom/container.py index 87b5c359..c0af3a03 100644 --- a/backend/bloom/container.py +++ b/backend/bloom/container.py @@ -13,6 +13,7 @@ from bloom.services.metrics import MetricsService from bloom.usecase.GenerateAlerts import GenerateAlerts from dependency_injector import containers, providers +import redis class UseCases(containers.DeclarativeContainer): @@ -23,6 +24,13 @@ class UseCases(containers.DeclarativeContainer): db_url=db_url, ) + service_cache = providers.Factory( + redis.Redis, + host=settings.redis_host, + port=settings.redis_port, + db=0 + ) + vessel_repository = providers.Factory( VesselRepository, session_factory=db.provided.session, diff --git a/backend/bloom/routers/v1/vessels.py b/backend/bloom/routers/v1/vessels.py index bf396c65..fe087a00 100644 --- a/backend/bloom/routers/v1/vessels.py +++ b/backend/bloom/routers/v1/vessels.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, Depends, HTTPException,Request from redis import Redis from bloom.config import settings from bloom.container import UseCases @@ -24,26 +24,28 @@ rd = redis.Redis(host=settings.redis_host, port=settings.redis_port, db=0, password=settings.redis_password) @router.get("/vessels") -async def list_vessels(nocache:bool=False,key: str = Depends(X_API_KEY_HEADER)): +async def list_vessels(request: Request, + nocache:bool=False, + key: str = Depends(X_API_KEY_HEADER)): + use_cases = UseCases() check_apikey(key) - endpoint=f"/vessels" - cache= rd.get(endpoint) + cache_key=f"{request.url.path}" + cache= use_cases.service_cache().get(cache_key) start = time.time() if cache and not nocache: - logger.debug(f"{endpoint} cached ({settings.redis_cache_expiration})s") + logger.debug(f"{cache_key} cached ({settings.redis_cache_expiration})s") payload=json.loads(cache) - logger.debug(f"{endpoint} elapsed Time: {time.time()-start}") + logger.debug(f"{cache_key} elapsed Time: {time.time()-start}") return payload else: - use_cases = UseCases() vessel_repository = use_cases.vessel_repository() db = use_cases.db() with db.session() as session: json_data = [json.loads(v.model_dump_json() if v else "{}") for v in vessel_repository.get_vessels_list(session)] - rd.set(endpoint, json.dumps(json_data)) - rd.expire(endpoint,settings.redis_cache_expiration) + rd.set(cache_key, json.dumps(json_data)) + rd.expire(cache_key,settings.redis_cache_expiration) return json_data @router.get("/vessels/{vessel_id}")