码迷,mamicode.com
首页 > 其他好文 > 详细

Redis学习笔记(9)-管道/分布式

时间:2016-06-23 20:41:25      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

package cn.com;

import java.util.Arrays;
import java.util.List;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPipeline;

public class Redis_Dispersed {

	/**
	 * 	分布式直连同步调用
	 * */
	public static void sync_dispersed(){
		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("userid" + i, "age" + i); 
	    } 
	    long end = System.currentTimeMillis(); 
	    System.out.println("执行实行: " + ((end - start)/1000.0) + " seconds"); 

	    sharding.disconnect(); 
		
	}
	
	/**
	 * 分布式直连异步调用
	 * */
	public static void shardpipelined() { 
		    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 < 100; i++) { 
		        pipeline.set("userid" + i, "age" + i); 
		    } 
		    List<Object> results = pipeline.syncAndReturnAll(); 
		    long end = System.currentTimeMillis(); 
		    System.out.println("执行时间: " + ((end - start)/1000.0) + " seconds"); 

		    sharding.disconnect(); 
	}
	
	/**
	 * 管道  pipeline方式是将一些列命令打包一起发送到服务器执行。
	 * 而服务端会处理完多条命令后会将多条命令的处理结果打包到一起返回给客户端
	 * 注意:打包的命令越多,缓存消耗内存也越多。所以并是不是打包的命令越多越好
	 * */
	public static void pipeline(){
		 	Jedis jedis = new Jedis("localhost",6379); 
		    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(); 
		    jedis.close();
	}
	/**
	 * 管道中调用事务
	 * */
	public static void trans_pipeline(){
		Jedis jedis = new Jedis("localhost",6379); 
	    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(); 
	}
	
	public static void main(String [] args){
		
		shardpipelined();
	}
}

  

Redis学习笔记(9)-管道/分布式

标签:

原文地址:http://www.cnblogs.com/zuolun2017/p/5611997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!