归档
简介
实现类
常用方法
- 命令
# readAsync
# writeAsync
# async
- 脚本
# evalReadAsync
# evalWriteAsync
# evalAsync
测试
@Test
public void testLock() {
RLock lock = redisson.getLock("lock");
lock.lock(10, TimeUnit.SECONDS);
try {
System.out.println("OK!");
} finally {
lock.unlock();
}
}
@Test
public void testCompletionStage() {
CompletionStage<String> replicationFuture = CompletableFuture.completedFuture("test-001");
CompletionStage<String> resFuture = replicationFuture.thenCompose(r -> {
System.out.println("r: " + r);
return CompletableFuture.completedFuture("OK-123");
});
resFuture.thenAccept(res -> System.out.println("res: " + res));
}
说明
org.redisson.command.CommandAsyncService
public class CommandAsyncService implements CommandAsyncExecutor {
public <V, R> RFuture<R> async(boolean readOnlyMode, NodeSource source, Codec codec,
RedisCommand<V> command, Object[] params, boolean ignoreRedirect, boolean noRetry) {
...
CompletableFuture<R> mainPromise = createPromise();
RedisExecutor<V, R> executor = new RedisExecutor<>(readOnlyMode, source, codec, command, params, mainPromise,
ignoreRedirect, connectionManager, objectBuilder, referenceType, noRetry);
executor.execute();
return new CompletableFutureWrapper<>(mainPromise);
}
private <T, R> RFuture<R> evalAsync(NodeSource nodeSource, boolean readOnlyMode, Codec codec, RedisCommand<T> evalCommandType,
String script, List<Object> keys, boolean noRetry, Object... params) {
if (isEvalCacheActive() && evalCommandType.getName().equals("EVAL")) {
CompletableFuture<R> mainPromise = new CompletableFuture<>();
...
List<Object> args = new ArrayList<Object>(2 + keys.size() + params.length);
args.add(sha1);
...
RedisExecutor<T, R> executor = new RedisExecutor<>(readOnlyMode, nodeSource, codec, cmd,
args.toArray(), promise, false,
connectionManager, objectBuilder, referenceType, noRetry);
executor.execute();
promise.whenComplete((res, e) -> {
if (e != null) {
if (e.getMessage().startsWith("ERR unknown command")) {
evalShaROSupported.set(false);
RFuture<R> future = evalAsync(nodeSource, readOnlyMode, codec, evalCommandType, script, keys, noRetry, pps);
transfer(future.toCompletableFuture(), mainPromise);
} else if (e.getMessage().startsWith("NOSCRIPT")) {
...
} else {
free(pps);
mainPromise.completeExceptionally(e);
}
return;
}
free(pps);
mainPromise.complete(res);
});
return new CompletableFutureWrapper<>(mainPromise);
}
...
return async(readOnlyMode, nodeSource, codec, evalCommandType, args.toArray(), false, noRetry);
}
}
org.redisson.command.RedisExecutor
public void execute() {
...
CompletableFuture<RedisConnection> connectionFuture = getConnection();
...
connectionFuture.whenComplete((connection, e) -> {
...
sendCommand(attemptPromise, connection);
...
});
...
}
protected void sendCommand(CompletableFuture<R> attemptPromise, RedisConnection connection) {
if (source.getRedirect() == Redirect.ASK) {
...
} else {
...
writeFuture = connection.send(new CommandData<>(attemptPromise, codec, command, params));
...
}
}
org.redisson.client.RedisConnection
public <T, R> ChannelFuture send(CommandData<T, R> data) {
return channel.writeAndFlush(data);
}