Redis的Java客戶端Jedis的八種調用方式(事務、管道、分佈式…)

redis是一個著名的key-value存儲系統,而做爲其官方推薦的java版客戶端jedis也很是強大和穩定,支持事務、管道及有jedis自身實現的分佈式。 
在這裏對jedis關於事務、管道和分佈式的調用方式作一個簡單的介紹和對比: 
1、普通同步方式 
最簡單和基礎的調用方式, java

@Test 
public void test1Normal() { 
    Jedis jedis = new Jedis("localhost"); 
    long start = System.currentTimeMillis(); 
    for (int i = 0; i < 100000; i++) { 
        String result = jedis.set("n" + i, "n" + i); 
    } 
    long end = System.currentTimeMillis(); 
    System.out.println("Simple SET: " + ((end - start)/1000.0) + " seconds"); 
    jedis.disconnect(); 
}

很簡單吧,每次set以後均可以返回結果,標記是否成功。 
2、事務方式(Transactions) 
redis的事務很簡單,他主要目的是保障,一個client發起的事務中的命令能夠連續的執行,而中間不會插入其餘client的命令。 
看下面例子: redis

@Test 
public void test2Trans() { 
    Jedis jedis = new Jedis("localhost"); 
    long start = System.currentTimeMillis(); 
    Transaction tx = jedis.multi(); 
    for (int i = 0; i < 100000; i++) { 
        tx.set("t" + i, "t" + i); 
    } 
    List<Object> results = tx.exec(); 
    long end = System.currentTimeMillis(); 
    System.out.println("Transaction SET: " + ((end - start)/1000.0) + " seconds"); 
    jedis.disconnect(); 
}

咱們調用jedis.watch(…)方法來監控key,若是調用後key值發生變化,則整個事務會執行失敗。另外,事務中某個操做失敗,並不會回滾其餘操做。這一點須要注意。還有,咱們可使用discard()方法來取消事務。 
3、管道(Pipelining) 
有時,咱們須要採用異步方式,一次發送多個指令,不一樣步等待其返回結果。這樣能夠取得很是好的執行效率。這就是管道,調用方法以下: sql

@Test 
public void test3Pipelined() { 
    Jedis jedis = new Jedis("localhost"); 
    Pipeline pipeline = jedis.pipelined(); 
    long start = System.currentTimeMillis(); 
    for (int i = 0; i < 100000; i++) { 
        pipeline.set("p" + i, "p" + i); 
    } 
    List<Object> results = pipeline.syncAndReturnAll(); 
    long end = System.currentTimeMillis(); 
    System.out.println("Pipelined SET: " + ((end - start)/1000.0) + " seconds"); 
    jedis.disconnect(); 
}

4、管道中調用事務 
就Jedis提供的方法而言,是能夠作到在管道中使用事務,其代碼以下:安全

@Test 
public void test4combPipelineTrans() { 
    jedis = new Jedis("localhost"); 
    long start = System.currentTimeMillis(); 
    Pipeline pipeline = jedis.pipelined(); 
    pipeline.multi(); 
    for (int i = 0; i < 100000; i++) { 
        pipeline.set("" + i, "" + i); 
    } 
    pipeline.exec(); 
    List<Object> results = pipeline.syncAndReturnAll(); 
    long end = System.currentTimeMillis(); 
    System.out.println("Pipelined transaction: " + ((end - start)/1000.0) + " seconds"); 
    jedis.disconnect(); 
}

可是經測試(見本文後續部分),發現其效率和單獨使用事務差很少,甚至還略微差點。 
5、分佈式直連同步調用 服務器

@Test 
public void test5shardNormal() { 
    List<JedisShardInfo> shards = Arrays.asList( 
            new JedisShardInfo("localhost",6379), 
            new JedisShardInfo("localhost",6380)); 

    ShardedJedis sharding = new ShardedJedis(shards); 

    long start = System.currentTimeMillis(); 
    for (int i = 0; i < 100000; i++) { 
        String result = sharding.set("sn" + i, "n" + i); 
    } 
    long end = System.currentTimeMillis(); 
    System.out.println("Simple@Sharing SET: " + ((end - start)/1000.0) + " seconds"); 

    sharding.disconnect(); 
}

這個是分佈式直接鏈接,而且是同步調用,每步執行都返回執行結果。相似地,還有異步管道調用。 
6、分佈式直連異步調用 異步

@Test 
public void test6shardpipelined() { 
    List<JedisShardInfo> shards = Arrays.asList( 
            new JedisShardInfo("localhost",6379), 
            new JedisShardInfo("localhost",6380)); 

    ShardedJedis sharding = new ShardedJedis(shards); 

    ShardedJedisPipeline pipeline = sharding.pipelined(); 
    long start = System.currentTimeMillis(); 
    for (int i = 0; i < 100000; i++) { 
        pipeline.set("sp" + i, "p" + i); 
    } 
    List<Object> results = pipeline.syncAndReturnAll(); 
    long end = System.currentTimeMillis(); 
    System.out.println("Pipelined@Sharing SET: " + ((end - start)/1000.0) + " seconds"); 

    sharding.disconnect(); 
}

