Skip to content

Commit 0bc087e

Browse files
committed
[ELI-619] - switching approach to base the cache off the consumer id
1 parent a6b8563 commit 0bc087e

7 files changed

Lines changed: 17 additions & 32 deletions

File tree

src/eligibility_signposting_api/config/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
CONSUMER_ID = "NHSE-Product-ID"
77
ALLOWED_CONDITIONS = Literal["COVID", "FLU", "MMR", "RSV"]
88
CONSUMER_MAPPING_FILE_NAME = "consumer_mapping_config.json"
9+
RESERVED_TEST_CONSUMER_IDS = {"test-consumer-1", "test-consumer-2", "test-consumer-3"}
910

10-
ttl = {
11+
TTL = {
1112
"local": 0,
1213
"test": 300,
1314
"dev": 300,

src/eligibility_signposting_api/repos/campaign_repo.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from wireup import Inject, service
1111

1212
from eligibility_signposting_api.model.campaign_config import CampaignConfig, Rules
13-
from eligibility_signposting_api.config.constants import ttl
13+
from eligibility_signposting_api.config.constants import TTL, RESERVED_TEST_CONSUMER_IDS
1414

1515
BucketName = NewType("BucketName", str)
1616

@@ -32,14 +32,14 @@ def __init__(
3232
self.bucket_name = bucket_name
3333
self._campaign_configs_cache: list[CampaignConfig] | None = None
3434
self._cache_expiry_epoch: float = 0.0
35-
self._cache_ttl_seconds: int = int(ttl.get(os.getenv("ENVIRONMENT"), 0))
35+
self._cache_ttl_seconds: int = int(TTL.get(os.getenv("ENVIRONMENT"), 0))
3636

37-
def get_campaign_configs(self, bypass_cache: bool = False) -> Generator[CampaignConfig, None, None]:
37+
def get_campaign_configs(self, consumer_id: str) -> Generator[CampaignConfig, None, None]:
3838
now = time.time()
3939
cache_enabled = self._cache_ttl_seconds > 0
4040
cache_valid = (
4141
cache_enabled
42-
and not bypass_cache
42+
and consumer_id not in RESERVED_TEST_CONSUMER_IDS
4343
and self._campaign_configs_cache is not None
4444
and now < self._cache_expiry_epoch
4545
)
@@ -51,13 +51,13 @@ def get_campaign_configs(self, bypass_cache: bool = False) -> Generator[Campaign
5151
return
5252

5353
logger.info(
54-
"Refreshing campaign configs from S3 (bypass_cache=%s, ttl_seconds=%s)",
55-
bypass_cache,
54+
"Refreshing campaign configs from S3 (consumer_id=%s, ttl_seconds=%s)",
55+
consumer_id,
5656
self._cache_ttl_seconds,
5757
)
5858
campaign_configs = self._load_campaign_configs_from_s3()
5959

60-
if cache_enabled and not bypass_cache:
60+
if cache_enabled and consumer_id not in RESERVED_TEST_CONSUMER_IDS:
6161
self._campaign_configs_cache = campaign_configs
6262
self._cache_expiry_epoch = now + self._cache_ttl_seconds
6363

src/eligibility_signposting_api/services/eligibility_services.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def get_eligibility_status(
4242
conditions: list[str],
4343
category: str,
4444
consumer_id: str,
45-
bypass_campaign_config_cache: bool = False,
4645
) -> eligibility_status.EligibilityStatus:
4746
"""Calculate a person's eligibility for vaccination given an NHS number."""
4847
if nhs_number:
@@ -52,9 +51,7 @@ def get_eligibility_status(
5251
raise UnknownPersonError from e
5352
else:
5453
campaign_configs: list[CampaignConfig] = list(
55-
self.campaign_repo.get_campaign_configs(
56-
bypass_cache=bypass_campaign_config_cache
57-
)
54+
self.campaign_repo.get_campaign_configs(consumer_id)
5855
)
5956
permitted_campaign_configs = self.__collect_permitted_campaign_configs(
6057
campaign_configs, ConsumerId(consumer_id)

src/eligibility_signposting_api/views/eligibility.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
Status.not_actionable: eligibility_response.Status.not_actionable,
2828
Status.not_eligible: eligibility_response.Status.not_eligible,
2929
}
30-
31-
BYPASS_CAMPAIGN_CONFIG_CACHE_HEADER = "X-Bypass-Campaign-Config-Cache"
32-
3330
logger = logging.getLogger(__name__)
3431

3532
eligibility_blueprint = Blueprint("eligibility", __name__)
@@ -55,7 +52,6 @@ def check_eligibility(
5552

5653
query_params = _get_or_default_query_params()
5754
consumer_id = _get_consumer_id_from_headers()
58-
bypass_campaign_config_cache = _should_bypass_campaign_config_cache()
5955

6056
try:
6157
eligibility_status = eligibility_service.get_eligibility_status(
@@ -64,7 +60,6 @@ def check_eligibility(
6460
query_params["conditions"],
6561
query_params["category"],
6662
consumer_id,
67-
bypass_campaign_config_cache=bypass_campaign_config_cache,
6863
)
6964
except UnknownPersonError:
7065
return handle_unknown_person_error(nhs_number)
@@ -81,11 +76,6 @@ def _get_consumer_id_from_headers() -> ConsumerId:
8176
return ConsumerId(request.headers.get(CONSUMER_ID, ""))
8277

8378

84-
def _should_bypass_campaign_config_cache() -> bool:
85-
value = request.headers.get(BYPASS_CAMPAIGN_CONFIG_CACHE_HEADER, "")
86-
return value.lower() == "true"
87-
88-
8979
def _get_or_default_query_params() -> dict[str, Any]:
9080
default_query_params = {"category": "ALL", "conditions": ["ALL"], "includeActions": "Y"}
9181

tests/integration/repo/test_campaign_repo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_get_campaign_config(s3_client: BaseClient, rules_bucket: BucketName, ca
2727
repo = CampaignRepo(s3_client, rules_bucket)
2828

2929
# When
30-
actual = list(repo.get_campaign_configs())
30+
actual = list(repo.get_campaign_configs("consumer_id"))
3131

3232
# Then
3333
assert_that(

tests/unit/repos/test_campaign_repo.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_get_campaign_configs_loads_from_s3(self, repo, mock_s3_client, rules_pa
3737
}
3838
mock_s3_client.get_object.return_value = make_s3_body(rules_payload)
3939

40-
result = list(repo.get_campaign_configs())
40+
result = list(repo.get_campaign_configs("consumer_id"))
4141

4242
assert len(result) == 1
4343
assert result[0].id == rules_payload["campaign_config"]["id"]
@@ -67,8 +67,8 @@ def test_get_campaign_configs_uses_cache_within_ttl(
6767

6868
monkeypatch.setattr("time.time", lambda: 1000.0)
6969

70-
first = list(repo.get_campaign_configs())
71-
second = list(repo.get_campaign_configs())
70+
first = list(repo.get_campaign_configs("consumer_id"))
71+
second = list(repo.get_campaign_configs("consumer_id"))
7272

7373
assert first[0].version == 1
7474
assert second[0].version == 1
@@ -97,11 +97,11 @@ def test_get_campaign_configs_refreshes_after_ttl_expiry(
9797
current_time = {"value": 1000.0}
9898
monkeypatch.setattr("time.time", lambda: current_time["value"])
9999

100-
first = list(repo.get_campaign_configs())
100+
first = list(repo.get_campaign_configs("consumer_id"))
101101
current_time["value"] = 1030.0
102-
second = list(repo.get_campaign_configs())
102+
second = list(repo.get_campaign_configs("consumer_id"))
103103
current_time["value"] = 1061.0
104-
third = list(repo.get_campaign_configs())
104+
third = list(repo.get_campaign_configs("test-consumer-1"))
105105

106106
assert first[0].version == 1
107107
assert second[0].version == 1

tests/unit/views/test_eligibility.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def get_eligibility_status(
6262
_conditions: list[str],
6363
_category: str,
6464
_consumer_id: str,
65-
bypass_campaign_config_cache: bool = False,
6665
) -> EligibilityStatus:
6766
return EligibilityStatusFactory.build()
6867

@@ -78,7 +77,6 @@ def get_eligibility_status(
7877
_conditions: list[str],
7978
_category: str,
8079
_consumer_id: str,
81-
bypass_campaign_config_cache: bool = False,
8280
) -> EligibilityStatus:
8381
raise UnknownPersonError
8482

@@ -94,7 +92,6 @@ def get_eligibility_status(
9492
_conditions: list[str],
9593
_category: str,
9694
_consumer_id: str,
97-
bypass_campaign_config_cache: bool = False,
9895
) -> EligibilityStatus:
9996
raise ValueError
10097

0 commit comments

Comments
 (0)