三、redis学习(jedis连接池)

一、jedis连接池

二、jedis连接池+config配置文件

三、jedis连接池+config配置文件+util工具类

util类

public class JedisPoolUtils {
    //工具类主要都是获取Jedis
    private 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(pro.getProperty("host"),Integer.parseInt(pro.getProperty("port")));
    }

    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}

test测试类

public class JedisUtilTest {
    @Test
    public void testJedisUtil(){
        Jedis jedis=JedisPoolUtils.getJedis();
        jedis.set("java","965");
        String java = jedis.get("java");
        System.out.println(java);
        jedis.close();
    }
}

jedis.properties

host=127.0.0.1
port=6379
maxTotal=50
maxIdle=10
原文地址:https://www.cnblogs.com/shiguanzui/p/11836567.html