gpt4 book ai didi

java - redis pipeline默认执行一次多少条命令?

转载 作者:IT王子 更新时间:2023-10-29 06:16:29 24 4
gpt4 key购买 nike

我正在使用 jedis 管道将一批数据插入到 redis 中。现在我面临一个困惑的问题。我想批处理特定大小,然后调用 sync(),但似乎管道将大约每 200 条记录自动调用一次同步。这是我的代码,谁能告诉我是否有关于此的任何配置?

public class RedisClusterTest {
public static void main(String args[]) throws IOException, InterruptedException {
String host = args[0];
int port = Integer.valueOf(args[1]);
int cnt = Integer.valueOf(args[2]);
Jedis jedis = new Jedis(host, port);

Pipeline pip = jedis.pipelined();
for(int i = 0 ; i < 2000; i++) {
pip.hset("Server", String.valueOf(i), String.valueOf(i));
Thread.sleep(10);
}
// When it end loop, about 1900 records has already been insert into redis, and the last sync only made last remaining data been sync.
pip.sync()

最佳答案

管道不会等待您确认将批处理发送到 Redis。文档说:

Sometimes you need to send a bunch of different commands. A very cool way to do that, and have better performance than doing it the naive way, is to use pipelining. This way you send commands without waiting for response, and you actually read the responses at the end, which is faster.

通过总结它说使用管道无需等待响应并且就像流一样发送。

我查看了他们的源代码,证实了他们的文档。

public Pipeline pipelined() {
pipeline = new Pipeline();
pipeline.setClient(client);
return pipeline;
}

这将返回您的管道实例。然后你调用一堆 HSET

public Long hset(final byte[] key, final byte[] field, final byte[] value) {
checkIsInMultiOrPipeline();
client.hset(key, field, value);
return client.getIntegerReply();
}

立即得到回复。

然后调用sync,它:

Synchronize pipeline by reading all responses. This operation close the pipeline. In order to get return values from pipelined commands, capture the different Response<?> of the commands you execute.

换句话说,它处理您的管道实例。

总结一下,如果您希望它仅在调用同步时发送批处理,则不应使用“流水线”。它不是这样工作的。

关于java - redis pipeline默认执行一次多少条命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54733695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com