7、分佈式鏈接池同步調用 (適用於2.2及如下版本)
若是,你的分佈式調用代碼是運行在線程中,那麼上面兩個直連調用方式就不合適了,由於直連方式是非線程安全的,這個時候,你就必須選擇鏈接池調用。 nosql

@Test 
public void test7shardSimplePool() { 
    List<JedisShardInfo> shards = Arrays.asList( 
            new JedisShardInfo("localhost",6379), 
            new JedisShardInfo("localhost",6380)); 

    ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); 

    ShardedJedis one = pool.getResource(); 

    long start = System.currentTimeMillis(); 
    for (int i = 0; i < 100000; i++) { 
        String result = one.set("spn" + i, "n" + i); 
    } 
    long end = System.currentTimeMillis(); 
    pool.returnResource(one); 
    System.out.println("Simple@Pool SET: " + ((end - start)/1000.0) + " seconds"); 

    pool.destroy(); 
}

上面是同步方式,固然還有異步方式。 
8、分佈式鏈接池異步調用 (適用於2.2及如下版本)分佈式

@Test 
public void test8shardPipelinedPool() { 
    List<JedisShardInfo> shards = Arrays.asList( 
            new JedisShardInfo("localhost",6379), 
            new JedisShardInfo("localhost",6380)); 

    ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); 

    ShardedJedis one = pool.getResource(); 

    ShardedJedisPipeline pipeline = one.pipelined(); 

    long start = System.currentTimeMillis(); 
    for (int i = 0; i < 100000; i++) { 
        pipeline.set("sppn" + i, "n" + i); 
    } 
    List<Object> results = pipeline.syncAndReturnAll(); 
    long end = System.currentTimeMillis(); 
    pool.returnResource(one); 
    System.out.println("Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds"); 
    pool.destroy(); 
}

9、須要注意的地方 
    事務和管道都是異步模式。在事務和管道中不能同步查詢結果。好比下面兩個調用,都是不容許的: 
 性能

Transaction tx = jedis.multi(); 
     for (int i = 0; i < 100000; i++) { 
         tx.set("t" + i, "t" + i); 
     } 
     System.out.println(tx.get("t1000").get());  //不容許 

     List<Object> results = tx.exec(); 

     … 
     … 

     Pipeline pipeline = jedis.pipelined(); 
     long start = System.currentTimeMillis(); 
     for (int i = 0; i < 100000; i++) { 
         pipeline.set("p" + i, "p" + i); 
     } 
     System.out.println(pipeline.get("p1000").get()); //不容許 

     List<Object> results = pipeline.syncAndReturnAll();

事務和管道都是異步的,我的感受,在管道中再進行事務調用,沒有必要,不如直接進行事務模式。 
    分佈式中,鏈接池的性能比直連的性能略好(見後續測試部分)。 
    分佈式調用中不支持事務。 
    由於事務是在服務器端實現,而在分佈式中,每批次的調用對象均可能訪問不一樣的機器,因此,無法進行事務。 
10、測試 
運行上面的代碼,進行測試,其結果以下: 測試

Simple SET: 5.227 seconds 

Transaction SET: 0.5 seconds 
Pipelined SET: 0.353 seconds 
Pipelined transaction: 0.509 seconds 

Simple@Sharing SET: 5.289 seconds 
Pipelined@Sharing SET: 0.348 seconds 

Simple@Pool SET: 5.039 seconds 
Pipelined@Pool SET: 0.401 seconds

另外,經測試分佈式中用到的機器越多,調用會越慢。上面是2片,下面是5片: 

Simple@Sharing SET: 5.494 seconds 
Pipelined@Sharing SET: 0.51 seconds 
Simple@Pool SET: 5.223 seconds 
Pipelined@Pool SET: 0.518 seconds

下面是10片: 

Simple@Sharing SET: 5.9 seconds 
Pipelined@Sharing SET: 0.794 seconds 
Simple@Pool SET: 5.624 seconds 
Pipelined@Pool SET: 0.762 seconds

下面是100片: 

Simple@Sharing SET: 14.055 seconds 
Pipelined@Sharing SET: 8.185 seconds 
Simple@Pool SET: 13.29 seconds 
Pipelined@Pool SET: 7.767 seconds

分佈式中,鏈接池方式調用不但線程安全外,根據上面的測試數據,也能夠看出鏈接池比直連的效率更好。 
11、完整的測試代碼 
 

package com.example.nosqlclient; 

import java.util.Arrays; 
import java.util.List; 

import org.junit.AfterClass; 
import org.junit.BeforeClass; 
import org.junit.Test; 

import redis.clients.jedis.Jedis; 
import redis.clients.jedis.JedisPoolConfig; 
import redis.clients.jedis.JedisShardInfo; 
import redis.clients.jedis.Pipeline; 
import redis.clients.jedis.ShardedJedis; 
import redis.clients.jedis.ShardedJedisPipeline; 
import redis.clients.jedis.ShardedJedisPool; 
import redis.clients.jedis.Transaction; 

