Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to configure timeout of JedisConnectionFactory base on spring boot framework

I'm using springboot, I'm confused to configure the timeout to connect redis.

Currently, my configurations are:

application.yml:

spring.redis.host: myhost
spring.redis.port: 6379
spring.redis.pool.max-idle: 8
spring.redis.pool.min-idle: 0
spring.redis.pool.max-active: 8
spring.redis.pool.max-wait: -1

StringRedisDao.java:

@Autowired
public StringRedisDao(final StringRedisTemplate template, final ObjectMapper mapper) {
    if (template.getConnectionFactory() instanceof JedisConnectionFactory) {
        ((JedisConnectionFactory) template.getConnectionFactory()).getShardInfo().setTimeout(5000);
        ((JedisConnectionFactory) template.getConnectionFactory()).setTimeout(5000);
    }
    this.template = template;
    this.mapper = mapper;
}

I use wireshark to capture the packets, and I found that the redis was disconnected after 2 seconds, not 5 seconds as I set in the code above.

Because of this, I cannot perform a requests that the query time of redis is more than 2 seconds.

Please, how can I do this?

like image 526
terry Avatar asked May 25 '15 13:05

terry


People also ask

How do I set timeout in Redis?

To create a Redis with an expiration time, use the SET command and the EX option to set the expiration time. The EX option takes a number in seconds and sets the number of seconds the key is valid until expiration. You can also use PX to specify the expiration time in Milliseconds.

What is Redis timeout?

Redis client uses a single TCP connection and can only read one response at a time. Even when a first operation times out, it does not stop the data being sent to/from the server. Because of this, it blocks other requests and causes timeouts.

How Redis works in spring boot?

Spring Boot will automatically configure a Redis-cache Manager but with default properties. We can modify this configuration and change it as per our requirement. Modifying the configurations gives us more control over the basic cache configuration.


1 Answers

There is also a configuration setting you can put in application.properties:

spring.redis.timeout=5000
like image 92
Bal Avatar answered Sep 20 '22 14:09

Bal