23.7 Jedis_连接池
- jedis连接池: JedisPool
- 使用:
- 1.创建JedisPool连接池对象 ;
- 2.调用方法 getResource() 方法获取Jedis连接。
- 示例:
@Test
public void test7(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(50);
config.setMaxIdle(10);
JedisPool jedisPool = new JedisPool(config,"localhost",6379);
Jedis jedis = jedisPool.getResource();
jedis.set("hehe","heihei");
jedis.close();;
}
23.8 Jedis_连接池工具类
package com.xww.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;
public class JedisPoolUtils {
private static JedisPool jedisPool;
static{
InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
Properties pro = new Properties();
try {
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
jedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port")));
}
public static Jedis getJedis(){
return jedisPool.getResource();
}
}
@Test
public void test8(){
Jedis jedis = JedisPoolUtils.getJedis();
jedis.set("hello","world");
jedis.close();;
}