-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve subscription caching with domain model endpoint and graphql s…
…ubscriptions (#663) * Improve subscription caching with get domain model and graphql subscriptions - change get_subscription_dict to a util function and add subscription to cache when not fetched from cache. - use get_subscription_dict for get domain model endpoint and in graphql get_subscription_details * Bump version to 2.3.0rc4 * Remove to_redis in get_subscription_dict and use _generate_etag * Add docstring to get_subscription_dict and add unit tests --------- Co-authored-by: Mark90 <[email protected]>
- Loading branch information
Showing
10 changed files
with
95 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from uuid import UUID | ||
|
||
from orchestrator.domain.base import SubscriptionModel | ||
from orchestrator.services.subscriptions import _generate_etag, build_extended_domain_model | ||
from orchestrator.utils.redis import from_redis | ||
|
||
|
||
async def get_subscription_dict(subscription_id: UUID) -> tuple[dict, str]: | ||
"""Helper function to get subscription dict by uuid from db or cache.""" | ||
|
||
if cached_model := from_redis(subscription_id): | ||
return cached_model # type: ignore | ||
|
||
subscription_model = SubscriptionModel.from_subscription(subscription_id) | ||
subscription = build_extended_domain_model(subscription_model) | ||
etag = _generate_etag(subscription) | ||
return subscription, etag |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from os import getenv | ||
from unittest import mock | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
|
||
from orchestrator import app_settings | ||
from orchestrator.domain.base import SubscriptionModel | ||
from orchestrator.services.subscriptions import build_extended_domain_model | ||
from orchestrator.utils.get_subscription_dict import get_subscription_dict | ||
from orchestrator.utils.redis import to_redis | ||
|
||
|
||
@mock.patch.object(app_settings, "CACHE_DOMAIN_MODELS", False) | ||
@mock.patch("orchestrator.utils.get_subscription_dict._generate_etag") | ||
async def test_get_subscription_dict_db(generate_etag, generic_subscription_1): | ||
generate_etag.side_effect = Mock(return_value="etag-mock") | ||
await get_subscription_dict(generic_subscription_1) | ||
assert generate_etag.called | ||
|
||
|
||
@pytest.mark.skipif( | ||
not getenv("AIOCACHE_DISABLE", "0") == "0", reason="AIOCACHE must be enabled for this test to do anything" | ||
) | ||
@mock.patch("orchestrator.utils.get_subscription_dict._generate_etag") | ||
async def test_get_subscription_dict_cache(generate_etag, generic_subscription_1, cache_fixture): | ||
subscription = SubscriptionModel.from_subscription(generic_subscription_1) | ||
extended_model = build_extended_domain_model(subscription) | ||
|
||
# Add domainmodel to cache | ||
to_redis(extended_model) | ||
cache_fixture.extend([f"domain:{generic_subscription_1}", f"domain:etag:{generic_subscription_1}"]) | ||
|
||
generate_etag.side_effect = Mock(return_value="etag-mock") | ||
await get_subscription_dict(generic_subscription_1) | ||
assert not generate_etag.called |