8

I'm using Spring Boot and I'm confused how 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?

informatik01
  • 16,038
  • 10
  • 74
  • 104
terry
  • 341
  • 2
  • 6
  • 13

2 Answers2

6

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

spring.redis.timeout=5000
informatik01
  • 16,038
  • 10
  • 74
  • 104
Bal
  • 2,027
  • 4
  • 25
  • 51
  • Really? Have you confirmed that this actually has an effect? I cannot see any timeout property in org.springframework.boot.autoconfigure.redis.RedisProperties! – Kutzi Jan 11 '16 at 17:45
  • 2
    Here is a list of common properties: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html – Bal Jan 11 '16 at 19:06
  • ... and that property is not in the list .. maybe it was in the past – Rafael Feb 21 '23 at 16:36
  • 1
    @Rafael AFAIK there is not master list of all available properties. The list of common properties in that link is not comprehensive, however it does look like the property has changed to utilize the "spring.data.redis" prefix: https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java – Bal Feb 23 '23 at 16:11
0

It might be helpful to someone. I have used Redisson client for redis implementation. Setting timeout for redis use spring.cache.redis.time-to-live=5000

firozSujan
  • 118
  • 1
  • 9