千家信息网

Redis Java客户端Jedis的八种调用方式是什么

发表于:2024-11-11 作者:千家信息网编辑
千家信息网最后更新 2024年11月11日,本篇文章为大家展示了Redis Java客户端Jedis的八种调用方式是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。在这里对jedis关于事务、管道和分
千家信息网最后更新 2024年11月11日Redis Java客户端Jedis的八种调用方式是什么

本篇文章为大家展示了Redis Java客户端Jedis的八种调用方式是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

在这里对jedis关于事务、管道和分布式的调用方式做一个简单的介绍和对比:

一、普通同步方式

最简单和基础的调用方式,

@Testpublic 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之后都可以返回结果,标记是否成功。

二、事务方式(Transactions)

redis的事务很简单,他主要目的是保障,一个client发起的事务中的命令可以连续的执行,而中间不会插入其他client的命令。

看下面例子:

@Testpublic 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 results = tx.exec();    long end = System.currentTimeMillis();    System.out.println("Transaction SET: " + ((end - start)/1000.0) + " seconds");    jedis.disconnect();}

我们调用jedis.watch(…)方法来监控key,如果调用后key值发生变化,则整个事务会执行失败。另外,事务中某个操作失败,并不会回滚其他操作。这一点需要注意。还有,我们可以使用discard()方法来取消事务。

三、管道(Pipelining)

有时,我们需要采用异步方式,一次发送多个指令,不同步等待其返回结果。这样可以取得非常好的执行效率。这就是管道,调用方法如下:

@Testpublic 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 results = pipeline.syncAndReturnAll();    long end = System.currentTimeMillis();    System.out.println("Pipelined SET: " + ((end - start)/1000.0) + " seconds");    jedis.disconnect();}

四、管道中调用事务

就Jedis提供的方法而言,是可以做到在管道中使用事务,其代码如下:

@Testpublic 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 results = pipeline.syncAndReturnAll();    long end = System.currentTimeMillis();    System.out.println("Pipelined transaction: " + ((end - start)/1000.0) + " seconds");    jedis.disconnect();}

但是经测试(见本文后续部分),发现其效率和单独使用事务差不多,甚至还略微差点。

五、分布式直连同步调用

@Testpublic void test5shardNormal() {    List 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();}

这个是分布式直接连接,并且是同步调用,每步执行都返回执行结果。类似地,还有异步管道调用。

六、分布式直连异步调用

@Testpublic void test6shardpipelined() {    List 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 results = pipeline.syncAndReturnAll();    long end = System.currentTimeMillis();    System.out.println("Pipelined@Sharing SET: " + ((end - start)/1000.0) + " seconds");    sharding.disconnect();}

七、分布式连接池同步调用

如果,你的分布式调用代码是运行在线程中,那么上面两个直连调用方式就不合适了,因为直连方式是非线程安全的,这个时候,你就必须选择连接池调用。

@Testpublic void test7shardSimplePool() {    List 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();}

上面是同步方式,当然还有异步方式。

八、分布式连接池异步调用

@Testpublic void test8shardPipelinedPool() {    List 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 results = pipeline.syncAndReturnAll();    long end = System.currentTimeMillis();    pool.returnResource(one);    System.out.println("Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds");    pool.destroy();}

九、需要注意的地方

  1. 事务和管道都是异步模式。在事务和管道中不能同步查询结果。比如下面两个调用,都是不允许的:

     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 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 results = pipeline.syncAndReturnAll();


  2. 事务和管道都是异步的,个人感觉,在管道中再进行事务调用,没有必要,不如直接进行事务模式。

  3. 分布式中,连接池的性能比直连的性能略好(见后续测试部分)。

  4. 分布式调用中不支持事务。

    因为事务是在服务器端实现,而在分布式中,每批次的调用对象都可能访问不同的机器,所以,没法进行事务。

  5. 十、测试

    运行上面的代码,进行测试,其结果如下:

    Simple SET: 5.227 secondsTransaction SET: 0.5 secondsPipelined SET: 0.353 secondsPipelined transaction: 0.509 secondsSimple@Sharing SET: 5.289 secondsPipelined@Sharing SET: 0.348 secondsSimple@Pool SET: 5.039 secondsPipelined@Pool SET: 0.401 seconds

    另外,经测试分布式中用到的机器越多,调用会越慢。上面是2片,下面是5片:

    Simple@Sharing SET: 5.494 secondsPipelined@Sharing SET: 0.51 secondsSimple@Pool SET: 5.223 secondsPipelined@Pool SET: 0.518 seconds

    下面是10片:

    Simple@Sharing SET: 5.9 secondsPipelined@Sharing SET: 0.794 secondsSimple@Pool SET: 5.624 secondsPipelined@Pool SET: 0.762 seconds

    下面是100片:

    Simple@Sharing SET: 14.055 secondsPipelined@Sharing SET: 8.185 secondsSimple@Pool SET: 13.29 secondsPipelined@Pool SET: 7.767 seconds

    分布式中,连接池方式调用不但线程安全外,根据上面的测试数据,也可以看出连接池比直连的效率更好。

    十一、完整的测试代码

    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 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 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 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 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 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 results = pipeline.syncAndReturnAll();        long end = System.currentTimeMillis();        pool.returnResource(one);        System.out.println("Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds");    }}

    上述内容就是Redis Java客户端Jedis的八种调用方式是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

    0