vandonr-amz commented on code in PR #30259:
URL: https://github.com/apache/airflow/pull/30259#discussion_r1282410789


##########
airflow/secrets/cache.py:
##########
@@ -0,0 +1,127 @@
+#
+# 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
+
+
+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 NotPresent(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 = datetime.datetime.utcnow()
+
+        def is_expired(self, ttl: datetime.timedelta) -> bool:
+            return datetime.datetime.utcnow() - self.date > ttl
+
+    _VARIABLE_PREFIX = "__v_"
+    _CONNECTION_PREFIX = "__c_"
+
+    @classmethod
+    def init(cls):
+        """Initializes 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):
+        """For test purposes only."""
+        cls._cache = None
+
+    @classmethod
+    def get_variable(cls, key: str) -> str | None:
+        """
+        Tries to get the value associated with the key from the cache.

Review Comment:
   I can add it, but it'd look like `:param key: the key`, only adding clutter 
imho.
   Also, it's not enforced by static checks, so my feeling is that if we feel 
that adding docstring for params is useless, we shouldn't do it.



-- 
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]

Reply via email to