amoghrajesh commented on code in PR #49779: URL: https://github.com/apache/airflow/pull/49779#discussion_r2073002905
########## task-sdk/src/airflow/sdk/execution_time/cache.py: ########## @@ -0,0 +1,130 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import datetime +import multiprocessing + +from airflow.configuration import conf +from airflow.utils import timezone + + +class SecretCache: + """A static class to manage the global secret cache.""" + + __manager: multiprocessing.managers.SyncManager | None = None + _cache: dict[str, _CacheValue] | None = None + _ttl: datetime.timedelta + + class NotPresentException(Exception): + """Raised when a key is not present in the cache.""" + + class _CacheValue: + def __init__(self, value: str | None) -> None: + self.value = value + self.date = timezone.utcnow() + + def is_expired(self, ttl: datetime.timedelta) -> bool: + return timezone.utcnow() - self.date > ttl + + _VARIABLE_PREFIX = "__v_" + _CONNECTION_PREFIX = "__c_" + + @classmethod + def init(cls): Review Comment: This is good yes but since its porting it over, i would like to keep it the way it was ########## task-sdk/src/airflow/sdk/execution_time/cache.py: ########## @@ -0,0 +1,130 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import datetime +import multiprocessing + +from airflow.configuration import conf +from airflow.utils import timezone + + +class SecretCache: + """A static class to manage the global secret cache.""" + + __manager: multiprocessing.managers.SyncManager | None = None + _cache: dict[str, _CacheValue] | None = None + _ttl: datetime.timedelta + + class NotPresentException(Exception): + """Raised when a key is not present in the cache.""" + + class _CacheValue: + def __init__(self, value: str | None) -> None: + self.value = value + self.date = timezone.utcnow() + + def is_expired(self, ttl: datetime.timedelta) -> bool: + return timezone.utcnow() - self.date > ttl + + _VARIABLE_PREFIX = "__v_" + _CONNECTION_PREFIX = "__c_" + + @classmethod + def init(cls): + """ + Initialize the cache, provided the configuration allows it. + + Safe to call several times. + """ + if cls._cache is not None: + return + use_cache = conf.getboolean(section="secrets", key="use_cache", fallback=False) + if not use_cache: + return + if cls.__manager is None: + # it is not really necessary to save the manager, but doing so allows to reuse it between tests, + # making them run a lot faster because this operation takes ~300ms each time + cls.__manager = multiprocessing.Manager() + cls._cache = cls.__manager.dict() + ttl_seconds = conf.getint(section="secrets", key="cache_ttl_seconds", fallback=15 * 60) + cls._ttl = datetime.timedelta(seconds=ttl_seconds) + + @classmethod + def reset(cls): Review Comment: Same comment as above -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
