Duplicate Prevention Demo
Show how repeated calls return cached results
01
Sample Code
from psp.platform.idempotency import InMemoryIdempotencyStore, check_idempotency
store = InMemoryIdempotencyStore()
call_count = 0
def expensive_operation():
global call_count
call_count += 1
return {"order_id": "ORD-123", "total": 99.99}
# First call - operation executes
result1 = check_idempotency(store, "order-key-abc", expensive_operation)
print(f"Call count: {call_count}") # 1
# Second call - returns cached result, operation skipped
result2 = check_idempotency(store, "order-key-abc", expensive_operation)
print(f"Call count: {call_count}") # Still 1!
# Results are identical
assert result1 == result2
# Different key - operation executes again
result3 = check_idempotency(store, "order-key-xyz", expensive_operation)
print(f"Call count: {call_count}") # 2