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


    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();
    	}
    }
    

      

  • 相关阅读:
    AVL树的旋转操作详解
    KMP算法的优化与详解
    手机号码等的正则验证
    Josephus问题的java实现
    Match类解析
    this的相关介绍与用法
    Iterator——迭代接口
    final对于访问效率的影响
    final关键字介绍
    JAVA事件监听机制与实现
  • 原文地址:https://www.cnblogs.com/zuolun2017/p/5611997.html
Copyright © 2020-2023  润新知