Jedis连接池和Jedis连接池工具类
Jedis连接池对象:
@Test
public void MyTest06() {
//创建配置对象
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(50);
jedisPoolConfig.setMaxIdle(10);
//创建Jedis连接池对象
JedisPool jedisPool = new JedisPool(jedisPoolConfig,"localhost",6379);
//获取连接
Jedis resource = jedisPool.getResource();
// resource.set("aa", "bb");
// System.out.println(resource.get("aa"));
String s = resource.flushDB();
System.out.println(s);
resource.close();
}
连接池工具类:
package com.ailyt.jiedis.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 {
private static JedisPool jedisPool;
static {
ClassLoader classLoader = JedisPoolUtils.class.getClassLoader();
InputStream resourceAsStream = classLoader.getResourceAsStream("jedis.properties");
Properties prop = new Properties();
try {
prop.load(resourceAsStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(Integer.parseInt(prop.getProperty("maxTotal")));
jedisPoolConfig.setMaxIdle(Integer.parseInt(prop.getProperty("maxIdle")));
jedisPool = new JedisPool(prop.getProperty("host"), Integer.parseInt(prop.getProperty("port")));
}
/**
* 获取连接方法
*/
public static Jedis getJedis() {
return jedisPool.getResource();
}
}
host=127.0.0.1
port=6379
maxTotal=50
maxIdle=10