一、如何在redission使用pipeline
Redisson supports pipelining. Multiple operations can be batched as a single atomic operation. This is facilitated by the RBatch class. Multiple commands are aggregated against an RBatch object instance before they are executed.
RBatch batch = client.createBatch();
batch.getMap("ledgerMap").fastPutAsync("1", "2");
batch.getMap("ledgerMap").putAsync("2", "5");
BatchResult<?> batchResult = batch.execute();
二、关于RBatch
https://www.javadoc.io/doc/org.redisson/redisson/3.5.6/org/redisson/api/RBatch.html
三、代码案例
package org.redisson.example.objects;
import java.util.concurrent.ExecutionException;
import org.redisson.Redisson;
import org.redisson.api.BatchOptions;
import org.redisson.api.BatchResult;
import org.redisson.api.RBatch;
import org.redisson.api.RFuture;
import org.redisson.api.RedissonClient;
public class BatchExamples {
public static void main(String[] args) throws ExecutionException, InterruptedException {
RedissonClient redisson = Redisson.create();
RBatch batch = redisson.createBatch(BatchOptions.defaults());
batch.getMap("test1").fastPutAsync("1", "2");
batch.getMap("test2").fastPutAsync("2", "3");
batch.getMap("test3").putAsync("2", "5");
RFuture<Long> future = batch.getAtomicLong("counter").incrementAndGetAsync();
batch.getAtomicLong("counter").incrementAndGetAsync();
future.whenComplete((res, exception) -> {
});
BatchResult<?> res = batch.execute();
Long counter = (Long) res.getResponses().get(3);
future.get().equals(counter);
redisson.shutdown();
}
}
参考代码来源 https://github.com/redisson/redisson-examples/blob/master/objects-examples/src/main/java/org/redisson/example/objects/BatchExamples.java
|