import org.junit.FixMethodOrder; 
import org.junit.runners.MethodSorters; 

@FixMethodOrder(MethodSorters.NAME_ASCENDING) 
public class TestJedis { 

    private static Jedis jedis; 
    private static ShardedJedis sharding; 
    private static ShardedJedisPool pool; 

    @BeforeClass 
    public static void setUpBeforeClass() throws Exception { 
        List<JedisShardInfo> shards = Arrays.asList( 
                new JedisShardInfo("localhost",6379), 
                new JedisShardInfo("localhost",6379)); //使用相同的ip:port,僅做測試 


        jedis = new Jedis("localhost"); 
        sharding = new ShardedJedis(shards); 

        pool = new ShardedJedisPool(new JedisPoolConfig(), shards); 
    } 

    @AfterClass 
    public static void tearDownAfterClass() throws Exception { 
        jedis.disconnect(); 
        sharding.disconnect(); 
        pool.destroy(); 
    } 

    @Test 
    public void test1Normal() { 
        long start = System.currentTimeMillis(); 
        for (int i = 0; i < 100000; i++) { 
            String result = jedis.set("n" + i, "n" + i); 
        } 
        long end = System.currentTimeMillis(); 
        System.out.println("Simple SET: " + ((end - start)/1000.0) + " seconds"); 
    } 

    @Test 
    public void test2Trans() { 
        long start = System.currentTimeMillis(); 
        Transaction tx = jedis.multi(); 
        for (int i = 0; i < 100000; i++) { 
            tx.set("t" + i, "t" + i); 
        } 
        //System.out.println(tx.get("t1000").get()); 

        List<Object> results = tx.exec(); 
        long end = System.currentTimeMillis(); 
        System.out.println("Transaction SET: " + ((end - start)/1000.0) + " seconds"); 
    } 

    @Test 
    public void test3Pipelined() { 
        Pipeline pipeline = jedis.pipelined(); 
        long start = System.currentTimeMillis(); 
        for (int i = 0; i < 100000; i++) { 
            pipeline.set("p" + i, "p" + i); 
        } 
        //System.out.println(pipeline.get("p1000").get()); 
        List<Object> results = pipeline.syncAndReturnAll(); 
        long end = System.currentTimeMillis(); 
        System.out.println("Pipelined SET: " + ((end - start)/1000.0) + " seconds"); 
    } 

    @Test 
    public void test4combPipelineTrans() { 
        long start = System.currentTimeMillis(); 
        Pipeline pipeline = jedis.pipelined(); 
        pipeline.multi(); 
        for (int i = 0; i < 100000; i++) { 
            pipeline.set("" + i, "" + i); 
        } 
        pipeline.exec(); 
        List<Object> results = pipeline.syncAndReturnAll(); 
        long end = System.currentTimeMillis(); 
        System.out.println("Pipelined transaction: " + ((end - start)/1000.0) + " seconds"); 
    } 

    @Test 
    public void test5shardNormal() { 
        long start = System.currentTimeMillis(); 
        for (int i = 0; i < 100000; i++) { 
            String result = sharding.set("sn" + i, "n" + i); 
        } 
        long end = System.currentTimeMillis(); 
        System.out.println("Simple@Sharing SET: " + ((end - start)/1000.0) + " seconds"); 
    } 

    @Test 
    public void test6shardpipelined() { 
        ShardedJedisPipeline pipeline = sharding.pipelined(); 
        long start = System.currentTimeMillis(); 
        for (int i = 0; i < 100000; i++) { 
            pipeline.set("sp" + i, "p" + i); 
        } 
        List<Object> results = pipeline.syncAndReturnAll(); 
        long end = System.currentTimeMillis(); 
        System.out.println("Pipelined@Sharing SET: " + ((end - start)/1000.0) + " seconds"); 
    } 

    @Test 
    public void test7shardSimplePool() { 
        ShardedJedis one = pool.getResource(); 

        long start = System.currentTimeMillis(); 
        for (int i = 0; i < 100000; i++) { 
            String result = one.set("spn" + i, "n" + i); 
        } 
        long end = System.currentTimeMillis(); 
        pool.returnResource(one); 
        System.out.println("Simple@Pool SET: " + ((end - start)/1000.0) + " seconds"); 
    } 

    @Test 
    public void test8shardPipelinedPool() { 
        ShardedJedis one = pool.getResource(); 

        ShardedJedisPipeline pipeline = one.pipelined(); 

        long start = System.currentTimeMillis(); 
        for (int i = 0; i < 100000; i++) { 
            pipeline.set("sppn" + i, "n" + i); 
        } 
        List<Object> results = pipeline.syncAndReturnAll(); 
        long end = System.currentTimeMillis(); 
        pool.returnResource(one); 
        System.out.println("Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds"); 
    } 
}
相關文章
相關標籤/搜索