Search in sources :

Example 1 with TransactionResult

use of io.lettuce.core.TransactionResult in project tutorials by eugenp.

the class LettuceIntegrationLiveTest method givenMultipleOperationsThatNeedToBeExecutedAtomically_thenWrapThemInATransaction.

@Test
public void givenMultipleOperationsThatNeedToBeExecutedAtomically_thenWrapThemInATransaction() throws Exception {
    RedisAsyncCommands<String, String> asyncCommands = redisConnection.async();
    // Start a transaction
    asyncCommands.multi();
    // Add three sets to it, and save the future responses
    RedisFuture<String> result1 = asyncCommands.set("key1", "value1");
    RedisFuture<String> result2 = asyncCommands.set("key2", "value2");
    RedisFuture<String> result3 = asyncCommands.set("key3", "value3");
    // Execute it
    RedisFuture<TransactionResult> execResult = asyncCommands.exec();
    TransactionResult transactionResult = execResult.get();
    // Get the three results in the transaction return
    String firstResult = transactionResult.get(0);
    String secondResult = transactionResult.get(0);
    String thirdResult = transactionResult.get(0);
    // Our results are in both!
    assertTrue(firstResult.equals("OK"));
    assertTrue(secondResult.equals("OK"));
    assertTrue(thirdResult.equals("OK"));
    assertTrue(result1.get().equals("OK"));
    assertTrue(result2.get().equals("OK"));
    assertTrue(result3.get().equals("OK"));
}
Also used : TransactionResult(io.lettuce.core.TransactionResult) Test(org.junit.Test)

Example 2 with TransactionResult

use of io.lettuce.core.TransactionResult in project sandbox by backpaper0.

the class CasExample method run.

@Override
public void run() {
    try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
        RedisCommands<String, String> commands = connection.sync();
        for (int i = 0; i < 100; i++) {
            while (true) {
                commands.watch(key);
                int value = Integer.parseInt(commands.get(key));
                value++;
                commands.multi();
                commands.set(key, String.valueOf(value));
                TransactionResult result = commands.exec();
                commands.unwatch();
                if (result.isEmpty() == false && Objects.equals(result.get(0), "OK")) {
                    break;
                }
            }
        }
    }
}
Also used : TransactionResult(io.lettuce.core.TransactionResult)

Example 3 with TransactionResult

use of io.lettuce.core.TransactionResult in project di-authentication-api by alphagov.

the class RedisConnectionService method popValue.

public String popValue(String key) {
    try (StatefulRedisConnection<String, String> connection = pool.borrowObject()) {
        RedisCommands<String, String> commands = connection.sync();
        commands.multi();
        commands.get(key);
        commands.del(key);
        TransactionResult result = commands.exec();
        return result.get(0);
    } catch (Exception e) {
        return null;
    }
}
Also used : TransactionResult(io.lettuce.core.TransactionResult)

Example 4 with TransactionResult

use of io.lettuce.core.TransactionResult in project lettuce-core by lettuce-io.

the class CustomCommandIntegrationTests method standaloneAsyncBatchTransaction.

@Test
void standaloneAsyncBatchTransaction() {
    RedisCommand<String, String, String> multi = new Command<>(CommandType.MULTI, new StatusOutput<>(StringCodec.UTF8));
    RedisCommand<String, String, String> set = new Command<>(CommandType.SET, new StatusOutput<>(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).addKey("key").add("value"));
    RedisCommand<String, String, TransactionResult> exec = new Command<>(CommandType.EXEC, null);
    AsyncCommand<String, String, String> async1 = new AsyncCommand<>(multi);
    AsyncCommand<String, String, String> async2 = new AsyncCommand<>(set);
    AsyncCommand<String, String, TransactionResult> async3 = new AsyncCommand<>(exec);
    getStandaloneConnection().dispatch(Arrays.asList(async1, async2, async3));
    assertThat(TestFutures.getOrTimeout(async1.toCompletableFuture())).isEqualTo("OK");
    assertThat(TestFutures.getOrTimeout(async2.toCompletableFuture())).isEqualTo("OK");
    TransactionResult transactionResult = TestFutures.getOrTimeout(async3.toCompletableFuture());
    assertThat(transactionResult.wasDiscarded()).isFalse();
    assertThat(transactionResult.<String>get(0)).isEqualTo("OK");
}
Also used : TransactionResult(io.lettuce.core.TransactionResult) Command(io.lettuce.core.protocol.Command) AsyncCommand(io.lettuce.core.protocol.AsyncCommand) RedisCommand(io.lettuce.core.protocol.RedisCommand) AsyncCommand(io.lettuce.core.protocol.AsyncCommand) Test(org.junit.jupiter.api.Test)

Example 5 with TransactionResult

use of io.lettuce.core.TransactionResult in project lettuce-core by lettuce-io.

the class CustomCommandIntegrationTests method dispatchTransactions.

@Test
void dispatchTransactions() {
    redis.multi();
    String response = redis.dispatch(CommandType.SET, new StatusOutput<>(StringCodec.UTF8), new CommandArgs<>(StringCodec.UTF8).addKey(key).addValue(value));
    TransactionResult exec = redis.exec();
    assertThat(response).isNull();
    assertThat(exec).hasSize(1).contains("OK");
}
Also used : TransactionResult(io.lettuce.core.TransactionResult) Test(org.junit.jupiter.api.Test)

Aggregations

TransactionResult (io.lettuce.core.TransactionResult)7 Test (org.junit.jupiter.api.Test)3 AsyncCommand (io.lettuce.core.protocol.AsyncCommand)1 Command (io.lettuce.core.protocol.Command)1 RedisCommand (io.lettuce.core.protocol.RedisCommand)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Test (org.junit.Test)1