zoukankan      html  css  js  c++  java
  • Redis管道理解

    Redis管道理解

    简介

    管道并不是Redis本身提供的功能,通常是客户端提供的功能;

    管道就是打包多条无关命令批量执行,以减少多个命令分别执行消耗的网络交互时间(TCP网络交互),可以显著提升Redis的性能;

    管道使用的场景并不适用于,必须知道每次交互结果的场景或者当前的执行依赖于上一次的执行结果等等,相反的,比较适用于对于可靠性不高,允许一定程度的失败,并且不需要立即得到执行的反馈,比如群发短信服务;

    需要注意的是,如果以管道处理的形式发送大批的命令,那么Redis必须将这些命令都执行完存储在内存中,也就是说,并不是批量的命令个数越多越好,否则会造成资源的浪费;

    操作

    # -*- coding: utf-8 -*-

    # @Time   : 2019/4/13 5:28 AM
    # @Author : George
    # @File   : pipeline.py
    # @Contact : georgewang1994@163.com

    from redis import StrictRedis
    import time
    conn = StrictRedis()

    cache_key_list = ['testing_pipeline_%s' for i in range(10)]
    count = 10000
    num_list = [num for num in range(count)]

    # 遍历加入
    start_time1 = time.time()
    for cache_key in cache_key_list:
       for num in num_list:
           conn.sadd(cache_key, num)
    end_time1 = time.time()
    print u"遍历加入花费时间: %s's" % (end_time1 - start_time1)

    # 命令一次性加入
    start_time2 = time.time()
    for cache_key in cache_key_list:
       conn.sadd(cache_key, *num_list)
    end_time2 = time.time()
    print u"命令一次性加入花费时间: %s's" % (end_time2 - start_time2)

    # 管道加入
    start_time3 = time.time()
    pipe = conn.pipeline(transaction=False)
    for cache_key in cache_key_list:
       pipe.sadd(cache_key, *num_list)
    pipe.execute()
    end_time3 = time.time()
    print u"管道加入花费时间: %s's" % (end_time3 - start_time3)

    # 运行结果
    # 遍历加入花费时间: 11.5690069199's
    # 命令一次性加入花费时间: 0.477045059204's
    # 管道加入花费时间: 0.41309595108's

    原理

    todo: 以后补充

  • 相关阅读:
    redis 事务 lq
    redis集群架构高可用分析 lq
    redis分布式锁 lq
    jvm垃圾回收 lq
    【攻防世界】逆向666
    第六届上海市大学生网络安全大赛wp
    【攻防世界】逆向answer_to_everying
    2021 DJBCTF(大吉大利杯) wp
    渗透靶机bossplayersCTF
    2020首届祥云杯部分wp
  • 原文地址:https://www.cnblogs.com/George1994/p/10699659.html
Copyright © 2011-2022 走看看