From b969b8d7a2f4e0cb4bba15a7738a53c6c5960f2a Mon Sep 17 00:00:00 2001 From: Sylvain <35365065+sanderegg@users.noreply.github.com> Date: Fri, 11 Mar 2022 08:23:32 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Cache=20list=20service=20request=20?= =?UTF-8?q?in=20catalog=20(#2874)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/settings_library/postgres.py | 9 ++++++++- .../simcore_service_catalog/api/routes/services.py | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/settings-library/src/settings_library/postgres.py b/packages/settings-library/src/settings_library/postgres.py index fc49d501f0b..bcbcefa4345 100644 --- a/packages/settings-library/src/settings_library/postgres.py +++ b/packages/settings-library/src/settings_library/postgres.py @@ -64,7 +64,14 @@ def dsn(self) -> str: @cached_property def dsn_with_async_sqlalchemy(self) -> str: - dsn = self.dsn.replace("postgresql", "postgresql+asyncpg") + dsn: str = PostgresDsn.build( + scheme="postgresql+asyncpg", + user=self.POSTGRES_USER, + password=self.POSTGRES_PASSWORD.get_secret_value(), + host=self.POSTGRES_HOST, + port=f"{self.POSTGRES_PORT}", + path=f"/{self.POSTGRES_DB}", + ) return dsn @cached_property diff --git a/services/catalog/src/simcore_service_catalog/api/routes/services.py b/services/catalog/src/simcore_service_catalog/api/routes/services.py index 257d073b0a0..248cc5582b0 100644 --- a/services/catalog/src/simcore_service_catalog/api/routes/services.py +++ b/services/catalog/src/simcore_service_catalog/api/routes/services.py @@ -39,6 +39,7 @@ } DIRECTOR_CACHING_TTL = 5 * MINUTE +LIST_SERVICES_CACHING_TTL = 30 def _prepare_service_details( @@ -69,8 +70,19 @@ def _prepare_service_details( return validated_service +def _build_cache_key(fct, *_, **kwargs): + return f"{fct.__name__}_{kwargs['user_id']}_{kwargs['x_simcore_products_name']}_{kwargs['details']}" + + +# NOTE: this call is pretty expensive and can be called several times +# (when e2e runs or by the webserver when listing projects) therefore +# a cache is setup here @router.get("", response_model=List[ServiceOut], **RESPONSE_MODEL_POLICY) @cancellable_request +@cached( + ttl=LIST_SERVICES_CACHING_TTL, + key_builder=_build_cache_key, +) async def list_services( request: Request, # pylint:disable=unused-argument user_id: PositiveInt,