From cb41339a66ed93de1eb0be08d6a705651f1c023f Mon Sep 17 00:00:00 2001 From: Pedro Crespo-Valero <32402063+pcrespov@users.noreply.github.com> Date: Mon, 12 Aug 2024 15:29:37 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=97=EF=B8=8F=20env-vars=20to=20control=20?= =?UTF-8?q?cachetools=20(#6169)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../catalog/_api_units.py | 1 - .../catalog/_models.py | 26 ++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/services/web/server/src/simcore_service_webserver/catalog/_api_units.py b/services/web/server/src/simcore_service_webserver/catalog/_api_units.py index 76a84842d49..a0367b60030 100644 --- a/services/web/server/src/simcore_service_webserver/catalog/_api_units.py +++ b/services/web/server/src/simcore_service_webserver/catalog/_api_units.py @@ -45,7 +45,6 @@ def replace_service_input_outputs( ): """Thin wrapper to replace i/o ports in returned service model""" # This is a fast solution until proper models are available for the web API - for input_key in service["inputs"]: new_input: ServiceInputGet = ( ServiceInputGetFactory.from_catalog_service_api_model( diff --git a/services/web/server/src/simcore_service_webserver/catalog/_models.py b/services/web/server/src/simcore_service_webserver/catalog/_models.py index 59f0b5827c3..969b9a9f52d 100644 --- a/services/web/server/src/simcore_service_webserver/catalog/_models.py +++ b/services/web/server/src/simcore_service_webserver/catalog/_models.py @@ -1,3 +1,5 @@ +import logging +import os from dataclasses import dataclass from typing import Any, Final @@ -11,6 +13,8 @@ from models_library.services import BaseServiceIOModel from pint import PintError, UnitRegistry +_logger = logging.getLogger(__name__) + def get_unit_name(port: BaseServiceIOModel) -> str | None: unit: str | None = port.unit @@ -56,10 +60,10 @@ def get_html_formatted_unit( # - the least recently used items will be discarded first to make space when necessary. # -_CACHE_MAXSIZE: Final = ( - 100 # number of items i.e. ServiceInputGet/ServiceOutputGet insteances -) -_CACHE_TTL: Final = 60 # secs +_CACHE_MAXSIZE: Final = int( + os.getenv("CACHETOOLS_CACHE_MAXSIZE", "100") +) # number of items i.e. ServiceInputGet/ServiceOutputGet instances +_CACHE_TTL: Final = int(os.getenv("CACHETOOLS_CACHE_TTL_SECS", "60")) # secs def _hash_inputs( @@ -71,9 +75,19 @@ def _hash_inputs( return f"{service['key']}/{service['version']}/{input_key}" +def _cachetools_cached(*args, **kwargs): + def decorator(func): + if os.getenv("CACHETOOLS_DISABLE", "0") == "0": + return cachetools.cached(*args, **kwargs)(func) + _logger.warning("cachetools disabled") + return func + + return decorator + + class ServiceInputGetFactory: @staticmethod - @cachetools.cached( + @_cachetools_cached( cachetools.TTLCache(ttl=_CACHE_TTL, maxsize=_CACHE_MAXSIZE), key=_hash_inputs ) def from_catalog_service_api_model( @@ -107,7 +121,7 @@ def _hash_outputs( class ServiceOutputGetFactory: @staticmethod - @cachetools.cached( + @_cachetools_cached( cachetools.TTLCache(ttl=_CACHE_TTL, maxsize=_CACHE_MAXSIZE), key=_hash_outputs ) def from_catalog_service_api_model(