Class RedisIdempotencyRepository

java.lang.Object
io.github.ravocode.avoonce.redis.RedisIdempotencyRepository
All Implemented Interfaces:
IdempotencyRepository

public class RedisIdempotencyRepository extends Object implements IdempotencyRepository
A distributed IdempotencyRepository backed by Redis using Jedis.
  • Constructor Details

    • RedisIdempotencyRepository

      public RedisIdempotencyRepository(RedisOperations redisOperations, IdempotencyConfig config)
      Constructs a RedisIdempotencyRepository.
      Parameters:
      redisOperations - Redis operations abstraction
      config - Idempotency configuration
  • Method Details

    • acquireOrGet

      public Optional<IdempotencyRecord> acquireOrGet(String idempotencyKey)
      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 an IdempotencyConflictException. If a record is in a STARTED state and has expired, a new record is created.
      Specified by:
      acquireOrGet in interface IdempotencyRepository
      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

      public Optional<IdempotencyRecord> acquireOrGet(String idempotencyKey, String requestHash)
      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, an IdempotencyMismatchException is thrown.
      Specified by:
      acquireOrGet in interface IdempotencyRepository
      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

      public void saveSuccess(String idempotencyKey, IdempotencyResponse response)
      Updates an existing record with a successful payload and sets the state to COMPLETED.
      Specified by:
      saveSuccess in interface IdempotencyRepository
      Parameters:
      idempotencyKey - The unique key for the request.
      response - The response to save.
    • saveFailure

      public void saveFailure(String idempotencyKey, String errorMessage)
      Clears the active lock and updates the state to FAILED. Usually implies the request can be retried by the client.
      Specified by:
      saveFailure in interface IdempotencyRepository
      Parameters:
      idempotencyKey - The unique key for the request.
      errorMessage - The error message to save.
    • get

      public Optional<IdempotencyRecord> get(String idempotencyKey)
      Pure read-only check of the current record.
      Specified by:
      get in interface IdempotencyRepository
      Parameters:
      idempotencyKey - The unique key for the request.
      Returns:
      An optional containing the record if found, empty otherwise.
    • delete

      public void delete(String idempotencyKey)
      Removes the idempotency record associated with the given key. Useful for administrative cleanup or explicit invalidation.
      Specified by:
      delete in interface IdempotencyRepository
      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:
      evictExpired in interface IdempotencyRepository
      Returns:
      The number of records deleted.