Skip to content

Commit b969b8d

Browse files
authored
✨ Cache list service request in catalog (ITISFoundation#2874)
1 parent dcad2db commit b969b8d

File tree

2 files changed

+20
-1
lines changed
  • packages/settings-library/src/settings_library
  • services/catalog/src/simcore_service_catalog/api/routes

2 files changed

+20
-1
lines changed

packages/settings-library/src/settings_library/postgres.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ def dsn(self) -> str:
6464

6565
@cached_property
6666
def dsn_with_async_sqlalchemy(self) -> str:
67-
dsn = self.dsn.replace("postgresql", "postgresql+asyncpg")
67+
dsn: str = PostgresDsn.build(
68+
scheme="postgresql+asyncpg",
69+
user=self.POSTGRES_USER,
70+
password=self.POSTGRES_PASSWORD.get_secret_value(),
71+
host=self.POSTGRES_HOST,
72+
port=f"{self.POSTGRES_PORT}",
73+
path=f"/{self.POSTGRES_DB}",
74+
)
6875
return dsn
6976

7077
@cached_property

services/catalog/src/simcore_service_catalog/api/routes/services.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
}
4040

4141
DIRECTOR_CACHING_TTL = 5 * MINUTE
42+
LIST_SERVICES_CACHING_TTL = 30
4243

4344

4445
def _prepare_service_details(
@@ -69,8 +70,19 @@ def _prepare_service_details(
6970
return validated_service
7071

7172

73+
def _build_cache_key(fct, *_, **kwargs):
74+
return f"{fct.__name__}_{kwargs['user_id']}_{kwargs['x_simcore_products_name']}_{kwargs['details']}"
75+
76+
77+
# NOTE: this call is pretty expensive and can be called several times
78+
# (when e2e runs or by the webserver when listing projects) therefore
79+
# a cache is setup here
7280
@router.get("", response_model=List[ServiceOut], **RESPONSE_MODEL_POLICY)
7381
@cancellable_request
82+
@cached(
83+
ttl=LIST_SERVICES_CACHING_TTL,
84+
key_builder=_build_cache_key,
85+
)
7486
async def list_services(
7587
request: Request, # pylint:disable=unused-argument
7688
user_id: PositiveInt,

0 commit comments

Comments
 (0)