(一)简介 
Redis客户端与redis之间使用TCP协议进行连接,一个客户端可以通过一个socket连接发起多个请求命令。每个请求命令发出后client通常会阻塞并等待redis服务处理,redis处理完后请求命令后会将结果通过响应报文返回给client,因此当执行多条命令的时候都需要等待上一条命令执行完毕才能执行,如:get ‘0’,get ‘1’,get ‘2’

Redis中PipeLine使用(11)

其执行过程如下图所示:

Redis中PipeLine使用(11)

而管道(pipeline)可以一次性发送多条命令并在执行完后一次性将结果返回,pipeline通过减少客户端与redis的通信次数来实现降低往返延时时间,其过程如下图所示 
Redis中PipeLine使用(11)

(二)普通方式与pipeline的比较

1.测试环境: 
redis版本2.4.15 
jedis版本2.2.1

2.测试代码 
分别向redis中插入100、500、1000、2000、5000、10000数据

  1. long start=System.currentTimeMillis();  
  2.       for (int i = 0; i <50000; i++) {  
  3.           redis.set(String.valueOf(i),String.valueOf(i));  
  4.       }  
  5.       long end=System.currentTimeMillis();  
  6.       logger.info("the total time is:"+(end-start));  
  7.   
  8.       Pipeline pipe=redis.pipelined();  
  9.       long start_pipe=System.currentTimeMillis();  
  10.       for (int i = 0; i <50000; i++) {  
  11.           pipe.set(String.valueOf(i),String.valueOf(i));  
  12.       }  
  13.       pipe.sync();  
  14.       long end_pipe=System.currentTimeMillis();  
  15.       logger.info("the pipe total time is:"+(end_pipe-start_pipe)); 

测试结果对比: 

Redis中PipeLine使用(11)

批量查询的相关问题总结

再做测试之前首先向Redis中批量插入一组数据

 1-->1  
    2-->2  
    3-->3  
    4-->4  
    5-->5  
    6-->6  

现在批量get数据

    for (Entry<String,String> entry :map.entrySet())       
            {  
                 pipe.get(entry.getKey().getBytes());  
            }  
            List<Object> list=pipe.syncAndReturnAll();  
      
            for(Object o:list){  
                byte[] tmp=(byte[])o;  
                System.out.println("---->"+new String(tmp));  
            }  

其打印结果是:
这里写图片描述
然而这并不是我们想要的,那怎样才能知道打印的这个value对应的key呢?
这样的应用场景很多,我们首先批量从redis get数据出来,然后将get的数据与内存中的数据进行运行在批量写入数据库!
这是需要引入一个HashMap

    HashMap<String, String> map=new HashMap<String, String>();  
            map.put("1","1");  
            map.put("2","2");  
            map.put("3","3");  
            map.put("4","4");  
            map.put("5","5");  
            map.put("6","6");  
      
            Pipeline pipe=redis.pipelined();  
      
            HashMap<byte[], Response<byte[]>> newMap=new HashMap<byte[], Response<byte[]>>();  
      
            for (Entry<String,String> entry :map.entrySet()) {  
                newMap.put(entry.getKey().getBytes(), pipe.get(entry.getKey().getBytes()));  
            }  
      
            pipe.sync();  
      
            for (Entry<byte[], Response<byte[]>> entry :newMap.entrySet()) {  
                Response<byte[]> sResponse=(Response<byte[]>)entry.getValue();  
                System.out.println(new String(entry.getKey())+"-----"+new String(sResponse.get()).toString());  
            }  

        这里写图片描述

批量get之后与内存的数据想加再批量set

    HashMap<String, String> map=new HashMap<String, String>();  
            map.put("1","1");  
            map.put("2","2");  
            map.put("3","3");  
            map.put("4","4");  
            map.put("5","5");  
            map.put("6","6");  
      
            Pipeline pipe=redis.pipelined();  
      
            HashMap<byte[], Response<byte[]>> newMap=new HashMap<byte[], Response<byte[]>>();  
      
            for (Entry<String,String> entry :map.entrySet()) {  
                newMap.put(entry.getKey().getBytes(), pipe.get(entry.getKey().getBytes()));  
            }  
      
            pipe.sync();  
      
            for (Entry<byte[], Response<byte[]>> entry :newMap.entrySet()) {  
                Response<byte[]> sResponse=(Response<byte[]>)entry.getValue();  
                long temp=Long.valueOf(Long.parseLong(map.get(new String(entry.getKey())))+Long.parseLong(new String(sResponse.get()).toString()));  
                map.put(new String(entry.getKey()), Long.toString(temp));  
            }  
      
            for (Entry<String,String> entry :map.entrySet()) {  
               pipe.set(entry.getKey().getBytes(), entry.getValue().getBytes());  
            }  
            pipe.sync(); 

相关文章: