java调用redis的多种方式与心得
心得:
/**
* 心得:
* 1.连接方式主要有:直连同步,直连事务,直连管道,直连管道事务,分布式直连同步,分布式直连管道,
* 分布式连接池同步,分布式连接池管道;普通连接池同步,普通连接池管道;
* 2.同步方式会返回数据库执行结果,管道则不会返回数据库执行结果;
* 3。管道分两种执行方式:有返回命令执行结果,无返回命令执行结果;
* 4.返回数据库执行结果 与 返回命令执行结果 不是一个东西;
* 5一般管道的无返回命令执行结果 的执行方式会比 有返回结果的方式快一点点,,但是在分布式连接池的测试里则得出相反的结果,
* 因此,这两种管道方式的速度差距不大,按使用需求即可。
*/
测试源码
* redis几种调用方法
*/
/**
* 普通直连同步写入操作,于myRedis方法一样,都是单实例方法
* * 同步执行,会返回执行结果
* 写入10万行字符串
*/
@org.junit.Test
public void r1() {
Jedis jedis = new Jedis("127.0.0.1", 6379);
//密码,如果服务器没有密码,则会报错,因此,要对用使用
//jedis.auth("admin"); long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
//返回的是个字符串
String res = jedis.set("n" + i, "n" + i);
System.out.println("返回的结果:" + res);
//返回的结果:OK
}
long end = System.currentTimeMillis();
System.out.println("普通同步写入:" + ((end - start) / 1000.0) + "秒");
jedis.close();
}
//结果:
//普通同步写入:5.792秒
/**
* 普通批量事物提交,, REDIS事物不支持回滚
* 同步执行,会返回执行结果
* 写入10万行字符串
*/
1 @org.junit.Test
2 public void r2() {
3 Jedis jedis = null;
4 try {
5 jedis = new Jedis("127.0.0.1", 6379);
6 long start = System.currentTimeMillis();
7 //事务类型的参数 , REDIS事物不支持回滚
8 Transaction transaction = jedis.multi();
9 for (int i = 0; i < 100000; i++) {
10 //没有返回值
11 transaction.set("n" + i, "n" + i);
12 }
13 //执行事务,返回执行的命令结果
14 List<Object> res = transaction.exec();
15 // System.out.println("操作成功条数:" + res.size());
16 long end = System.currentTimeMillis();
17 System.out.println("普通批量事物写入:" + ((end - start) / 1000.0) + "秒");
18 //断开连接
19 jedis.disconnect();
20 } catch (Exception e) {
21 e.printStackTrace();
22 } finally {
23 if (jedis != null) {
24 System.out.println("未关闭");
25 jedis.close();
26 System.out.println("关闭成功");
27 } else {
28 System.out.println("已关闭");
29 }
30 }
31 // 操作成功条数:100000
32 // 普通批量事物写入:0.443秒
33 // 未关闭
34 // 关闭成功
35 }
/**
* 普通异步管道提交,不需要等待执行完成后的结果
*/
1 @org.junit.Test
2 public void r3() {
3 Jedis jedis = null;
4 try {
5 jedis = new Jedis("127.0.0.1", 6379);
6 long start = System.currentTimeMillis();
7 Pipeline pipeline = jedis.pipelined();
8 for (int i = 0; i < 100000; i++) {
9 //没有返回值
10 pipeline.set("n" + i, "n" + i);
11 }
12 // //跟批量事务提交速度一样,syncAndReturnAll()会返回结果,花费0.406秒
13 // List<Object> res = pipeline.syncAndReturnAll();
14 // System.out.println("返回的结果条数:"+res.size());
15 //无结果返回,速度更快一点点,0.334秒左右即可
16 pipeline.sync();
17 long end = System.currentTimeMillis();
18 System.out.println("普通异步管道提交:" + ((end - start) / 1000.0) + "秒");
19 //断开连接
20 jedis.disconnect();
21 } catch (Exception e) {
22 e.printStackTrace();
23 } finally {
24 if (jedis != null) {
25 System.out.println("未关闭");
26 jedis.close();
27 System.out.println("关闭成功");
28 } else {
29 System.out.println("已关闭");
30 }
31 }
32 }
/**
* 在异步管道中使用事务
* 效率和单独使用事务差不多
*/
@org.junit.Test
public void r4() {
Jedis jedis = null;
try {
jedis = new Jedis("127.0.0.1", 6379);
long start = System.currentTimeMillis();
Pipeline pipeline = jedis.pipelined();
//管道开启事务
pipeline.multi();
for (int i = 0; i < 100000; i++) {
//没有返回值
pipeline.set("n" + i, "n" + i);
}
//执行事务
pipeline.exec();
// //执行管道,返回执行的命令结果,,花费时间0.413秒
// List<Object> res = pipeline.syncAndReturnAll();
//// for (Object ob:res){
//// System.out.println(ob);
//// }
//// System.out.println("返回的结果条数:" + res.size());
//无返回值,花费的时间0.366秒
pipeline.sync();
long end = System.currentTimeMillis();
System.out.println("普通异步管道提交:" + ((end - start) / 1000.0) + "秒");
//断开连接
jedis.disconnect();
// 普通异步管道提交:0.334秒
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
System.out.println("未关闭");
jedis.close();
System.out.println("关闭成功");
} else {
System.out.println("已关闭");
}
}
}
/**
* 分布式直连,普通同步操作
* 与普通的单例连接速度一样
*/
1 @org.junit.Test
2 public void r5() {
3 long start = System.currentTimeMillis();
4 //生产环境下,这里一般换成不同的ip,也就是说,使用从机
5 List<JedisShardInfo> shardInfos = Arrays.asList(
6 new JedisShardInfo("127.0.0.1", 6379),
7 new JedisShardInfo("127.0.0.1", 6379));
8 ShardedJedis shardedJedis = new ShardedJedis(shardInfos);
9 for (int i = 0; i < 100000; i++) {
10 //返回的是个字符串
11 String res = shardedJedis.set("n" + i, "n" + i);
12 // System.out.println("返回的结果:" + res);
13 //返回的结果:OK
14 }
15 long end = System.currentTimeMillis();
16 System.out.println("分布式直连:" + ((end - start) / 1000.0) + "秒");
17 //断开连接
18 shardedJedis.disconnect();
19 //关闭连接
20 shardedJedis.close();
21 // 分布式直连:5.254秒
22 }
/**
* 分布式直连,使用管道
* <p>
* 十万条数据花费0.457秒
*/
1 @org.junit.Test
2 public void r6() {
3 long start = System.currentTimeMillis();
4 //生产环境下,这里一般换成不同的ip,也就是说,使用从机
5 List<JedisShardInfo> shardInfos = Arrays.asList(
6 new JedisShardInfo("127.0.0.1", 6379),
7 new JedisShardInfo("127.0.0.1", 6379));
8 ShardedJedis shardedJedis = new ShardedJedis(shardInfos);
9 //开启异步管道
10 ShardedJedisPipeline shardedJedisPipeline = shardedJedis.pipelined();
11 for (int i = 0; i < 100000; i++) {
12 shardedJedisPipeline.set("n" + i, "n" + i);
13 }
14 //有返回结果的执行方式,其实是个以队列的形式发送命令,然后返回执行命令结果
15 List<Object> list = shardedJedisPipeline.syncAndReturnAll();
16 long end = System.currentTimeMillis();
17 System.out.println("分布式管道:" + ((end - start) / 1000.0) + "秒");
18 shardedJedis.disconnect();
19 shardedJedis.close();
20 // 分布式管道:0.457秒
21 }
/**
* 分布式连接池,适合多线程
* <p>
* 同步调用
*/
1 @org.junit.Test
2 public void r7() {
3 long start = System.currentTimeMillis();
4 //生产环境下,这里一般换成不同的ip,也就是说,使用从机
5 List<JedisShardInfo> shardInfos = Arrays.asList(
6 new JedisShardInfo("127.0.0.1", 6379),
7 new JedisShardInfo("127.0.0.1", 6379));
8 //new JedisPoolConfig() 表示默认设置,可以自定义设置属性参数,这里不展示
9 ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shardInfos);
10 ShardedJedis shardedJedis = pool.getResource();
11 for (int i = 0; i < 100000; i++) {
12 //返回的是个字符串
13 String res = shardedJedis.set("n" + i, "n" + i);
14 // System.out.println("返回的结果:" + res);
15 //返回的结果:OK
16 }
17 pool.returnResource(shardedJedis);
18 long end = System.currentTimeMillis();
19 System.out.println("分布式连接池,同步调用:" + ((end - start) / 1000.0) + "秒");
20 //销毁连接池
21 pool.destroy();
22 //断开连接,这个可写可不写
23 shardedJedis.disconnect();
24 //关闭连接
25 //不可以在这里使用shardedJedis.close();
26 //否则会报错redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool
27 ////
28 //分布式连接池,同步调用:5.419秒
29 }
/**
* 分布式连接池,异步管道调用
*/
1 @org.junit.Test
2 public void r8(){
3 long start = System.currentTimeMillis();
4 //生产环境下,这里一般换成不同的ip,也就是说,使用从机
5 List<JedisShardInfo> shardInfos = Arrays.asList(
6 new JedisShardInfo("127.0.0.1", 6379),
7 new JedisShardInfo("127.0.0.1", 6379));
8 //new JedisPoolConfig() 表示默认设置,可以自定义设置属性参数,这里不展示
9 ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shardInfos);
10 ShardedJedis shardedJedis = pool.getResource();
11 //开启管道
12 ShardedJedisPipeline pipeline = shardedJedis.pipelined();
13 for (int i = 0; i < 100000; i++) {
14 pipeline.set("n" + i, "n" + i);
15 }
16 // //有返回结果的执行方式,其实是个以队列的形式发送命令,然后返回执行命令结果
17 // List<Object> list = pipeline.syncAndReturnAll();
18 //无结果返回
19 pipeline.sync();
20 pool.returnResource(shardedJedis);
21 long end = System.currentTimeMillis();
22 System.out.println("分布式连接池,异步管道调用:" + ((end - start) / 1000.0) + "秒");
23 //销毁连接池
24 pool.destroy();
25 //断开连接,这个可写可不写
26 shardedJedis.disconnect();
27 //关闭连接
28 //不可以在这里使用shardedJedis.close();
29 //否则会报错redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool
30 ////
31 //有返回结果的执行方式
32 //分布式连接池,异步管道调用:0.49秒
33 //
34 //无结果返回执行方式
35 //分布式连接池,异步管道调用:0.517秒
36
37 }
/**
* 普通连接池同步
*/
@org.junit.Test
public void r9() {
long start = System.currentTimeMillis();
JedisPoolConfig config = new JedisPoolConfig();
// //最大连接数
// config.setMaxTotal(30);
// //最大连接空闲数
// config.setMaxIdle(2);
JedisPool jedisPool = new JedisPool(config, "127.0.0.1", 6379);
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
for (int i = 0; i < 100000; i++) {
jedis.set("n" + i, "n" + i);
}
//如果加入这一句,则不能使用jedis.close();
// jedisPool.returnResource(jedis);
long end = System.currentTimeMillis();
System.out.println("普通连接池同步:" + ((end - start) / 1000.0) + "秒");
//销毁连接池
jedisPool.destroy();
//断开连接,这个可写可不写
jedis.disconnect();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
}
} }
// 普通连接池同步:5.166秒
/**
* 普通连接池管道
*/
1 @org.junit.Test
2 public void r10() {
3 long start = System.currentTimeMillis();
4 JedisPoolConfig config = new JedisPoolConfig();
5 // //最大连接数
6 // config.setMaxTotal(30);
7 // //最大连接空闲数
8 // config.setMaxIdle(2);
9 JedisPool jedisPool = new JedisPool(config, "127.0.0.1", 6379);
10 Jedis jedis = null;
11 try {
12 jedis = jedisPool.getResource();
13 Pipeline pipeline = jedis.pipelined();
14 for (int i = 0; i < 100000; i++) {
15 pipeline.set("n" + i, "n" + i);
16 }
17 pipeline.syncAndReturnAll();
18 //如果加入这一句,则不能使用jedis.close();
19 // jedisPool.returnResource(jedis);
20 long end = System.currentTimeMillis();
21 System.out.println("普通连接池管道:" + ((end - start) / 1000.0) + "秒");
22 //销毁连接池
23 jedisPool.destroy();
24 //断开连接,这个可写可不写
25 jedis.disconnect();
26 } catch (Exception e) {
27 e.printStackTrace();
28 } finally {
29 if (jedis != null) {
30 jedis.close();
31 }
32 }
33 // 普通连接池管道:0.457秒
34 }
java调用redis的多种方式与心得的更多相关文章
- Java调用DLL有多种方式,常用的方式有JNative、JNA、JNI等。
JNative方式调用dll JNative是一种能够使Java语言使调用DLL的一种技术,对JNI进行了封装,可能有些读者会有这样一个问题,JNative对JNI进行了封装,并且是一种跨语言的使用D ...
- java获得路径的多种方式
本文讲解java语言中获得运行时路径的多种方式,包括java项目.java web项目.jar.weblogic等多种场景. 一.this.getClass().getClassLoader().ge ...
- linux系统下安装redis以及java调用redis
关系型数据库:MySQL Oracle 非关系型数据库:Redis 去掉主外键等关系数据库的关系性特性 1)安装redis编译的c环境,yum install gcc-c++ 2)将redis-2. ...
- java创建线程的多种方式
java创建线程的四种方式 1.继承 Thread 类 通过继承 Thread 类,并重写它的 run 方法,我们就可以创建一个线程. 首先定义一个类来继承 Thread 类,重写 run 方法. 然 ...
- Java调用Redis集群
前文 需要使用以下jar包 Maven项目引用以下配置: <dependency> <groupId>org.apache.commons</groupId> &l ...
- (转)java调用python脚本
这篇博客旨在吐血分享今天遇到的java调用python脚本遇到的坑,折腾了3个多小时终于可以跑通了,代码超级短,但网上的好多资料都是抄来抄去的,很少有能够直接跑通的,尤其是针对你的python文件中用 ...
- java 获取classpath下文件多种方式
java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...
- [OpenSource]浅谈.Net和Java互相调用的三种方式
在很多的大型系统开发中,开发工具往往不限制于同一种开发语言,而是会使用多种开发语言的混合型开发.目前Java和.Net都声称自己占85%的市场份额,不管谁对谁错,Java和.Net是目前应用开发的两个 ...
- Redis(Windows安装方法与Java调用实例 & 配置文件参数说明 & Java使用Redis所用Jar包 & Redis与Memcached区别 & redis-cli.exe命令及示例)
Windows下Redis的安装使用 0.前言 因为是初次使用,所以是在windows下进行安装和使用,参考了几篇博客,下面整理一下 1.安装Redis 官方网站:http://redis.io/ 官 ...
随机推荐
- shell脚本 系统状态信息查看
一.简介 源码地址 日期:2018/6/23 介绍:显示简单的系统信息 效果图: 二.使用 适用:centos6+,ubuntu12+ 语言:中文 注意:无 下载 wget https://raw.g ...
- 转:Intent 操作常用URI代码示例
以下是常用到的Intent的URI及其示例,包含了大部分应用中用到的共用Intent 一.打开一个网页,类别是Intent.ACTION_VIEW 1 2 Uri uri = Uri.parse(&q ...
- Java值引用和对象引用区别Demo
转自:http://blog.csdn.net/gundsoul/article/details/4927404 以前就知道JAVA对象分对象引用和值引用,并且还知道8种基础数据类型,即引用时是值引用 ...
- ciscn_2019_es_1
拿到题目例行检查 将题目放到idax64中进行代码审计 主界面,我也没看懂什么意思 call 可以看到free的指针没有置零,存在uaf漏洞 add函数 show函数 该题的libc版本是2.27,所 ...
- js(jQuery)获取自定义data属性的值
有时候因为需要在标签上设置自定义data属性值, <div class="col-sm-6 col-md-4" id="get_id" data-c_id ...
- VS2017激活key
密钥 KBJFW-NXHK6-W4WJM-CRMQB-G3CDH
- 【LeetCode】1111. Maximum Nesting Depth of Two Valid Parentheses Strings 有效括号的嵌套深度
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目讲解 划分规则讲解 返回结果讲解 解题方法 代码 日期 题目地址:ht ...
- 【LeetCode】723. Candy Crush 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...
- 【LeetCode】322. Coin Change 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】728. Self Dividing Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环 filter函数 数字迭代 日期 题目地址:h ...