redis 管道 通讯
package com.irwin.redis; import java.util.Arrays; import java.util.List; 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; public class RedisConnectKindsTests { Jedis jedis = new Jedis("192.168.56.101", 6379); //普通同步方式 //最简单和基础的调用方式,每次执行命令后都可以返回结果,标记是否成功 @Test public void testNormal(){ long start = System.currentTimeMillis(); for (int i = 0; i < 5; i++) { String result = jedis.set("n" + i, "n" + i); System.out.println(result); } long end = System.currentTimeMillis(); System.out.println("Simple SET: " + ((end - start)/1000.0) + " seconds"); jedis.disconnect(); } //事务方式 //保障一个client发起的事务中的命令可以连续的执行,而中间不会插入其他client的命令 //调用jedis.watch(…)方法来监控key,如果调用后key值发生变化,则整个事务会执行失败。 //另外,事务中某个操作失败,并不会回滚其他操作。这一点需要注意。还有,我们可以使用discard()方法来取消事务。 @Test public void test2Transactions(){ long start = System.currentTimeMillis(); Transaction tx = jedis.multi(); for (int i = 0; i < 10; i++) { tx.set("t"+i, "t"+i); } System.out.println(jedis.watch("t1","t2")); //tx.discard(); List<Object> results = tx.exec(); long end = System.currentTimeMillis(); jedis.disconnect(); } //管道 //要采用异步方式,一次发送多个指令,不同步等待其返回结果 @Test public void test3Pipelined(){ Pipeline pipeline = jedis.pipelined(); for (int i = 0; i < 10; i++) { pipeline.set("p"+i, "p"+i); } List<Object> results = pipeline.syncAndReturnAll(); for (Object object : results) { System.out.println(object); } jedis.disconnect(); } //管道中调用事务 //Jedis提供的方法而言,是可以做到在管道中使用事务 @Test public void test4combPipelineTrans() { Pipeline pipeline = jedis.pipelined(); pipeline.multi(); for (int i = 0; i < 10; i++) { pipeline.set("" + i, "" + i); } List<Object> results = pipeline.syncAndReturnAll(); jedis.disconnect(); } //分布式直连同步调用 //分布式直接连接,并且是同步调用,每步执行都返回执行结果。类似地,还有异步管道调用 @Test public void test4ShardNormal(){ List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("192.168.56.101", 6379), new JedisShardInfo("192.168.56.101", 6179) ); ShardedJedis sharding = new ShardedJedis(shards); for (int i = 0; i < 10; i++) { String result = sharding.set("sn" + i, "n" + i); System.out.println(result); } sharding.disconnect(); } //分布式直连异步调用 @Test public void test6shardpipelined() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("192.168.56.101", 6379), new JedisShardInfo("192.168.56.101", 6179) ); 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(); } //分布式连接池同步调用 //分布式调用代码是运行在线程中,那么上面两个直连调用方式就不合适了, //因为直连方式是非线程安全的,这个时候,你就必须选择连接池调用 @Test public void test7shardSimplePool() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("192.168.56.101", 6379), new JedisShardInfo("192.168.56.101", 6179) ); ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); ShardedJedis one = pool.getResource(); long start = System.currentTimeMillis(); for (int i = 0; i < 10; i++) { String result = one.set("spn" + i, "n" + i); System.out.println(result); } long end = System.currentTimeMillis(); pool.returnResource(one); System.out.println("Simple@Pool SET: " + ((end - start)/1000.0) + " seconds"); pool.destroy(); } //分布式连接池异步调用 @Test public void test8shardPipelinedPool() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("192.168.56.101", 6379), new JedisShardInfo("192.168.56.101", 6179) ); 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(); } }
做个快乐的删库程序员,随时准备跑路^_^