9、redis之事务2-Jedis的八种调用方式(事务、管道、分布式)介绍

1、普通同步

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

例子中的for循环里的每一个set操作都是一个事务。

2、事务方式(Transactions)

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

 1 @Test
 2 public void test2Trans() {
 3     Jedis jedis = new Jedis("localhost");
 4     long start = System.currentTimeMillis();
 5     Transaction tx = jedis.multi();
 6     for (int i = 0; i < 100000; i++) {
 7         tx.set("t" + i, "t" + i);
 8     }
 9     List<Object> results = tx.exec();
10     long end = System.currentTimeMillis();
11     System.out.println("Transaction SET: " + ((end - start)/1000.0) + " seconds");
12     jedis.disconnect();
13 }

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

3、管道(Pipelining)

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

 1 @Test
 2 public void test3Pipelined() {
 3     Jedis jedis = new Jedis("localhost");
 4     Pipeline pipeline = jedis.pipelined();
 5     long start = System.currentTimeMillis();
 6     for (int i = 0; i < 100000; i++) {
 7         pipeline.set("p" + i, "p" + i);
 8     }
 9     List<Object> results = pipeline.syncAndReturnAll();
10     long end = System.currentTimeMillis();
11     System.out.println("Pipelined SET: " + ((end - start)/1000.0) + " seconds");
12     jedis.disconnect();
13 }

4、管道中调用事务

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

 1 @Test
 2 public void test4combPipelineTrans() {
 3     jedis = new Jedis("localhost"); 
 4     long start = System.currentTimeMillis();
 5     Pipeline pipeline = jedis.pipelined();
 6     pipeline.multi();
 7     for (int i = 0; i < 100000; i++) {
 8         pipeline.set("" + i, "" + i);
 9     }
10     pipeline.exec();
11     List<Object> results = pipeline.syncAndReturnAll();
12     long end = System.currentTimeMillis();
13     System.out.println("Pipelined transaction: " + ((end - start)/1000.0) + " seconds");
14     jedis.disconnect();
15 }

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

5、分布式直连同步调用

 1 @Test
 2 public void test5shardNormal() {
 3     List<JedisShardInfo> shards = Arrays.asList(
 4             new JedisShardInfo("localhost",6379),
 5             new JedisShardInfo("localhost",6380));
 6  
 7     ShardedJedis sharding = new ShardedJedis(shards);
 8  
 9     long start = System.currentTimeMillis();
10     for (int i = 0; i < 100000; i++) {
11         String result = sharding.set("sn" + i, "n" + i);
12     }
13     long end = System.currentTimeMillis();
14     System.out.println("Simple@Sharing SET: " + ((end - start)/1000.0) + " seconds");
15  
16     sharding.disconnect();
17 }

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

6、分布式直连异步调用

 1 @Test
 2 public void test6shardpipelined() {
 3     List<JedisShardInfo> shards = Arrays.asList(
 4             new JedisShardInfo("localhost",6379),
 5             new JedisShardInfo("localhost",6380));
 6  
 7     ShardedJedis sharding = new ShardedJedis(shards);
 8  
 9     ShardedJedisPipeline pipeline = sharding.pipelined();
10     long start = System.currentTimeMillis();
11     for (int i = 0; i < 100000; i++) {
12         pipeline.set("sp" + i, "p" + i);
13     }
14     List<Object> results = pipeline.syncAndReturnAll();
15     long end = System.currentTimeMillis();
16     System.out.println("Pipelined@Sharing SET: " + ((end - start)/1000.0) + " seconds");
17  
18     sharding.disconnect();
19 }

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

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

 1 @Test
 2 public void test7shardSimplePool() {
 3     List<JedisShardInfo> shards = Arrays.asList(
 4             new JedisShardInfo("localhost",6379),
 5             new JedisShardInfo("localhost",6380));
 6  
 7     ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);
 8  
 9     ShardedJedis one = pool.getResource();
10  
11     long start = System.currentTimeMillis();
12     for (int i = 0; i < 100000; i++) {
13         String result = one.set("spn" + i, "n" + i);
14     }
15     long end = System.currentTimeMillis();
16     pool.returnResource(one);
17     System.out.println("Simple@Pool SET: " + ((end - start)/1000.0) + " seconds");
18  
19     pool.destroy();
20 }

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

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

 1 @Test
 2 public void test8shardPipelinedPool() {
 3     List<JedisShardInfo> shards = Arrays.asList(
 4             new JedisShardInfo("localhost",6379),
 5             new JedisShardInfo("localhost",6380));
 6  
 7     ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);
 8  
 9     ShardedJedis one = pool.getResource();
10  
11     ShardedJedisPipeline pipeline = one.pipelined();
12  
13     long start = System.currentTimeMillis();
14     for (int i = 0; i < 100000; i++) {
15         pipeline.set("sppn" + i, "n" + i);
16     }
17     List<Object> results = pipeline.syncAndReturnAll();
18     long end = System.currentTimeMillis();
19     pool.returnResource(one);
20     System.out.println("Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds");
21     pool.destroy();
22 }

需要注意的地方:

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

 1 Transaction tx = jedis.multi();
 2  for (int i = 0; i < 100000; i++) {
 3      tx.set("t" + i, "t" + i);
 4  }
 5  System.out.println(tx.get("t1000").get());  //不允许
 6  List<Object> results = tx.exec();
 7  8  9  Pipeline pipeline = jedis.pipelined();
10  long start = System.currentTimeMillis();
11  for (int i = 0; i < 100000; i++) {
12      pipeline.set("p" + i, "p" + i);
13  }
14  System.out.println(pipeline.get("p1000").get()); //不允许
15  List<Object> results = pipeline.syncAndReturnAll();

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

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

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

转载自:http://www.open-open.com/lib/view/open1410485827242.html

原文地址:https://www.cnblogs.com/yangzhilong/p/5276183.html