工具类---JedisPoolUtils

JedisPoolUtils

package cn.itcast.travel.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/*
jedisPool工具类
 加载配置文件,配置的是一些是连接池的参数。

 */
public class JedisPoolUtils {
    public static JedisPool jedisPool;
    static {
        //读取配置文件
        InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
        //创建Properties 对象
        Properties pro=new Properties();
        //关联文件
        try {
            pro.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取数据,并且设置到jedisPoolConfig中
        JedisPoolConfig config =new JedisPoolConfig();
        config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
        config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
        //初始化JedisPool
        jedisPool=new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port")));
    }
    //获取连接方法
    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}