zoukankan      html  css  js  c++  java
  • 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秒
    View Code




    /**
    * 普通批量事物提交,, 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     }
    View Code

    /**
    * 普通异步管道提交,不需要等待执行完成后的结果
    */
     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     }
    View Code


    /**
    * 在异步管道中使用事务
    * 效率和单独使用事务差不多
    */
     @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("已关闭");
                }
            }
        }
    View Code



    /**
    * 分布式直连,普通同步操作
    * 与普通的单例连接速度一样
    */
     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     }
    View Code



    /**
    * 分布式直连,使用管道
    * <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     }
    View Code



    /**
    * 分布式连接池,适合多线程
    * <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     }
    View Code


    /**
    * 分布式连接池,异步管道调用
    */
     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     }
    View Code



        /**
    * 普通连接池同步
    */
        @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秒
    View Code


    /**
    * 普通连接池管道
    */
     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     }
    View Code


  • 相关阅读:
    MongoDB安装
    前端构建工具gulp入门教程
    限制input输入类型(多种方法实现)
    【E20200105-1】Centos 7.x 下vsftpd配置文件常用配置详解
    【E20200102-1】centos 7 下vsftp的安装和配置
    【E20200101-1】Centos 7.x 关闭防火墙(firewall)和SELinux
    Linux下如何修改用户默认目录
    linux给普通用户增加ssh权限
    在IIS7中应用Application Request Routing配置反向代理
    vmware相关服务默认禁用 修改服务弹出服务拒绝访问解决办法
  • 原文地址:https://www.cnblogs.com/c2g5201314/p/12216892.html
Copyright © 2011-2022 走看看