I'm using lettuce as my connectionFactory in a spring-boot application to connect to redis. It creates a new connection every time before an operation. Checked with the MONITOR command and profiler, there's a PING command sent before every operation. Attaching the configuration I used below.
public LettuceConnectionFactory redisConnectionFactory() throws FileNotFoundException { final SocketOptions socketOptions = SocketOptions.builder() .connectTimeout(Duration.ofMillis(connectionTimeout)).keepAlive(true).build(); final SslOptions sslOptions = sslEnabled ? SslOptions.builder().jdkSslProvider() .trustManager(ResourceUtils.getFile(truststorePath)).build() : null; final ClientOptions clientOptions = sslEnabled ? ClientOptions.builder().sslOptions(sslOptions).socketOptions(socketOptions).build() : ClientOptions.builder().socketOptions(socketOptions).build(); LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder() .commandTimeout(Duration.ofMillis(timeout)).clientOptions(clientOptions).useSsl().build(); RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration(hostname, port); serverConfig.setPassword(accessKey); final LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(serverConfig, clientConfig); lettuceConnectionFactory.setValidateConnection(true); lettuceConnectionFactory.setEagerInitialization(true); lettuceConnectionFactory.afterPropertiesSet(); return lettuceConnectionFactory; } @Bean public RedisTemplate<String, Long> redisTemplate() throws FileNotFoundException { RedisTemplate<String, Long> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory()); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Long.class)); return redisTemplate; }
Is there a way to reuse the lettuce connection?