Skip to content

Instantly share code, notes, and snippets.

@ItsCosmas
Created January 30, 2024 09:33
Show Gist options
  • Save ItsCosmas/6a108dff8208aff59899fb07efb9b0f9 to your computer and use it in GitHub Desktop.
Save ItsCosmas/6a108dff8208aff59899fb07efb9b0f9 to your computer and use it in GitHub Desktop.
import io.lettuce.core.ReadFrom;
import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisClusterConnection;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
@Configuration
public class RedisConfiguration {
private final AppConfig cfg;
private final RedisProperties redisProperties;
public RedisConfiguration(AppConfig cfg, RedisProperties redisProperties) {
this.cfg = cfg;
this.redisProperties = redisProperties;
}
@Bean
protected ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
return createLettuceConnectionFactory();
}
@Bean
public ReactiveRedisClusterConnection reactiveRedisClusterConnection() {
return reactiveRedisConnectionFactory().getReactiveClusterConnection();
}
LettuceConnectionFactory createLettuceConnectionFactory() {
// Cluster configuration
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(
redisProperties.getCluster().getNodes());
redisClusterConfiguration.setMaxRedirects(cfg.getMaxRedirects());
redisClusterConfiguration.setPassword(cfg.getClusterPassword());
// Adaptive cluster topology whereby our application will keep refreshing
// the cluster to get the current status of the nodes in the cluster
ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
.enablePeriodicRefresh()
.enableAllAdaptiveRefreshTriggers()
.refreshPeriod(Duration.ofMinutes(cfg.getRefreshTime()))
.build();
ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder()
.topologyRefreshOptions(clusterTopologyRefreshOptions)
.build();
LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder()
.readFrom(ReadFrom.ANY)
.clientOptions(clusterClientOptions)
.build();
return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);
}
@Bean
public ReactiveRedisOperations<String, Object> redisOperations(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
RedisSerializer<String> keySerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer<Object> valueSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
RedisSerializationContext.RedisSerializationContextBuilder<String, Object> builder =
RedisSerializationContext.newSerializationContext(new StringRedisSerializer());
RedisSerializationContext<String, Object> context = builder
.value(valueSerializer)
.hashValue(valueSerializer)
.hashKey(valueSerializer)
.key(keySerializer)
.build();
return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment