文章目录
- 1. Jedis概述(java客户端操作Redis的工具类)
- 1.1 测试Jedis连接Redis操作
- 2. Jedis连接池原理(空间换时间,就相当于一个集合)
- 连接池的优势:
- 2.1 Jedis连接池实现(硬编码)
- 2.2 Jedis连接池优化(写成工具类调用)
1. Jedis概述(java客户端操作Redis的工具类)
Jedis是Redis官方推荐使用的Java连接开发工具。除了使用命令方式,现在基本上主流的语言都有客户端支持。Java的客户端,主要使用的就是Jedis。Jedis提供了完整Redis命令,而Redisson有更多分布式的容器实现。
java操作Redis的工具类:Jedis
Jedis连接池使用
jedis连接池是基于apache-commons pool2实现的。在构建连接池对象的时候,需要提供池对象的配置对象,及JedisPoolConfig(继承自GenericObjectPoolConfig)。我们可以通过这个配置对象对连接池进行相关参数的配置(如最大连接数,最大空数等)。
j两个jar包
1.1 测试Jedis连接Redis操作
public class TestJedis {
@Test
public void test01(){
//1:创建一个Jedis对象 创建一个连接,参1 ip地址(Redis服务器的ip地址) 参2 端口
Jedis jedis = new Jedis("localhost", 6379);
//2:执行Redis的指令(set指令)
//写入数据
jedis.set("name","strive_gf@163.com");//添加 string-string
jedis.sadd("set","hello","strive_gf@163.com","Welcome to use");//添加string-set集合类型
//读取数据
System.out.println(jedis.get("name"));
System.out.println(jedis.smembers("set"));
//关闭连接
jedis.close();
}
}
运行结果:
strive_gf@163.com
[hello, Welcome to use, strive_gf@163.com]
2. Jedis连接池原理(空间换时间,就相当于一个集合)
连接池的优势:
因为频繁创建与销毁连接非常的消耗性能(TCP/IP的三次握手四次挥手非常的消耗性能),连接池就是为了解决资源的频繁分配、释放所造成消耗性能问题。
方法:创建集合初始化多个Connection,需要时调getConnection()方法申请连接,使用完后调用close()放回连接池中。(这里的close()是放回连接池,不是关闭连接)
2.1 Jedis连接池实现(硬编码)
硬编码
配置参数写在java代码,编译成class,将来必须修改java代码才能改配置
public class TestJedisPool {
@Test
public void test01(){
// 1:创建连接池的配置对象
JedisPoolConfig config = new JedisPoolConfig();
//设置最大链接数
config.setMaxTotal(20);
//设置空闲连接数
config.setMaxIdle(10);
// 2:创建连接池 参1:Redis服务器ip地址 参2:端口号
JedisPool pool = new JedisPool(config, "localhost", 6379);
// 3:从连接池中获取一个连接
Jedis jedis = pool.getResource();//获取一个连接
// 4:执行Redis执行 Map<String,String> set get
jedis.set("name","JedisPool连接池");
String value = jedis.get("name");
System.out.println(value);
// 5:释放连接
jedis.close();
}
}
运行结果:
JedisPool连接池
2.2 Jedis连接池优化(写成工具类调用)
项目中,使用配置文件来配置参数,以后不需要修改java代码,直接改文件中的参数。
- 编写测试类(业务逻辑)
//Jedis连接池优化(JedisPool优化)
public class TestJedisUtils {
@Test
public void test01(){
//1:从连接池获取连接
Jedis jedis = JedisUtils.getRedis();
//2: 进行读写操作
jedis.set("name","JedisPool连接池优化JedisUtils");
String value = jedis.get("name");
System.out.println(value);
//3: 关闭连接
JedisUtils.close(jedis);
}
}
- 创建properties文件存放参数变量(jedis.properties)
//properties文件里面存的是键值对形式的数据,可以使用专门的读取工具Properties类来读取
maxTotal=50
maxIdle=15
url=localhost
port=6379
- 创建jedis连接池的工具类(读取.properties中文件)
读.properties文件的方式:
- 可以先使用类加载器去加载成流,再使用Properties类来读。
- 也可以使用ResourceBundle读.propties
3.1 使用类加载器去加载成流,再使用Properties类来读
public class JedisUtils {
//单例模式 静态代码块只执行一次,因为创建会很消耗性能
private static JedisPool pool;
//静态代码在项目中,如果被使用只会加载一次
static {
//读src下的文件用类加载器的方式 - 类加载器去加载成流,再使用Properties类来读
InputStream inputStream= JedisUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
//.properties文件专门的读取工具
Properties properties = new Properties();
try {
//将流中的数据读成map
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
// 1:创建连接池的配置对象
JedisPoolConfig config = new JedisPoolConfig();
//设置最大链接数
config.setMaxTotal(Integer.parseInt(properties.getProperty("maxTotal")));
//设置空闲连接数 "3"
config.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle")));
//2:创建连接池
pool = new JedisPool(config, properties.getProperty("url"), Integer.parseInt(properties.getProperty("port")));
}
public static Jedis getRedis() {
// 3:从连接池中获取一个连接
Jedis jedis = pool.getResource();//获取一个连接
return jedis;
}
public static void close(Jedis jedis) {
if(jedis!=null){
jedis.close();
}
}
}
3.2 使用ResourceBundle读.propties
public class JedisUtils {
private static JedisPool pool = null;
//1:创建一个连接池
static{
//1.1 解析properties文件
ResourceBundle bundle = ResourceBundle.getBundle("jedis");
//获取参数
String maxTotal = bundle.getString("maxTotal");
String maxIdle = bundle.getString("maxIdle");
String url = bundle.getString("url");
String port = bundle.getString("port");
//1.2创建连接池
//1:创建连接池的配置对象
JedisPoolConfig config = new JedisPoolConfig();
//1.1 设置最大连接数
config.setMaxTotal(Integer.parseInt(maxTotal));
//1.2 设置空闲连接数
config.setMaxIdle(Integer.parseInt(maxIdle));
//2:创建连接池
pool = new JedisPool(config, url, Integer.parseInt(port));
}
//2:对外提供一个获取连接的方法
public static Jedis getJedis(){
return pool.getResource();
}
//3:提供释放资源的方法
public static void close(Jedis jedis){
if(jedis != null) {
jedis.close();
}
}
}
运行结果:
JedisPool连接池优化JedisUtils