javaspring-webfluxspring-data-redis-reactive

Is there a way to set a timeout in Java with Redis reactive?


I'm using ReactiveRedisConnection to configure a connection to a local redis container.

But in the future the application will be hosted on a webserver and the redis on a different server.

Is there any option to set a timeout for a request?


Solution

  • After some research and tests, I found that the timeout must be set on the request query instead.

    So on the config Class:

    @Bean
    public ReactiveRedisTemplate<String, String> reactiveRedisTemplateString
    (ReactiveRedisConnectionFactory connectionFactory) {
        return new ReactiveRedisTemplate<>
                  (connectionFactory, RedisSerializationContext.string());
    }
    

    and in the service:

    @Autowired
    private ReactiveRedisTemplate<String, Response> repository;
    
    public Mono<String> execute(String value){
            return repository.opsForHash().entries("KEY_TO_SEARCH")
                    .timeout(Duration.ofMillis(TIMEOUT))
                    .collect(Collectors.toMap("CODE_HERE");
    

    Edit: Thank for everyone who helped here.