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 = jedis.pipelined();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            pipeline.set("p" + i, "p" + i);
            //List<Object> results = pipeline.syncAndReturnAll();      //  ---- 1
        }
        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
posted @ 2017-03-23 22:22  jihite  阅读(3751)  评论(0编辑  收藏  举报