gpt4 book ai didi

java - Jedis 管道异常

转载 作者:可可西里 更新时间:2023-11-01 11:13:28 29 4
gpt4 key购买 nike

当我像下面的代码一样使用 jedis 时:

public class JedisTest extends Sync {
private static final String _SET_KEY_1 = "test1";
private static final String _SET_KEY_2 = "test2";

public void process() throws SQLException {
Set<String> appSet = getAllUserableAppkey();
final ShardedJedis jedis = RedisHelper.getJedis();
final ShardedJedisPipeline pipeline = jedis.pipelined();

for (String key : appSet) {
Set<String> result = jedis.smembers(_SET_KEY_1);
Set<String> result2 = jedis.smembers(_SET_KEY_2);
String rangName = String.format("%s::%s", "test", key);
for (int i = 0; i < 10; i++) {
pipeline.sadd(rangName, String.valueOf(i));
}
}
pipeline.sync();
}

public Set<String> getAllUserableAppkey() {

}

public static void main(String[] args) throws Exception {
DbHelper.init();
RedisHelper.init();
JedisTest jedisTest = new JedisTest();
try {
jedisTest.process();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

它像这样抛出异常:

Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.util.List
at redis.clients.jedis.Connection.getBinaryMultiBulkReply(Connection.java:224)
at redis.clients.jedis.Connection.getMultiBulkReply(Connection.java:217)
at redis.clients.jedis.Jedis.smembers(Jedis.java:1055)
at redis.clients.jedis.ShardedJedis.smembers(ShardedJedis.java:339)
at com.snda.sync.impl.test.JedisTest.process(JedisTest.java:29)
at com.snda.sync.impl.test.JedisTest.main(JedisTest.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

我可以修改代码来更正:

public void process() throws SQLException {
Set<String> appSet = getAllUserableAppkey();
final ShardedJedis jedis = RedisHelper.getJedis();
for (String key : appSet) {
final ShardedJedisPipeline pipeline = jedis.pipelined();
Set<String> result = jedis.smembers(_SET_KEY_1);
Set<String> result2 = jedis.smembers(_SET_KEY_2);
//log.warn("result1 :{},result2:{}",result,result2);
String rangName = String.format("%s::%s", "test", key);
for (int i = 0; i < 10; i++) {
pipeline.sadd(rangName, String.valueOf(i));
}
pipeline.sync();
}
}

但我不知道为什么会抛出异常,pipline.sadd() 是否与 jedis.smembers 冲突?谢谢你回答我!

绝地武士最新:2.7.2

最佳答案

你不应该在流水线时直接使用 Jedis 实例。

管道使用 Jedis 实例的流(不初始化新流),并且由于正常操作立即读取响应而管道稍后读取整个响应,混淆两种用法会使 Jedis 陷入困境。

P -- 流水线式/N -- 普通

请求 --> P(1) P(2) N(3) N(4) P(5)

Redis 响应 --> P(1) P(2) N(3) N(4) P(5)

匹配的请求-响应 --> N(1: 应该是 3) N(2: 应该是 4) P(3: 应该是 1) P(4: 应该是 2) P(5)

您可以看到响应很容易有缺陷。

关于java - Jedis 管道异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31182303/

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