Jedis连接池预热&客户端

连接配置

@Component
public class RedisConstants {

    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private Integer port;
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.timeout}")
    private Integer timeout;

}

连接池配置

@Component
public class PoolConstants {

    @Value("${spring.redis.jedis.pool.max-total}")
    private Integer maxTotal;

    @Value("${spring.redis.jedis.pool.max-total}")
    private Integer maxIdle;

    @Value("${spring.redis.jedis.pool.max-total}")
    private Integer minIdle;

    @Value("${spring.redis.jedis.pool.max-total}")
    private Integer maxWait;
}

连接池预热

@Component
public class PoolConfig {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    RedisConstants redisConstants;

    @Autowired
    PoolConstants poolConstants;


    private static JedisPool pool = null;

    public static Jedis getJedis() {
        Jedis jedis = pool.getResource();
        return jedis;
    }


    public JedisPool jedisPool() {
        if (pool == null) {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(poolConstants.getMaxTotal());
            jedisPoolConfig.setMaxIdle(poolConstants.getMaxIdle());
            jedisPoolConfig.setMaxWaitMillis(1000 * 5);
            pool = new JedisPool(jedisPoolConfig, redisConstants.getHost(), redisConstants.getPort(), redisConstants.getTimeout(), redisConstants.getPassword());
        }
        return pool;
    }

    /**
     * 使用最小空闲连接数预热连接池
     */
    @PostConstruct
    public void initPool(){
        JedisPool pools = this.jedisPool();
        List<Jedis> jedisList = new ArrayList<>(poolConstants.getMinIdle());
        //建立连接
        for(int i = 0;i < poolConstants.getMinIdle();i++) {
            Jedis jedis;
            try {
                jedis = pools.getResource();
                jedisList.add(jedis);
                jedis.ping();
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
        //归还连接
        for (int i = 0; i < poolConstants.getMinIdle(); i++) {
            Jedis jedis;
            try {
                jedis = jedisList.get(i);
                jedis.close();
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
        log.info("Jedis连接池预热完成!!!");
    }
}

  

客户端工具类

public class JedisClients {

    private final static Integer TTL_TIME = 60;

    public static String set(String key, String value){
        Jedis jedis = PoolConfig.getJedis();
        String result = jedis.set(key,value);
        jedis.expire(key,TTL_TIME);
        jedis.close();
        return result;
    }

    public static String get(String key) {
        Jedis jedis = PoolConfig.getJedis();
        String result = jedis.get(key);
        jedis.close();
        return result;
    }
}

  

posted @ 2020-11-17 15:56  你要昂首阔步的走啊  阅读(442)  评论(0)    收藏  举报