use of org.apache.commons.lang.StringUtils in project druid by druid-io.
the class RedisCacheFactory method create.
public static Cache create(final RedisCacheConfig config) {
if (config.getCluster() != null && StringUtils.isNotBlank(config.getCluster().getNodes())) {
Set<HostAndPort> nodes = Arrays.stream(config.getCluster().getNodes().split(",")).map(String::trim).filter(StringUtils::isNotBlank).map(hostAndPort -> {
int index = hostAndPort.indexOf(':');
if (index <= 0 || index == hostAndPort.length()) {
throw new IAE("Invalid redis cluster configuration: %s", hostAndPort);
}
int port;
try {
port = Integer.parseInt(hostAndPort.substring(index + 1));
} catch (NumberFormatException e) {
throw new IAE("Invalid port in %s", hostAndPort);
}
if (port <= 0 || port > 65535) {
throw new IAE("Invalid port in %s", hostAndPort);
}
return new HostAndPort(hostAndPort.substring(0, index), port);
}).collect(Collectors.toSet());
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(config.getMaxTotalConnections());
poolConfig.setMaxIdle(config.getMaxIdleConnections());
poolConfig.setMinIdle(config.getMinIdleConnections());
JedisCluster cluster;
if (config.getPassword() != null) {
cluster = new JedisCluster(nodes, // connection timeout
config.getTimeout().getMillisecondsAsInt(), // read timeout
config.getTimeout().getMillisecondsAsInt(), config.getCluster().getMaxRedirection(), config.getPassword().getPassword(), poolConfig);
} else {
cluster = new JedisCluster(nodes, // connection timeout and read timeout
config.getTimeout().getMillisecondsAsInt(), config.getCluster().getMaxRedirection(), poolConfig);
}
return new RedisClusterCache(cluster, config);
} else {
if (StringUtils.isBlank(config.getHost())) {
throw new IAE("Invalid redis configuration. no redis server or cluster configured.");
}
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(config.getMaxTotalConnections());
poolConfig.setMaxIdle(config.getMaxIdleConnections());
poolConfig.setMinIdle(config.getMinIdleConnections());
return new RedisStandaloneCache(new JedisPool(poolConfig, config.getHost(), config.getPort(), // connection timeout and read timeout
config.getTimeout().getMillisecondsAsInt(), config.getPassword() == null ? null : config.getPassword().getPassword(), config.getDatabase(), null), config);
}
}
Aggregations