1. 程式人生 > 實用技巧 >Jedis連線池預熱&客戶端

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;
    }
}