Skip to content

Commit

Permalink
fix: Using custom check for redis cluster client
Browse files Browse the repository at this point in the history
  • Loading branch information
jruaux committed Jul 30, 2024
1 parent a7d56d4 commit 425d7ae
Showing 1 changed file with 23 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Cluster.Refresh;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.redis.lettucemod.RedisModulesClient;
import com.redis.lettucemod.RedisURIBuilder;
import com.redis.lettucemod.api.StatefulRedisModulesConnection;
import com.redis.lettucemod.cluster.RedisModulesClusterClient;
import com.redis.lettucemod.cluster.api.StatefulRedisModulesClusterConnection;

import io.lettuce.core.AbstractRedisClient;
import io.lettuce.core.ClientOptions;
import io.lettuce.core.RedisURI;
import io.lettuce.core.SocketOptions;
Expand Down Expand Up @@ -71,17 +71,12 @@ ClientResources clientResources() {
}

@Bean(destroyMethod = "shutdown")
@ConditionalOnProperty(name = "spring.redis.cluster.enabled", havingValue = "false", matchIfMissing = true)
RedisModulesClient client(RedisURI redisURI, RedisProperties properties, ClientResources clientResources) {
RedisModulesClient client = RedisModulesClient.create(clientResources, redisURI);
client.setOptions(clientOptions(ClientOptions.builder(), properties).build());
return client;
}

@Bean(destroyMethod = "shutdown")
@ConditionalOnProperty(name = "spring.redis.cluster.enabled", havingValue = "true", matchIfMissing = false)
RedisModulesClusterClient clusterClient(RedisURI redisURI, RedisProperties properties,
ClientResources clientResources) {
AbstractRedisClient client(RedisURI redisURI, RedisProperties properties, ClientResources clientResources) {
if (properties.getCluster() == null || CollectionUtils.isEmpty(properties.getCluster().getNodes())) {
RedisModulesClient client = RedisModulesClient.create(clientResources, redisURI);
client.setOptions(clientOptions(ClientOptions.builder(), properties).build());
return client;
}
RedisModulesClusterClient client = RedisModulesClusterClient.create(clientResources, redisURI);
ClusterClientOptions.Builder builder = ClusterClientOptions.builder();
Refresh refreshProperties = properties.getLettuce().getCluster().getRefresh();
Expand All @@ -96,19 +91,15 @@ RedisModulesClusterClient clusterClient(RedisURI redisURI, RedisProperties prope
builder.topologyRefreshOptions(refreshBuilder.build());
client.setOptions(clientOptions(builder, properties).build());
return client;

}

@Bean(name = "redisConnection", destroyMethod = "close")
@ConditionalOnBean(RedisModulesClient.class)
StatefulRedisModulesConnection<String, String> redisConnection(RedisModulesClient client) {
return client.connect();
}

@Bean(name = "redisClusterConnection", destroyMethod = "close")
@ConditionalOnBean(RedisModulesClusterClient.class)
StatefulRedisModulesClusterConnection<String, String> redisClusterConnection(RedisModulesClusterClient client) {
return client.connect();
@ConditionalOnBean(AbstractRedisClient.class)
StatefulRedisModulesConnection<String, String> redisConnection(AbstractRedisClient client) {
if (client instanceof RedisModulesClusterClient) {
return ((RedisModulesClusterClient) client).connect();
}
return ((RedisModulesClient) client).connect();
}

private <K, V, C extends StatefulRedisModulesConnection<K, V>> GenericObjectPoolConfig<C> poolConfig(
Expand All @@ -131,17 +122,16 @@ private <K, V, C extends StatefulRedisModulesConnection<K, V>> GenericObjectPool
}

@Bean(name = "redisConnectionPool", destroyMethod = "close")
@ConditionalOnBean(RedisModulesClient.class)
@ConditionalOnBean(AbstractRedisClient.class)
GenericObjectPool<StatefulRedisModulesConnection<String, String>> redisConnectionPool(RedisProperties properties,
RedisModulesClient client) {
return ConnectionPoolSupport.createGenericObjectPool(client::connect, poolConfig(properties));
}

@Bean(name = "redisClusterConnectionPool", destroyMethod = "close")
@ConditionalOnBean(RedisModulesClusterClient.class)
GenericObjectPool<StatefulRedisModulesClusterConnection<String, String>> redisClusterConnectionPool(
RedisProperties properties, RedisModulesClusterClient client) {
return ConnectionPoolSupport.createGenericObjectPool(client::connect, poolConfig(properties));
AbstractRedisClient client) {
GenericObjectPoolConfig<StatefulRedisModulesConnection<String, String>> poolConfig = poolConfig(properties);
if (client instanceof RedisModulesClusterClient) {
RedisModulesClusterClient clusterClient = (RedisModulesClusterClient) client;
return ConnectionPoolSupport.createGenericObjectPool(clusterClient::connect, poolConfig);
}
RedisModulesClient redisClient = (RedisModulesClient) client;
return ConnectionPoolSupport.createGenericObjectPool(redisClient::connect, poolConfig);
}

}

0 comments on commit 425d7ae

Please sign in to comment.