Class CaffeineIdempotencyRepository
java.lang.Object
io.github.ravocode.avoonce.caffeine.CaffeineIdempotencyRepository
- All Implemented Interfaces:
IdempotencyRepository
In-memory
IdempotencyRepository backed by a Caffeine cache.
Suitable for single-node deployments or local testing. TTL-based expiry and
conflict detection are handled entirely in-process via Caffeine's atomic
Cache.asMap() compute operations.
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs aCaffeineIdempotencyRepositorywith the given configuration. -
Method Summary
Modifier and TypeMethodDescriptionacquireOrGet(String idempotencyKey) Delegates toacquireOrGet(String, String)with anullhash, keeping all locking logic in one place.acquireOrGet(String idempotencyKey, String requestHash) Atomically acquires a lock or returns an existing COMPLETED record.voidRemoves the idempotency record for the given key from the in-memory cache.intNo-op for Caffeine: TTL-based expiry is handled automatically by the underlying cache.Retrieves the currentIdempotencyRecordfor the given key without modifying it.voidsaveFailure(String idempotencyKey, String errorMessage) Transitions the record for the given key toIdempotencyStatus.FAILED, allowing the client to safely retry the operation.voidsaveSuccess(String idempotencyKey, IdempotencyResponse response) Transitions the record for the given key toIdempotencyStatus.COMPLETEDand stores the response.
-
Constructor Details
-
CaffeineIdempotencyRepository
Constructs aCaffeineIdempotencyRepositorywith the given configuration. A new Caffeine cache is created with TTL-based expiry matchingconfig.getTtl().- Parameters:
config- the idempotency TTL and lock-timeout configuration.
-
-
Method Details
-
acquireOrGet
Delegates toacquireOrGet(String, String)with anullhash, keeping all locking logic in one place.- 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 acquires a lock or returns an existing COMPLETED record.This override is required to ensure the
requestHashis persisted inside the newly-created STARTED record. Without it, the SPI default would delegate toacquireOrGet(String), producing a record with anullrequestHash and making the mismatch guard permanently ineffective.- 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.
- Throws:
IdempotencyConflictException- if a non-expired STARTED record already exists.IdempotencyMismatchException- if a COMPLETED record exists with a different hash.
-
saveSuccess
Transitions the record for the given key toIdempotencyStatus.COMPLETEDand stores the response.- Specified by:
saveSuccessin interfaceIdempotencyRepository- Parameters:
idempotencyKey- the idempotency key of the completing request.response- the response to cache for future replay.
-
saveFailure
Transitions the record for the given key toIdempotencyStatus.FAILED, allowing the client to safely retry the operation.- Specified by:
saveFailurein interfaceIdempotencyRepository- Parameters:
idempotencyKey- the idempotency key of the failed request.errorMessage- a description of the failure cause (not persisted in Caffeine).
-
get
Retrieves the currentIdempotencyRecordfor the given key without modifying it.- Specified by:
getin interfaceIdempotencyRepository- Parameters:
idempotencyKey- the key to look up.- Returns:
- an
Optionalcontaining the record, or empty if not found or expired.
-
delete
Removes the idempotency record for the given key from the in-memory cache. Useful for administrative invalidation of a specific key.- Specified by:
deletein interfaceIdempotencyRepository- Parameters:
idempotencyKey- The unique key to remove.
-
evictExpired
public int evictExpired()No-op for Caffeine: TTL-based expiry is handled automatically by the underlying cache.- Specified by:
evictExpiredin interfaceIdempotencyRepository- Returns:
- always
0
-