Redis管道Pipeline使用

更多访问redis高级知识 www.itkc8.com

Redis管道Pipeline使用
重要说明: 使用管道发送命令时,服务器将被迫回复一个队列答复,占用很多内存。所以,如果你需要发送大量的命令,最好是把他们按照合理数量分批次的处理,例如10K的命令,读回复,然后再发送另一个10k的命令,等等。

项目地址: https://github.com/csy512889371/learndemo/tree/master/ctoedu-redis

更多干货
分布式实战(干货)
spring cloud 实战(干货)
mybatis 实战(干货)
spring boot 实战(干货)
React 入门实战(干货)
构建中小型互联网企业架构(干货)
关于Pipeline 同步数据的问题
A、Pipeline 有与redis形同的操作,但是在数据落盘的时候需要在执行的方法后添加sync()方法,如果insert时有多条数据,在数据拼接完之后,在执行sync()方法,这样可以提高效率。
B、如果在hget()时没有sync()时会报,没有在hget()同步数据
C、如果在hset(),hdel(),hget()获取数据时都没有执行sync()方法,但是在最后执行了pl.close()方法,Pipeline 同样会执行sync()方法
相关代码
不用pipeline存储数据
用pipeline存储数据
直接使用Jedis hgetall
使用pipeline hgetall
public static void main(String[] args) {
        Jedis jedis = new CtoeduJedisPool().getJedis();
        Map<String, String> data = new HashMap<String, String>();

        //不用pipeline存储数据
        //选择redis的库
        jedis.select(4);
        jedis.flushDB();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            data.clear();
            data.put("k_" + i, "v_" + i);
            jedis.hmset("k_" + i, data);
        }
        long end = System.currentTimeMillis();
        System.out.println("datasize=" + jedis.dbSize());
        System.out.println("hmset without pipeline used=" + (end - start) / 1000 + "seconds!");

        //用pipeline存储数据
        jedis.select(4);
        jedis.flushDB();
        Pipeline pipeline = jedis.pipelined();
        start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            data.clear();
            data.put("k_" + i, "v_" + i);
            pipeline.hmset("k_" + i, data);
        }
        pipeline.sync();
        end = System.currentTimeMillis();
        System.out.println("datasize=" + jedis.dbSize());
        System.out.println("hmset with pipeline used=" + (end - start) / 1000 + "seconds!");


        Set<String> keys = jedis.keys("*");
        // 直接使用Jedis hgetall
        start = System.currentTimeMillis();
        Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>>();
        for (String key : keys) {
            result.put(key, jedis.hgetAll(key));
        }
        end = System.currentTimeMillis();
        System.out.println("result size:[" + result.size() + "] ..");
        System.out.println("hgetAll without pipeline used [" + (end - start) / 1000 + "] seconds ..");
        
        // 使用pipeline hgetall
        Map<String, Response<Map<String, String>>> responses =
                new HashMap<String, Response<Map<String, String>>>(
                        keys.size());
        result.clear();
        start = System.currentTimeMillis();
        for (String key : keys) {
            responses.put(key, pipeline.hgetAll(key));
        }
        pipeline.sync();
        for (String k : responses.keySet()) {
            result.put(k, responses.get(k).get());
        }
        end = System.currentTimeMillis();
        System.out.println("result size:[" + result.size() + "] ..");
        System.out.println("hgetAll with pipeline used [" + (end - start) / 1000 + "] seconds ..");
        jedis.disconnect();
    }

更多访问redis高级知识 www.itkc8.com
https://blog.csdn.net/qq_27384769/article/details/79498843

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值