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/ 官 ...
随机推荐
- Jedis操作五种不同的类型的数据
package cn.hope.jedis.utils;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;im ...
- 『学了就忘』Linux服务管理 — 75、Linux系统中的服务
目录 1.服务的介绍 2.Windows系统中的服务 3.Linux系统中服务的分类 4.独立的服务和基于xinetd服务的区别 5.如何查看一个服务是独立的服务还是基于xinetd的服务 (1)查看 ...
- Java中Date类型与String 类型之间的互相转换
Java中String类型和Date类型之间的转换 我们在注册网站的时候,往往需要填写个人信息,如姓名,年龄,出生日期等,在页面上的出生日期的值传递到后台的时候是一个字符串,而我们存入数据库的时候确需 ...
- 编译工具sbt部署
目录 一.简介 二.部署 三.测试 一.简介 项目构建工具是项目开发中非常重要的一个部分,充分利用好它能够极大的提高项目开发的效率.在学习SCALA的过程中,我遇到了SBT(Simple Build ...
- Java动态脚本Groovy读取配置文件
前言:请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i 核心涉及: @Value:作用是通过注解将常量.配置文件中的值.其他bean的属性值注入到变量中,作为变量的初始值. @Configur ...
- 从 CPython 源码角度看 Python 垃圾回收机制
环状双向链表 refchain 在 Python 程序中创建的任何对象都会被放到 refchain 链表中,当创建一个 Python 对象时,内部实际上创建了一些基本的数据: 上一个对象 下一个对象 ...
- [BUUCTF]PWN——jarvisoj_level1
jarvisoj_level1 附件 步骤: 例行检查,32位程序,没有开任何保护 本地运行一下程序,看看大概的情况,可以看到输出了一个地址 32位ida载入,习惯性的检索程序里的字符串,没有发现可以 ...
- MySQL 定时器
mysql定时器是系统给提供了event,而oracle里面的定时器是系统给提供的job.废话少说,下面创建表:create table mytable (id int auto_increment ...
- 优雅的按键模块-----Multi-button
优雅的按键模块-----Multi-button 在我们日常开发和使用的过程中常常使用了一些按键,利用按键实现不同的功能,比如长按,短按,双击等等.但是每次都是采用标志等等来实现信息的读取,是否有 ...
- SpringBoot整合quartz框架启动定时任务报错:the given trigger will never fire.
org.quartz.SchedulerException: Based on configured schedule, the given trigger 'DEFAULT.cron_b1a91e1 ...