Skip to content

Commit

Permalink
✨ Cache list service request in catalog (ITISFoundation#2874)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg authored Mar 11, 2022
1 parent dcad2db commit b969b8d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/settings-library/src/settings_library/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
}

DIRECTOR_CACHING_TTL = 5 * MINUTE
LIST_SERVICES_CACHING_TTL = 30


def _prepare_service_details(
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b969b8d

Please sign in to comment.