• 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: 以后补充

  • 相关阅读:
    Controlling behavior of existing services in system
    获取站点路径方法
    如何处理DataTable.Select();执行后重新排序的问题!
    ASP.NET 2.0 编程珠玑之五调试已创建好的代码
    Meal Scheduler in C#
    DHTML+Ajax? MXML+ActionScript? XAML+C#? 是巧合,还是必然?
    C#验证输入的是否数字
    Monitoring System Usage using Windows Service
    ADO.NET 2.0中的DataSet和DataTable
    使用asp.net 2.0和SQL SERVER 2005构建多层应用
  • 原文地址:https://www.cnblogs.com/George1994/p/10699659.html
Copyright © 2020-2023  润新知