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

  • 相关阅读:
    如何使用Junit
    CSS简单动画效果
    编程类软件下载地址
    常用的工具包-下载地址
    连接数据库常用工具类(二)------C3P0Utils工具类
    连接数据库时常用的工具类(一)-------C3P0XmlUtils
    浏览器输入服务器端口号来访问html网页
    使用C/S结构实现客户端上传本地文件到服务器
    冒泡排序
    一个注册界面
  • 原文地址:https://www.cnblogs.com/George1994/p/10699659.html
Copyright © 2011-2022 走看看