From 8a4bb1f25607b2a6579530f4b06d042d26631eb7 Mon Sep 17 00:00:00 2001 From: Peter Boers Date: Tue, 6 Aug 2024 07:53:58 -0400 Subject: [PATCH] Namespace the cache to orchestrator (#730) --- .bumpversion.cfg | 2 +- orchestrator/__init__.py | 2 +- orchestrator/api/api_v1/endpoints/settings.py | 4 ++-- orchestrator/utils/redis.py | 12 ++++++------ test/unit_tests/api/test_caching.py | 12 +++++++++--- test/unit_tests/api/test_subscriptions.py | 6 +++--- test/unit_tests/utils/get_subscription_dict.py | 4 +++- 7 files changed, 25 insertions(+), 17 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index bea52dcb3..81a9e99f7 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 2.7.1 +current_version = 2.7.2 commit = False tag = False parse = (?P\d+)\.(?P\d+)\.(?P\d+)(rc(?P\d+))? diff --git a/orchestrator/__init__.py b/orchestrator/__init__.py index 0264baf75..5cd64b68e 100644 --- a/orchestrator/__init__.py +++ b/orchestrator/__init__.py @@ -13,7 +13,7 @@ """This is the orchestrator workflow engine.""" -__version__ = "2.7.1" +__version__ = "2.7.2" from orchestrator.app import OrchestratorCore from orchestrator.settings import app_settings diff --git a/orchestrator/api/api_v1/endpoints/settings.py b/orchestrator/api/api_v1/endpoints/settings.py index 4030311d5..d0bc59b6e 100644 --- a/orchestrator/api/api_v1/endpoints/settings.py +++ b/orchestrator/api/api_v1/endpoints/settings.py @@ -35,7 +35,7 @@ CACHE_FLUSH_OPTIONS: dict[str, str] = { - "all": "All caches", + "all": "All caches namespaced by `orchestrator:*`", } @@ -45,7 +45,7 @@ async def clear_cache(name: str) -> int | None: if name not in CACHE_FLUSH_OPTIONS: raise_status(HTTPStatus.BAD_REQUEST, "Invalid cache name") - key_name = "*" if name == "all" else f"{name}:*" + key_name = "orchestrator:*" if name == "all" else f"{name}:*" return await delete_keys_matching_pattern(cache, key_name) diff --git a/orchestrator/utils/redis.py b/orchestrator/utils/redis.py index 751289777..631184b5c 100644 --- a/orchestrator/utils/redis.py +++ b/orchestrator/utils/redis.py @@ -41,8 +41,8 @@ def to_redis(subscription: dict[str, Any]) -> str | None: if caching_models_enabled(): logger.info("Setting cache for subscription", subscription=subscription["subscription_id"]) etag = _generate_etag(subscription) - cache.set(f"domain:{subscription['subscription_id']}", json_dumps(subscription), ex=ONE_WEEK) - cache.set(f"domain:etag:{subscription['subscription_id']}", etag, ex=ONE_WEEK) + cache.set(f"orchestrator:domain:{subscription['subscription_id']}", json_dumps(subscription), ex=ONE_WEEK) + cache.set(f"orchestrator:domain:etag:{subscription['subscription_id']}", etag, ex=ONE_WEEK) return etag logger.warning("Caching disabled, not caching subscription", subscription=subscription["subscription_id"]) @@ -53,8 +53,8 @@ def from_redis(subscription_id: UUID) -> tuple[PY_JSON_TYPES, str] | None: log = logger.bind(subscription_id=subscription_id) if caching_models_enabled(): log.debug("Try to retrieve subscription from cache") - obj = cache.get(f"domain:{subscription_id}") - etag = cache.get(f"domain:etag:{subscription_id}") + obj = cache.get(f"orchestrator:domain:{subscription_id}") + etag = cache.get(f"orchestrator:domain:etag:{subscription_id}") if obj and etag: log.info("Retrieved subscription from cache") return json_loads(obj), etag.decode("utf-8") @@ -67,8 +67,8 @@ def from_redis(subscription_id: UUID) -> tuple[PY_JSON_TYPES, str] | None: def delete_from_redis(subscription_id: UUID) -> None: if caching_models_enabled(): logger.info("Deleting subscription object from cache", subscription_id=subscription_id) - cache.delete(f"domain:{subscription_id}") - cache.delete(f"domain:etag:{subscription_id}") + cache.delete(f"orchestrator:domain:{subscription_id}") + cache.delete(f"orchestrator:domain:etag:{subscription_id}") else: logger.warning("Caching disabled, not deleting subscription", subscription=subscription_id) diff --git a/test/unit_tests/api/test_caching.py b/test/unit_tests/api/test_caching.py index 01db8f65d..9a284cc71 100644 --- a/test/unit_tests/api/test_caching.py +++ b/test/unit_tests/api/test_caching.py @@ -28,7 +28,9 @@ def test_cache_update_customer_description( # Add domainmodel to cache to_redis(extended_model) - cache_fixture.extend([f"domain:{generic_subscription_1}", f"domain:etag:{generic_subscription_1}"]) + cache_fixture.extend( + [f"orchestrator:domain:{generic_subscription_1}", f"orchestrator:domain:etag:{generic_subscription_1}"] + ) # Retrieve domain-model, customer description should be as inserted response1 = test_client.get(URL("api/subscriptions/domain-model") / generic_subscription_1) @@ -66,7 +68,9 @@ def test_cache_delete_customer_description( # Add domainmodel to cache to_redis(extended_model) - cache_fixture.extend([f"domain:{generic_subscription_1}", f"domain:etag:{generic_subscription_1}"]) + cache_fixture.extend( + [f"orchestrator:domain:{generic_subscription_1}", f"orchestrator:domain:etag:{generic_subscription_1}"] + ) # Retrieve domain-model, customer description should be as inserted response1 = test_client.get(URL("api/subscriptions/domain-model") / generic_subscription_1) @@ -93,7 +97,9 @@ def test_cache_create_customer_description( # Add domainmodel to cache to_redis(extended_model) - cache_fixture.extend([f"domain:{generic_subscription_1}", f"domain:etag:{generic_subscription_1}"]) + cache_fixture.extend( + [f"orchestrator:domain:{generic_subscription_1}", f"orchestrator:domain:etag:{generic_subscription_1}"] + ) # Retrieve domain-model, customer description should be empty response1 = test_client.get(URL("api/subscriptions/domain-model") / generic_subscription_1) diff --git a/test/unit_tests/api/test_subscriptions.py b/test/unit_tests/api/test_subscriptions.py index 797d1b474..5327c1d51 100644 --- a/test/unit_tests/api/test_subscriptions.py +++ b/test/unit_tests/api/test_subscriptions.py @@ -719,16 +719,16 @@ def test_subscription_detail_with_domain_model_cache(test_client, generic_subscr response = test_client.get(URL("api/subscriptions/domain-model") / generic_subscription_1) cache = Redis.from_url(str(app_settings.CACHE_URI)) - result = cache.get(f"domain:{generic_subscription_1}") + result = cache.get(f"orchestrator:domain:{generic_subscription_1}") cached_model = json_dumps(json_loads(result)) - cached_etag = cache.get(f"domain:etag:{generic_subscription_1}") + cached_etag = cache.get(f"orchestrator:domain:etag:{generic_subscription_1}") assert cached_model == json_dumps(extended_model) assert cached_etag.decode("utf-8") == etag assert response.status_code == HTTPStatus.OK assert response.json()["subscription_id"] == generic_subscription_1 app_settings.CACHE_DOMAIN_MODELS = False - cache.delete(f"domain:{generic_subscription_1}") + cache.delete(f"orchestrator:domain:{generic_subscription_1}") def test_subscription_detail_with_in_use_by_ids_filtered_self(test_client, product_one_subscription_1): diff --git a/test/unit_tests/utils/get_subscription_dict.py b/test/unit_tests/utils/get_subscription_dict.py index c3298a0ab..c6b062855 100644 --- a/test/unit_tests/utils/get_subscription_dict.py +++ b/test/unit_tests/utils/get_subscription_dict.py @@ -29,7 +29,9 @@ async def test_get_subscription_dict_cache(generate_etag, generic_subscription_1 # Add domainmodel to cache to_redis(extended_model) - cache_fixture.extend([f"domain:{generic_subscription_1}", f"domain:etag:{generic_subscription_1}"]) + cache_fixture.extend( + [f"orchestrator:domain:{generic_subscription_1}", f"orchestrator:domain:etag:{generic_subscription_1}"] + ) generate_etag.side_effect = Mock(return_value="etag-mock") await get_subscription_dict(generic_subscription_1)