Java 使用pipeline对redis进行批量读写

code

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import java.util.List;

public class testRedisPipeline {
public void test3Pipelined() {
Jedis jedis
= new Jedis("10.18.3.153", 7002);

    Pipeline pipeline </span>=<span style="color: rgba(0, 0, 0, 1)"> jedis.pipelined();
    </span><span style="color: rgba(0, 0, 255, 1)">long</span> start =<span style="color: rgba(0, 0, 0, 1)"> System.currentTimeMillis();
    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; 1000; i++<span style="color: rgba(0, 0, 0, 1)">) {
        pipeline.set(</span>"p" + i, "p" +<span style="color: rgba(0, 0, 0, 1)"> i);
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">List&lt;Object&gt; results = pipeline.syncAndReturnAll();      //  ---- 1</span>

}
List
<Object> results = pipeline.syncAndReturnAll(); // ------2
long end = System.currentTimeMillis();
System.out.println(
"Pipelined SET:" + ((end - start)/1000.0) + "seconds");
jedis.disconnect();
}
public static void main(String []args){
testRedisPipeline obj
= new testRedisPipeline();
obj.test3Pipelined();

}

}

结果对比 (使用代码中的 1,2 行代码)

  耗时 (s)
1 40.246
2 0.245