3 - Jedis的八种调用方式(事务、管道、分布式)调用redis

package com.cui.redis01_helloworld;

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;

/** Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)
 * 
 * @author Administrator */
@SuppressWarnings("all")
public final class Redis02_JedisCall {

	/** 一、普通同步方式 最简单和基础的调用方式, */
	@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();
		jedis.close();
	}

	/** 二、事务方式(Transactions) redis的事务很简单,他主要目的是保障,一个client发起的事务中的命令可以连续的执行,而中间不会插入其他client的命令。 */
	@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("tt" + i, "tt" + i);
		}
		List results = tx.exec();

		System.out.println(results);
		long end = System.currentTimeMillis();
		System.out.println("Transaction SET: " + ((end - start) / 1000.0) + " seconds");
		jedis.disconnect();

		jedis.close();
		// System.out.println(tx.get("tt1000").get()); //不允许
	}

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

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

	/** 四、管道中调用事务 就Jedis提供的方法而言,是可以做到在管道中使用事务,其代码如下: */
	@Test
	public void test4combPipelineTrans() {
		Jedis 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();
		System.out.println(results);
		long end = System.currentTimeMillis();
		System.out.println("Pipelined transaction: " + ((end - start) / 1000.0) + " seconds");
		jedis.disconnect();
		jedis.close();
	}

	/** 五、分布式直连同步调用 这个是分布式直接连接,并且是同步调用,每步执行都返回执行结果。类似地,还有异步管道调用。
	 * 
	 * 数据会随机放到一个缓存中 */
	@Test
	public 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();
	}

	/** 六、分布式直连异步调用
	 * 
	 * 数据会随机放到一个缓存中 */
	@Test
	public 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();
	}

	/** 七、分布式连接池同步调用 如果,你的分布式调用代码是运行在线程中,那么上面两个直连调用方式就不合适了,因为直连方式是非线程安全的,这个时候,你就必须选择连接池调用。 */
	@Test
	public 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();
	}

	/** 上面是同步方式,当然还有异步方式。 八、分布式连接池异步调用 */
	@Test
	public 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();
	}

} 
  

 

你可能感兴趣的:(redis)