You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by ep...@apache.org on 2023/08/10 13:27:52 UTC

[airflow] 02/04: Add a cache to `Variable` and `Connection` when called at dag parsing time (#30259)

This is an automated email from the ASF dual-hosted git repository.

ephraimanierobi pushed a commit to branch v2-7-test
in repository https://gitbox.apache.org/repos/asf/airflow.git

commit ba2a2db7bce06906bd460b443545178c24e863bf
Author: Raphaƫl Vandon <va...@amazon.com>
AuthorDate: Thu Aug 10 06:05:11 2023 -0700

    Add a cache to `Variable` and `Connection` when called at dag parsing time (#30259)
    
    (cherry picked from commit dc8ad0caf4da663daf39305070813d0801ebc147)
---
 airflow/config_templates/config.yml    |  25 ++++++
 airflow/dag_processing/manager.py      |   5 ++
 airflow/models/connection.py           |  11 +++
 airflow/models/variable.py             |  24 +++++-
 airflow/secrets/cache.py               | 125 +++++++++++++++++++++++++++++
 docs/apache-airflow/best-practices.rst |  23 ++++--
 tests/always/test_secrets.py           |   8 ++
 tests/models/test_variable.py          |  39 +++++++++
 tests/secrets/test_cache.py            | 142 +++++++++++++++++++++++++++++++++
 9 files changed, 391 insertions(+), 11 deletions(-)

diff --git a/airflow/config_templates/config.yml b/airflow/config_templates/config.yml
index f61f6baca0..995b4acc83 100644
--- a/airflow/config_templates/config.yml
+++ b/airflow/config_templates/config.yml
@@ -1097,6 +1097,31 @@ secrets:
       sensitive: true
       example: ~
       default: ""
+    use_cache:
+      description: |
+        .. note:: |experimental|
+
+        Enables local caching of Variables, when parsing DAGs only.
+        Using this option can make dag parsing faster if Variables are used in top level code, at the expense
+        of longer propagation time for changes.
+        Please note that this cache concerns only the DAG parsing step. There is no caching in place when DAG
+        tasks are run.
+      version_added: 2.6.0
+      type: boolean
+      example: ~
+      default: "False"
+    cache_ttl_seconds:
+      description: |
+        .. note:: |experimental|
+
+        When the cache is enabled, this is the duration for which we consider an entry in the cache to be
+        valid. Entries are refreshed if they are older than this many seconds.
+        It means that when the cache is enabled, this is the maximum amount of time you need to wait to see a
+        Variable change take effect.
+      version_added: 2.6.0
+      type: integer
+      example: ~
+      default: "900"
 cli:
   description: ~
   options:
diff --git a/airflow/dag_processing/manager.py b/airflow/dag_processing/manager.py
index dd252db9bd..04697650db 100644
--- a/airflow/dag_processing/manager.py
+++ b/airflow/dag_processing/manager.py
@@ -51,6 +51,7 @@ from airflow.models.dag import DagModel
 from airflow.models.dagwarning import DagWarning
 from airflow.models.db_callback_request import DbCallbackRequest
 from airflow.models.serialized_dag import SerializedDagModel
+from airflow.secrets.cache import SecretCache
 from airflow.stats import Stats
 from airflow.utils import timezone
 from airflow.utils.file import list_py_file_paths, might_contain_dag
@@ -1073,6 +1074,10 @@ class DagFileProcessorManager(LoggingMixin):
 
     def start_new_processes(self):
         """Start more processors if we have enough slots and files to process."""
+        # initialize cache to mutualize calls to Variable.get in DAGs
+        # needs to be done before this process is forked to create the DAG parsing processes.
+        SecretCache.init()
+
         while self._parallelism - len(self._processors) > 0 and self._file_path_queue:
             file_path = self._file_path_queue.popleft()
             # Stop creating duplicate processor i.e. processor with the same filepath
diff --git a/airflow/models/connection.py b/airflow/models/connection.py
index 18c6bcce59..fe514cdf48 100644
--- a/airflow/models/connection.py
+++ b/airflow/models/connection.py
@@ -30,6 +30,7 @@ from airflow.configuration import ensure_secrets_loaded
 from airflow.exceptions import AirflowException, AirflowNotFoundException, RemovedInAirflow3Warning
 from airflow.models.base import ID_LEN, Base
 from airflow.models.crypto import get_fernet
+from airflow.secrets.cache import SecretCache
 from airflow.utils.log.logging_mixin import LoggingMixin
 from airflow.utils.log.secrets_masker import mask_secret
 from airflow.utils.module_loading import import_string
@@ -451,10 +452,20 @@ class Connection(Base, LoggingMixin):
         :param conn_id: connection id
         :return: connection
         """
+        # check cache first
+        # enabled only if SecretCache.init() has been called first
+        try:
+            uri = SecretCache.get_connection_uri(conn_id)
+            return Connection(conn_id=conn_id, uri=uri)
+        except SecretCache.NotPresentException:
+            pass  # continue business
+
+        # iterate over backends if not in cache (or expired)
         for secrets_backend in ensure_secrets_loaded():
             try:
                 conn = secrets_backend.get_connection(conn_id=conn_id)
                 if conn:
+                    SecretCache.save_connection_uri(conn_id, conn.get_uri())
                     return conn
             except Exception:
                 log.exception(
diff --git a/airflow/models/variable.py b/airflow/models/variable.py
index edfe2dea96..a434a29e2b 100644
--- a/airflow/models/variable.py
+++ b/airflow/models/variable.py
@@ -29,6 +29,7 @@ from airflow.api_internal.internal_api_call import internal_api_call
 from airflow.configuration import ensure_secrets_loaded
 from airflow.models.base import ID_LEN, Base
 from airflow.models.crypto import get_fernet
+from airflow.secrets.cache import SecretCache
 from airflow.secrets.metastore import MetastoreBackend
 from airflow.utils.log.logging_mixin import LoggingMixin
 from airflow.utils.log.secrets_masker import mask_secret
@@ -175,6 +176,10 @@ class Variable(Base, LoggingMixin):
         Variable.delete(key, session=session)
         session.add(Variable(key=key, val=stored_value, description=description))
         session.flush()
+        # invalidate key in cache for faster propagation
+        # we cannot save the value set because it's possible that it's shadowed by a custom backend
+        # (see call to check_for_write_conflict above)
+        SecretCache.invalidate_variable(key)
 
     @staticmethod
     @provide_session
@@ -210,7 +215,9 @@ class Variable(Base, LoggingMixin):
 
         :param key: Variable Keys
         """
-        return session.execute(delete(Variable).where(Variable.key == key)).rowcount
+        rows = session.execute(delete(Variable).where(Variable.key == key)).rowcount
+        SecretCache.invalidate_variable(key)
+        return rows
 
     def rotate_fernet_key(self):
         """Rotate Fernet Key."""
@@ -256,15 +263,26 @@ class Variable(Base, LoggingMixin):
         :param key: Variable Key
         :return: Variable Value
         """
+        # check cache first
+        # enabled only if SecretCache.init() has been called first
+        try:
+            return SecretCache.get_variable(key)
+        except SecretCache.NotPresentException:
+            pass  # continue business
+
+        var_val = None
+        # iterate over backends if not in cache (or expired)
         for secrets_backend in ensure_secrets_loaded():
             try:
                 var_val = secrets_backend.get_variable(key=key)
                 if var_val is not None:
-                    return var_val
+                    break
             except Exception:
                 log.exception(
                     "Unable to retrieve variable from secrets backend (%s). "
                     "Checking subsequent secrets backend.",
                     type(secrets_backend).__name__,
                 )
-        return None
+
+        SecretCache.save_variable(key, var_val)  # we save None as well
+        return var_val
diff --git a/airflow/secrets/cache.py b/airflow/secrets/cache.py
new file mode 100644
index 0000000000..c4f4ec5065
--- /dev/null
+++ b/airflow/secrets/cache.py
@@ -0,0 +1,125 @@
+#
+# 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 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 = 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.
+
+        :return: The saved value (which can be None) if present in cache and not expired,
+            a NotPresent exception otherwise.
+        """
+        return cls._get(key, cls._VARIABLE_PREFIX)
+
+    @classmethod
+    def get_connection_uri(cls, conn_id: str) -> str:
+        """
+        Tries to get the uri associated with the conn_id from the cache.
+
+        :return: The saved uri if present in cache and not expired,
+            a NotPresent exception otherwise.
+        """
+        val = cls._get(conn_id, cls._CONNECTION_PREFIX)
+        if val:  # there shouldn't be any empty entries in the connections cache, but we enforce it here.
+            return val
+        raise cls.NotPresentException
+
+    @classmethod
+    def _get(cls, key: str, prefix: str) -> str | None:
+        if cls._cache is None:
+            # using an exception for misses allow to meaningfully cache None values
+            raise cls.NotPresentException
+
+        val = cls._cache.get(f"{prefix}{key}")
+        if val and not val.is_expired(cls._ttl):
+            return val.value
+        raise cls.NotPresentException
+
+    @classmethod
+    def save_variable(cls, key: str, value: str | None):
+        """Saves the value for that key in the cache, if initialized."""
+        cls._save(key, value, cls._VARIABLE_PREFIX)
+
+    @classmethod
+    def save_connection_uri(cls, conn_id: str, uri: str):
+        """Saves the uri representation for that connection in the cache, if initialized."""
+        if uri is None:
+            # connections raise exceptions if not present, so we shouldn't have any None value to save.
+            return
+        cls._save(conn_id, uri, cls._CONNECTION_PREFIX)
+
+    @classmethod
+    def _save(cls, key: str, value: str | None, prefix: str):
+        if cls._cache is not None:
+            cls._cache[f"{prefix}{key}"] = cls._CacheValue(value)
+
+    @classmethod
+    def invalidate_variable(cls, key: str):
+        """Invalidates (actually removes) the value stored in the cache for that Variable."""
+        if cls._cache is not None:
+            # second arg ensures no exception if key is absent
+            cls._cache.pop(f"{cls._VARIABLE_PREFIX}{key}", None)
diff --git a/docs/apache-airflow/best-practices.rst b/docs/apache-airflow/best-practices.rst
index b7202ef5a0..6cb2857d96 100644
--- a/docs/apache-airflow/best-practices.rst
+++ b/docs/apache-airflow/best-practices.rst
@@ -200,10 +200,13 @@ Some cases of dynamic DAG generation are described in the :doc:`howto/dynamic-da
 Airflow Variables
 -----------------
 
-As mentioned in the previous chapter, :ref:`best_practices/top_level_code`. you should avoid
-using Airflow Variables at top level Python code of DAGs. You can use the Airflow Variables freely inside the
-``execute()`` methods of the operators, but you can also pass the Airflow Variables to the existing operators
-via Jinja template, which will delay reading the value until the task execution.
+Using Airflow Variables yields network calls and database access, so their usage in top-level Python code for DAGs
+should be avoided as much as possible, as mentioned in the previous chapter, :ref:`best_practices/top_level_code`.
+If Airflow Variables must be used in top-level DAG code, then their impact on DAG parsing can be mitigated by
+:ref:`enabling the experimental cache<config:secrets__use_cache>`, configured with a sensible :ref:`ttl<config:secrets__cache_ttl_seconds>`.
+
+You can use the Airflow Variables freely inside the ``execute()`` methods of the operators, but you can also pass the
+Airflow Variables to the existing operators via Jinja template, which will delay reading the value until the task execution.
 
 The template syntax to do this is:
 
@@ -217,7 +220,11 @@ or if you need to deserialize a json object from the variable :
 
     {{ var.json.<variable_name> }}
 
-In top-level code, variables using jinja templates do not produce a request until a task is running, whereas, ``Variable.get()`` produces a request every time the dag file is parsed by the scheduler. Using ``Variable.get()`` will lead to suboptimal performance in the dag file processing. In some cases this can cause the dag file to timeout before it is fully parsed.
+In top-level code, variables using jinja templates do not produce a request until a task is running, whereas,
+``Variable.get()`` produces a request every time the dag file is parsed by the scheduler if caching is not enabled.
+Using ``Variable.get()`` without :ref:`enabling caching<config:secrets__use_cache>` will lead to suboptimal
+performance in the dag file processing.
+In some cases this can cause the dag file to timeout before it is fully parsed.
 
 Bad example:
 
@@ -225,20 +232,20 @@ Bad example:
 
     from airflow.models import Variable
 
-    foo_var = Variable.get("foo")  # DON'T DO THAT
+    foo_var = Variable.get("foo")  # AVOID THAT
     bash_use_variable_bad_1 = BashOperator(
         task_id="bash_use_variable_bad_1", bash_command="echo variable foo=${foo_env}", env={"foo_env": foo_var}
     )
 
     bash_use_variable_bad_2 = BashOperator(
         task_id="bash_use_variable_bad_2",
-        bash_command=f"echo variable foo=${Variable.get('foo')}",  # DON'T DO THAT
+        bash_command=f"echo variable foo=${Variable.get('foo')}",  # AVOID THAT
     )
 
     bash_use_variable_bad_3 = BashOperator(
         task_id="bash_use_variable_bad_3",
         bash_command="echo variable foo=${foo_env}",
-        env={"foo_env": Variable.get("foo")},  # DON'T DO THAT
+        env={"foo_env": Variable.get("foo")},  # AVOID THAT
     )
 
 
diff --git a/tests/always/test_secrets.py b/tests/always/test_secrets.py
index d00720d6ab..25245903bf 100644
--- a/tests/always/test_secrets.py
+++ b/tests/always/test_secrets.py
@@ -21,11 +21,15 @@ from unittest import mock
 
 from airflow.configuration import ensure_secrets_loaded, initialize_secrets_backends
 from airflow.models import Connection, Variable
+from airflow.secrets.cache import SecretCache
 from tests.test_utils.config import conf_vars
 from tests.test_utils.db import clear_db_variables
 
 
 class TestConnectionsFromSecrets:
+    def setup_method(self) -> None:
+        SecretCache.reset()
+
     @mock.patch("airflow.secrets.metastore.MetastoreBackend.get_connection")
     @mock.patch("airflow.secrets.environment_variables.EnvironmentVariablesBackend.get_connection")
     def test_get_connection_second_try(self, mock_env_get, mock_meta_get):
@@ -114,6 +118,7 @@ class TestConnectionsFromSecrets:
 class TestVariableFromSecrets:
     def setup_method(self) -> None:
         clear_db_variables()
+        SecretCache.reset()
 
     def teardown_method(self) -> None:
         clear_db_variables()
@@ -126,7 +131,10 @@ class TestVariableFromSecrets:
         Metastore DB
         """
         mock_env_get.return_value = None
+        mock_meta_get.return_value = "val"
+
         Variable.get_variable_from_secrets("fake_var_key")
+
         mock_meta_get.assert_called_once_with(key="fake_var_key")
         mock_env_get.assert_called_once_with(key="fake_var_key")
 
diff --git a/tests/models/test_variable.py b/tests/models/test_variable.py
index 67e267b8ca..3242d4e9f6 100644
--- a/tests/models/test_variable.py
+++ b/tests/models/test_variable.py
@@ -26,6 +26,7 @@ from cryptography.fernet import Fernet
 
 from airflow import settings
 from airflow.models import Variable, crypto, variable
+from airflow.secrets.cache import SecretCache
 from airflow.secrets.metastore import MetastoreBackend
 from tests.test_utils import db
 from tests.test_utils.config import conf_vars
@@ -36,6 +37,9 @@ class TestVariable:
     def setup_test_cases(self):
         crypto._fernet = None
         db.clear_db_variables()
+        SecretCache.reset()
+        with conf_vars({("secrets", "use_cache"): "true"}):
+            SecretCache.init()
         with mock.patch("airflow.models.variable.mask_secret", autospec=True) as m:
             self.mask_secret = m
             yield
@@ -102,8 +106,13 @@ class TestVariable:
         caplog.set_level(logging.WARNING, logger=variable.log.name)
         Variable.set("key", "db-value")
         with mock.patch.dict("os.environ", AIRFLOW_VAR_KEY="env-value"):
+            # setting value while shadowed by an env variable will generate a warning
             Variable.set("key", "new-db-value")
+            # value set above is not returned because the env variable value takes priority
             assert "env-value" == Variable.get("key")
+        # invalidate the cache to re-evaluate value
+        SecretCache.invalidate_variable("key")
+        # now that env var is not here anymore, we see the value we set before.
         assert "new-db-value" == Variable.get("key")
 
         assert caplog.messages[0] == (
@@ -259,6 +268,34 @@ class TestVariable:
         finally:
             session.rollback()
 
+    @mock.patch("airflow.models.variable.ensure_secrets_loaded")
+    def test_caching_caches(self, mock_ensure_secrets: mock.Mock):
+        mock_backend = mock.Mock()
+        mock_backend.get_variable.return_value = "secret_val"
+        mock_backend.__class__.__name__ = "MockSecretsBackend"
+        mock_ensure_secrets.return_value = [mock_backend, MetastoreBackend]
+
+        key = "doesn't matter"
+        first = Variable.get(key)
+        second = Variable.get(key)
+
+        mock_backend.get_variable.assert_called_once()  # second call was not made because of cache
+        assert first == second
+
+    def test_cache_invalidation_on_set(self):
+        with mock.patch.dict("os.environ", AIRFLOW_VAR_KEY="from_env"):
+            a = Variable.get("key")  # value is saved in cache
+        with mock.patch.dict("os.environ", AIRFLOW_VAR_KEY="from_env_two"):
+            b = Variable.get("key")  # value from cache is used
+        assert a == b
+
+        # setting a new value invalidates the cache
+        Variable.set("key", "new_value")
+
+        c = Variable.get("key")  # cache should not be used
+
+        assert c != b
+
 
 @pytest.mark.parametrize(
     "variable_value, deserialize_json, expected_masked_values",
@@ -272,6 +309,8 @@ class TestVariable:
 def test_masking_only_secret_values(variable_value, deserialize_json, expected_masked_values):
     from airflow.utils.log.secrets_masker import _secrets_masker
 
+    SecretCache.reset()
+
     session = settings.Session()
 
     try:
diff --git a/tests/secrets/test_cache.py b/tests/secrets/test_cache.py
new file mode 100644
index 0000000000..8d45080868
--- /dev/null
+++ b/tests/secrets/test_cache.py
@@ -0,0 +1,142 @@
+# 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
+
+import pytest
+
+from airflow.secrets.cache import SecretCache
+from tests.test_utils.config import conf_vars
+
+
+def test_cache_disabled_by_default():
+    SecretCache.init()
+    SecretCache.save_variable("test", "not saved")
+    with pytest.raises(SecretCache.NotPresentException):
+        SecretCache.get_variable("test")
+    assert SecretCache._cache is None
+
+
+class TestSecretCache:
+    @staticmethod
+    @conf_vars({("secrets", "use_cache"): "true"})
+    def setup_method() -> None:
+        SecretCache.init()
+
+    @staticmethod
+    def teardown_method(self) -> None:
+        SecretCache.reset()
+
+    def test_cache_accessible_from_other_process(self):
+        def writer():
+            SecretCache.save_variable("key", "secret_val")
+
+        def reader(pipe: multiprocessing.connection.Connection):
+            v = SecretCache.get_variable("key")
+            pipe.send(v)
+            pipe.close()
+
+        SecretCache.init()  # init needs to be explicit before creating threads
+        c = multiprocessing.get_context("fork")
+        # run first process that's going to set a key in the cache
+        p1 = c.Process(target=writer)
+        p1.start()
+        p1.join()
+        # setup pipe to receive what second process read (returns reading and writing ends of the pipe)
+        r, w = c.Pipe(duplex=False)
+        # start second process that's going to try to get the same key from the cache
+        p2 = c.Process(target=reader, args=(w,))
+        p2.start()
+        w.close()  # close pipe on our end because it's used by the child process
+        val = r.recv()
+        p2.join()
+
+        assert val is not None
+        assert val == "secret_val"
+
+    def test_returns_none_when_not_init(self):
+        with pytest.raises(SecretCache.NotPresentException):
+            SecretCache.get_variable("whatever")
+
+    def test_cache_saves_none_as_sentinel(self):
+        SecretCache.save_variable("key", None)
+
+        res = SecretCache.get_variable("key")
+
+        assert res is None
+
+    def test_invalidate(self):
+        SecretCache.save_variable("key", "some_value")
+
+        assert SecretCache.get_variable("key") == "some_value"
+
+        SecretCache.invalidate_variable("key")
+
+        # cannot get the value for that key anymore because we invalidated it
+        with pytest.raises(SecretCache.NotPresentException):
+            SecretCache.get_variable("key")
+
+    def test_invalidate_key_not_present(self):
+        SecretCache.invalidate_variable("not present")  # simply shouldn't raise any exception.
+
+    def test_expiration(self):
+        SecretCache.save_variable("key", "some_value")
+
+        assert SecretCache.get_variable("key") == "some_value"
+
+        SecretCache._ttl = datetime.timedelta(0)  # I don't want to sleep()
+
+        # value is now seen as expired
+        with pytest.raises(SecretCache.NotPresentException):
+            SecretCache.get_variable("key")
+
+    @conf_vars({("secrets", "use_cache"): "0"})
+    def test_disabled(self):
+        # do init to have it read config
+        SecretCache.reset()
+        SecretCache.init()
+
+        SecretCache.save_variable("key", "some_value")  # will be ignored
+
+        # cache is disabled, gets will always "fail"
+        with pytest.raises(SecretCache.NotPresentException):
+            SecretCache.get_variable("key")
+
+    def test_independence_variable_connection(self):
+        SecretCache.save_variable("same_key", "some_value")
+        SecretCache.save_connection_uri("same_key", "some_other_value")
+
+        assert SecretCache.get_variable("same_key") == "some_value"
+        assert SecretCache.get_connection_uri("same_key") == "some_other_value"
+
+        SecretCache.save_variable("var", "some_value")
+        SecretCache.save_connection_uri("conn", "some_other_value")
+
+        # getting the wrong type of thing with a key that exists in the other will not work
+        with pytest.raises(SecretCache.NotPresentException):
+            SecretCache.get_connection_uri("var")
+        with pytest.raises(SecretCache.NotPresentException):
+            SecretCache.get_variable("conn")
+
+    def test_connections_do_not_save_none(self):
+        # noinspection PyTypeChecker
+        SecretCache.save_connection_uri("key", None)
+
+        with pytest.raises(SecretCache.NotPresentException):
+            SecretCache.get_connection_uri("key")