Class LettuceOperations<K,V>
- Type Parameters:
K- Key type (e.g., String)V- Value type (e.g., String)
- Direct Known Subclasses:
StringRedisOperations
This class abstracts the management of Redis connections for Synchronous, Asynchronous, and Transactional operations.
Important Implementation Note: The connection() method is expected
to return a new or pooled connection. This class calls close()
on the connection after every operation. If a shared/singleton connection is returned,
this class will close it, rendering the application unable to communicate with Redis.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for asynchronous Redis operations.static interfaceFunctional interface for synchronous Redis operations. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionProviders a dedicated Redis connection for the current operation.<T> CompletableFuture<T> executeAsync(LettuceOperations.AsyncRedisOperation<K, V, T> operation) Executes an asynchronous Redis operation.<T> TexecuteSync(LettuceOperations.SyncRedisOperation<K, V, T> operation) Executes a synchronous Redis operation using try-with-resources.CompletableFuture<io.lettuce.core.TransactionResult> Executes an asynchronous transaction (MULTI ...io.lettuce.core.TransactionResultexecuteTransactionSync(LettuceOperations.SyncRedisOperation<K, V, Void> operation) Executes a synchronous transaction (MULTI ...
-
Constructor Details
-
LettuceOperations
public LettuceOperations()
-
-
Method Details
-
connection
Providers a dedicated Redis connection for the current operation.The connection provided here will be closed (or returned to the pool) automatically after the operation completes.
- Returns:
- a StatefulRedisConnection instance.
-
executeAsync
public <T> CompletableFuture<T> executeAsync(LettuceOperations.AsyncRedisOperation<K, V, T> operation) Executes an asynchronous Redis operation.The connection is automatically closed when the future completes (successfully or exceptionally).
- Type Parameters:
T- the type of the result.- Parameters:
operation- the operation to execute viaRedisAsyncCommands.- Returns:
- a CompletableFuture representing the result of the operation.
-
executeSync
Executes a synchronous Redis operation using try-with-resources.- Type Parameters:
T- the type of the result.- Parameters:
operation- the operation to execute viaRedisCommands.- Returns:
- the result of the operation.
- Throws:
RedisOperationException- if a Redis-specific error or general exception occurs.
-
executeTransactionSync
public io.lettuce.core.TransactionResult executeTransactionSync(LettuceOperations.SyncRedisOperation<K, V, Void> operation) Executes a synchronous transaction (MULTI ... EXEC).This method guarantees that
DISCARDis called if an error occurs during the queuing phase (beforeEXEC).- Parameters:
operation- the operation to execute within the transaction context.- Returns:
- the TransactionResult containing the results of all queued commands.
- Throws:
RedisOperationException- if the transaction fails.
-
executeTransactionAsync
public CompletableFuture<io.lettuce.core.TransactionResult> executeTransactionAsync(LettuceOperations.AsyncRedisOperation<K, V, Void> operation) Executes an asynchronous transaction (MULTI ... EXEC).- Parameters:
operation- the operation to execute within a transaction.- Returns:
- a CompletableFuture containing the TransactionResult.
-