Class LettuceOperations<K,V>

java.lang.Object
com.ohalee.database.redis.LettuceOperations<K,V>
Type Parameters:
K - Key type (e.g., String)
V - Value type (e.g., String)
Direct Known Subclasses:
StringRedisOperations

public abstract class LettuceOperations<K,V> extends Object
Enhanced Redis operations wrapper using the Lettuce driver.

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:
  • Constructor Details

    • LettuceOperations

      public LettuceOperations()
  • Method Details

    • connection

      protected abstract io.lettuce.core.api.StatefulRedisConnection<K,V> 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 via RedisAsyncCommands.
      Returns:
      a CompletableFuture representing the result of the operation.
    • executeSync

      public <T> T executeSync(LettuceOperations.SyncRedisOperation<K,V,T> operation)
      Executes a synchronous Redis operation using try-with-resources.
      Type Parameters:
      T - the type of the result.
      Parameters:
      operation - the operation to execute via RedisCommands.
      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 DISCARD is called if an error occurs during the queuing phase (before EXEC).

      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.