Skip to content

Commit

Permalink
Namespace the cache to orchestrator (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
pboers1988 authored Aug 6, 2024
1 parent e0822a4 commit 8a4bb1f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 2.7.1
current_version = 2.7.2
commit = False
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(rc(?P<build>\d+))?
Expand Down
2 changes: 1 addition & 1 deletion orchestrator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions orchestrator/api/api_v1/endpoints/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@


CACHE_FLUSH_OPTIONS: dict[str, str] = {
"all": "All caches",
"all": "All caches namespaced by `orchestrator:*`",
}


Expand All @@ -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)


Expand Down
12 changes: 6 additions & 6 deletions orchestrator/utils/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -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")
Expand All @@ -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)

Expand Down
12 changes: 9 additions & 3 deletions test/unit_tests/api/test_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/unit_tests/api/test_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion test/unit_tests/utils/get_subscription_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8a4bb1f

Please sign in to comment.