Skip to content

Commit

Permalink
⚗️ env-vars to control cachetools (#6169)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov authored Aug 12, 2024
1 parent 248fba2 commit cb41339
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging
import os
from dataclasses import dataclass
from typing import Any, Final

Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit cb41339

Please sign in to comment.