Skip to content

Commit a3cdef8

Browse files
author
Tom McCarthy
committed
chore: move cache conditionals inside of cache methods
1 parent 3876721 commit a3cdef8

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

aws_lambda_powertools/utilities/idempotency/persistence/base.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ def _save_to_cache(self, data_record: DataRecord):
260260
self._cache[data_record.idempotency_key] = data_record
261261

262262
def _retrieve_from_cache(self, idempotency_key: str):
263+
if not self.use_local_cache:
264+
return
263265
cached_record = self._cache.get(idempotency_key)
264266
if cached_record:
265267
if not cached_record.is_expired:
@@ -268,6 +270,8 @@ def _retrieve_from_cache(self, idempotency_key: str):
268270
self._delete_from_cache(idempotency_key)
269271

270272
def _delete_from_cache(self, idempotency_key: str):
273+
if not self.use_local_cache:
274+
return
271275
del self._cache[idempotency_key]
272276

273277
def save_success(self, event: Dict[str, Any], result: dict) -> None:
@@ -296,8 +300,7 @@ def save_success(self, event: Dict[str, Any], result: dict) -> None:
296300
)
297301
self._update_record(data_record=data_record)
298302

299-
if self.use_local_cache:
300-
self._save_to_cache(data_record)
303+
self._save_to_cache(data_record)
301304

302305
def save_inprogress(self, event: Dict[str, Any]) -> None:
303306
"""
@@ -317,10 +320,8 @@ def save_inprogress(self, event: Dict[str, Any]) -> None:
317320

318321
logger.debug(f"Saving in progress record for idempotency key: {data_record.idempotency_key}")
319322

320-
if self.use_local_cache:
321-
cached_record = self._retrieve_from_cache(idempotency_key=data_record.idempotency_key)
322-
if cached_record:
323-
raise IdempotencyItemAlreadyExistsError
323+
if self._retrieve_from_cache(idempotency_key=data_record.idempotency_key):
324+
raise IdempotencyItemAlreadyExistsError
324325

325326
self._put_record(data_record)
326327

@@ -343,8 +344,7 @@ def delete_record(self, event: Dict[str, Any], exception: Exception):
343344
)
344345
self._delete_record(data_record)
345346

346-
if self.use_local_cache:
347-
self._delete_from_cache(data_record.idempotency_key)
347+
self._delete_from_cache(data_record.idempotency_key)
348348

349349
def get_record(self, event: Dict[str, Any]) -> DataRecord:
350350
"""
@@ -370,19 +370,15 @@ def get_record(self, event: Dict[str, Any]) -> DataRecord:
370370

371371
idempotency_key = self._get_hashed_idempotency_key(event)
372372

373-
if self.use_local_cache:
374-
cached_record = self._retrieve_from_cache(idempotency_key=idempotency_key)
375-
if cached_record:
376-
logger.debug(f"Idempotency record found in cache with idempotency key: {idempotency_key}")
377-
self._validate_payload(event, cached_record)
378-
return cached_record
373+
cached_record = self._retrieve_from_cache(idempotency_key=idempotency_key)
374+
if cached_record:
375+
logger.debug(f"Idempotency record found in cache with idempotency key: {idempotency_key}")
376+
self._validate_payload(event, cached_record)
377+
return cached_record
379378

380379
record = self._get_record(idempotency_key)
381380

382-
# We can't cache "INPROGRESS" records as we have no way to reflect updates that can happen outside of the
383-
# execution environment
384-
if self.use_local_cache and not record.status == STATUS_CONSTANTS["INPROGRESS"]:
385-
self._save_to_cache(data_record=record)
381+
self._save_to_cache(data_record=record)
386382

387383
self._validate_payload(event, record)
388384
return record

tests/functional/idempotency/test_idempotency.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def lambda_handler(event, context):
157157
assert retrieve_from_cache_spy.call_count == 2 * loops
158158
retrieve_from_cache_spy.assert_called_with(idempotency_key=hashed_idempotency_key)
159159

160-
save_to_cache_spy.assert_not_called()
160+
save_to_cache_spy.assert_called()
161161
assert persistence_store._cache.get(hashed_idempotency_key) is None
162162

163163
stubber.assert_no_pending_responses()
@@ -595,3 +595,35 @@ def test_data_record_invalid_status_value():
595595
_ = data_record.status
596596

597597
assert e.value.args[0] == "UNSUPPORTED_STATUS"
598+
599+
600+
@pytest.mark.parametrize("persistence_store", [{"use_local_cache": True}], indirect=True)
601+
def test_in_progress_never_saved_to_cache(persistence_store):
602+
# GIVEN a data record with status "INPROGRESS"
603+
# and persistence_store has use_local_cache = True
604+
data_record = DataRecord("key", status="INPROGRESS")
605+
606+
# WHEN saving to local cache
607+
persistence_store._save_to_cache(data_record)
608+
609+
# THEN don't save to local cache
610+
assert persistence_store._cache.get("key") is None
611+
612+
613+
@pytest.mark.parametrize("persistence_store", [{"use_local_cache": False}], indirect=True)
614+
def test_user_local_disabled(persistence_store):
615+
# GIVEN a persistence_store with use_local_cache = False
616+
617+
# WHEN calling any local cache options
618+
data_record = DataRecord("key", status="COMPLETED")
619+
try:
620+
persistence_store._save_to_cache(data_record)
621+
cache_value = persistence_store._retrieve_from_cache("key")
622+
assert cache_value is None
623+
persistence_store._delete_from_cache("key")
624+
except AttributeError as e:
625+
pytest.fail(f"AttributeError should not be raised: {e}")
626+
627+
# THEN raise AttributeError
628+
# AND don't have a _cache attribute
629+
assert not hasattr("persistence_store", "_cache")

0 commit comments

Comments
 (0)