redis学习笔记——JedisPoolUtil
-
jedis.properties [放在src目录下]
host=127.0.0.1 port=6379 MaxTotal=50 MaxIdle=10
-
JedisPoolUtil
package com.nick.jedis.util; 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; /** * @description: JedisPool工具类 * 加载配置文件,配置连接池的参数 * 提供获取连接的方法 * @author: Nick 2020/12/3 10:01 下午 **/ public class JedisPoolUtil { private static JedisPool jedisPool; static { //读取配置文件 InputStream is = JedisPoolUtil.class.getClassLoader().getResourceAsStream("jedis.properties"); //创建Properties对象 Properties properties = new Properties(); //关联文件 try { properties.load(is); } catch (IOException e) { e.printStackTrace(); } //获取数据,设置到JedisPoolConfig中 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(Integer.parseInt(properties.getProperty("MaxTotal"))); config.setMaxIdle(Integer.parseInt(properties.getProperty("MaxIdle"))); //初始化JedisPool jedisPool = new JedisPool(config,properties.getProperty("host"), Integer.parseInt(properties.getProperty("port"))); } /* 获取连接的方法 */ public static Jedis getJedis(){ return jedisPool.getResource(); } }
-
JedisPoolUtilTest
/** * @decription:jedis连接工具类使用 * @param: * @return * @author:WZL 2020/12/3 10:15 下午 **/ @Test public void test8(){ //通过连接池工具类 Jedis jedis = JedisPoolUtil.getJedis(); //操作使用 jedis.set("name","lisi"); String name = jedis.get("name"); System.out.println(name); //关闭 归还到连接池中 jedis.close(); }
-
result