Class RedisIdempotencyRepository
java.lang.Object
io.github.ravocode.avoonce.redis.RedisIdempotencyRepository
- All Implemented Interfaces:
IdempotencyRepository
A distributed
IdempotencyRepository backed by Redis using Jedis.-
Constructor Summary
ConstructorsConstructorDescriptionRedisIdempotencyRepository(RedisOperations redisOperations, IdempotencyConfig config) Constructs a RedisIdempotencyRepository. -
Method Summary
Modifier and TypeMethodDescriptionacquireOrGet(String idempotencyKey) Atomically attempts to save a new record with the state STARTED.acquireOrGet(String idempotencyKey, String requestHash) Atomically attempts to save a new record with the state STARTED, verifying the request hash.voidRemoves the idempotency record associated with the given key.intEvicts all expired records from the underlying storage.Pure read-only check of the current record.voidsaveFailure(String idempotencyKey, String errorMessage) Clears the active lock and updates the state to FAILED.voidsaveSuccess(String idempotencyKey, IdempotencyResponse response) Updates an existing record with a successful payload and sets the state to COMPLETED.
-
Constructor Details
-
RedisIdempotencyRepository
Constructs a RedisIdempotencyRepository.- Parameters:
redisOperations- Redis operations abstractionconfig- Idempotency configuration
-
-
Method Details
-
acquireOrGet
Atomically attempts to save a new record with the state STARTED. If the record already exists and is in a COMPLETED state, it is returned. If a record is in a STARTED state and has not expired, this method should throw anIdempotencyConflictException. If a record is in a STARTED state and has expired, a new record is created.- Specified by:
acquireOrGetin interfaceIdempotencyRepository- Parameters:
idempotencyKey- The unique key for the request.- Returns:
- An optional containing the existing record if it's completed, or an empty optional if a new lock was acquired.
-
acquireOrGet
Atomically attempts to save a new record with the state STARTED, verifying the request hash. If the record already exists but the request hashes do not match, anIdempotencyMismatchExceptionis thrown.- Specified by:
acquireOrGetin interfaceIdempotencyRepository- Parameters:
idempotencyKey- The unique key for the request.requestHash- The hash of the request payload to ensure the key is not reused for a different payload.- Returns:
- An optional containing the existing record if it's completed, or an empty optional if a new lock was acquired.
-
saveSuccess
Updates an existing record with a successful payload and sets the state to COMPLETED.- Specified by:
saveSuccessin interfaceIdempotencyRepository- Parameters:
idempotencyKey- The unique key for the request.response- The response to save.
-
saveFailure
Clears the active lock and updates the state to FAILED. Usually implies the request can be retried by the client.- Specified by:
saveFailurein interfaceIdempotencyRepository- Parameters:
idempotencyKey- The unique key for the request.errorMessage- The error message to save.
-
get
Pure read-only check of the current record.- Specified by:
getin interfaceIdempotencyRepository- Parameters:
idempotencyKey- The unique key for the request.- Returns:
- An optional containing the record if found, empty otherwise.
-
delete
Removes the idempotency record associated with the given key. Useful for administrative cleanup or explicit invalidation.- Specified by:
deletein interfaceIdempotencyRepository- Parameters:
idempotencyKey- The unique key to remove.
-
evictExpired
public int evictExpired()Evicts all expired records from the underlying storage. Useful for periodic cleanup in persistent data stores.- Specified by:
evictExpiredin interfaceIdempotencyRepository- Returns:
- The number of records deleted.
-