Search in sources :

Example 6 with StringUtils

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);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Arrays(java.util.Arrays) HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) Set(java.util.Set) JedisPool(redis.clients.jedis.JedisPool) IAE(org.apache.druid.java.util.common.IAE) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) Collectors(java.util.stream.Collectors) HostAndPort(redis.clients.jedis.HostAndPort) StringUtils(org.apache.commons.lang.StringUtils) JedisCluster(redis.clients.jedis.JedisCluster) JedisPool(redis.clients.jedis.JedisPool) IAE(org.apache.druid.java.util.common.IAE) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig)

Aggregations

StringUtils (org.apache.commons.lang.StringUtils)6 InputStream (java.io.InputStream)3 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 JsonPath (com.jayway.restassured.path.json.JsonPath)1 Record (com.univocity.parsers.common.record.Record)1 TsvParser (com.univocity.parsers.tsv.TsvParser)1 TsvParserSettings (com.univocity.parsers.tsv.TsvParserSettings)1 MimeTypeResolutionException (ddf.mime.MimeTypeResolutionException)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Path (java.nio.file.Path)1 FileTime (java.nio.file.attribute.FileTime)1 Date (java.util.Date)1