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

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM