gpt4 book ai didi

java - JedisConnectionException 读取超时

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

我在我的Java程序中使用jedis。它的版本是2.9.0

         <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
</dependency>

为了避免用命令keys(*)查询结果花费太多时间,我将键存储在一个集合中,当需要所有键时,使用以下方法从集合中查询它们:

public Set<String> getKeysFromDailySet(String day) {
Jedis jedis = jedisPool.getResource();
Set<String> keys = new HashSet<>();
try {
keys = jedis.smembers(day);
jedis.close();
} catch (Exception e) {
logger.error("fail get keys from daily set {}", day);
}
return keys;
}

集合中有大约800万键,它抛出以下超时异常:

  Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:202) ~[scoring-20180118.jar:na]
at redis.clients.util.RedisInputStream.readByte(RedisInputStream.java:40) ~[scoring-20180118.jar:na]
at redis.clients.jedis.Protocol.process(Protocol.java:151) ~[scoring-20180118.jar:na]
at redis.clients.jedis.Protocol.read(Protocol.java:215) ~[scoring-20180118.jar:na]
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340) ~[scoring-20180118.jar:na]
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239) ~[scoring-20180118.jar:na]
at redis.clients.jedis.BinaryJedis.select(BinaryJedis.java:523) ~[scoring-20180118.jar:na]
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:111) ~[scoring-20180118.jar:na]
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868) ~[scoring-20180118.jar:na]
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435) ~[scoring-20180118.jar:na]
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363) ~[scoring-20180118.jar:na]
at redis.clients.util.Pool.getResource(Pool.java:49) ~[scoring-20180118.jar:na]
... 9 common frames omitted
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.7.0_80]
at java.net.SocketInputStream.read(SocketInputStream.java:152) ~[na:1.7.0_80]
at java.net.SocketInputStream.read(SocketInputStream.java:122) ~[na:1.7.0_80]
at java.net.SocketInputStream.read(SocketInputStream.java:108) ~[na:1.7.0_80]
at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:196) ~[scoring-20180118.jar:na]

这是我的 JedisPool 配置:

private final int HOUR_IN_SECONDS = 3600 * 10;  // 10 HOURS
private final Configuration config;
private final int INDEX;

public DatabaseConnection(String host, Configuration config) {
this.config = config;
this.INDEX = config.getRedisDbIndex();
jedisPool = new JedisPool(buildJedisPoolConfiguration(), host, 6379, HOUR_IN_SECONDS, null, INDEX);
}

private JedisPoolConfig buildJedisPoolConfiguration() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(this.config.getNumberOfThreads());
return jedisPoolConfig;
}

我已将超时设置为 10 小时,但错误发生在 1 小时后。我已经从 github 上阅读了 Jedis,但没有找到适合我的案例的示例。谁能帮忙?谢谢!

最佳答案

JedisPool 以毫秒为单位超时。所以您给定的超时实际上是 36 秒,而不是 10 小时。

进一步说明,与其在 800 万成员的集合上运行 SMEMBERS,不如考虑使用批量较小的 SSCAN

关于java - JedisConnectionException 读取超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48555300/

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