心得:

/**
* 心得:
* 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的多种方式与心得的更多相关文章

  1. Java调用DLL有多种方式,常用的方式有JNative、JNA、JNI等。

    JNative方式调用dll JNative是一种能够使Java语言使调用DLL的一种技术,对JNI进行了封装,可能有些读者会有这样一个问题,JNative对JNI进行了封装,并且是一种跨语言的使用D ...

  2. java获得路径的多种方式

    本文讲解java语言中获得运行时路径的多种方式,包括java项目.java web项目.jar.weblogic等多种场景. 一.this.getClass().getClassLoader().ge ...

  3. linux系统下安装redis以及java调用redis

    关系型数据库:MySQL  Oracle 非关系型数据库:Redis 去掉主外键等关系数据库的关系性特性 1)安装redis编译的c环境,yum install gcc-c++ 2)将redis-2. ...

  4. java创建线程的多种方式

    java创建线程的四种方式 1.继承 Thread 类 通过继承 Thread 类,并重写它的 run 方法,我们就可以创建一个线程. 首先定义一个类来继承 Thread 类,重写 run 方法. 然 ...

  5. Java调用Redis集群

    前文 需要使用以下jar包 Maven项目引用以下配置: <dependency> <groupId>org.apache.commons</groupId> &l ...

  6. (转)java调用python脚本

    这篇博客旨在吐血分享今天遇到的java调用python脚本遇到的坑,折腾了3个多小时终于可以跑通了,代码超级短,但网上的好多资料都是抄来抄去的,很少有能够直接跑通的,尤其是针对你的python文件中用 ...

  7. java 获取classpath下文件多种方式

    java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...

  8. [OpenSource]浅谈.Net和Java互相调用的三种方式

    在很多的大型系统开发中,开发工具往往不限制于同一种开发语言,而是会使用多种开发语言的混合型开发.目前Java和.Net都声称自己占85%的市场份额,不管谁对谁错,Java和.Net是目前应用开发的两个 ...

  9. Redis(Windows安装方法与Java调用实例 & 配置文件参数说明 & Java使用Redis所用Jar包 & Redis与Memcached区别 & redis-cli.exe命令及示例)

    Windows下Redis的安装使用 0.前言 因为是初次使用,所以是在windows下进行安装和使用,参考了几篇博客,下面整理一下 1.安装Redis 官方网站:http://redis.io/ 官 ...

随机推荐

  1. 1、Redis简介

    一.NOSQL 1.什么是NOSQL? NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL". 指的是非关系型的数据库.NoSQL有时也称作Not On ...

  2. Jenkins触发构建

    目录 一.简介 二.时间触发 定时触发 轮询代码仓库 三.事件触发 由上游任务触发 gitlab通知触发 四.通用触发接口 GWT 提取参数 触发某个具体项目 过滤请求值 控制打印内容 控制响应 一. ...

  3. Jenkins监控

    目录 一.Monitoring插件 二.Prometheus监控 一.Monitoring插件 Monitoring插件(monitoring)使用JavaMelody,对Jenkins进行监控.插件 ...

  4. 安装xampp开发环境更改默认项目路径

    xampp开发环境中默认的项目路径在xampp下的htdocs文件下 如果想修改默认项目的位置步骤如下: 1)D:\xampp\apache\conf 找到httpd.conf打开 2)找到 Docu ...

  5. 字符串函数(Excel函数集团)

    此处文章均为本妖原创,供下载.学习.探讨! 文章下载源是Office365国内版1Driver,如有链接问题请联系我. 请勿用于商业!谢谢 下载地址:https://officecommunity-m ...

  6. 2. Go中defer使用注意事项

    1. 简介 defer 会在当前函数返回前执行传入的函数,它会经常被用于关闭文件描述符.关闭数据库连接以及解锁资源. 理解这句话主要在三个方面: 当前函数 返回前执行,当然函数可能没有返回值 传入的函 ...

  7. Numpy.frompyfunc()将计算单个值的函数转化为计算数组中每个元素的函数

    Numpy.frompyfunc()将计算单个值的函数转化为计算数组中每个元素的函数 不再通过遍历,对数组中的元素进行运算,利用frompyfunc()将计算单个值的函数转化为计算数组中每个元素的函数 ...

  8. mysql使用自定义序列实现row_number功能

    看了一些文章,终于知道该怎么在 mysql 里面实现 row_number() 排序 话不多说,show you the code: 第一步:建表: create table grades( `nam ...

  9. CF816A Karen and Morning 题解

    Content 给定一个时间 \(h:m\),求从现在这个时间开始到下一个离该时间最近的回文时间要多久? 数据范围:\(0\leqslant h\leqslant 23,0\leqslant m\le ...

  10. java 编程基础 Class对象 反射 :数组操作java.lang.reflect.Array类

    java.lang.reflect包下还提供了Array类 java.lang.reflect包下还提供了Array类,Array对象可以代表所有的数组.程序可以通过使 Array 来动态地创建数组, ...