前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Redis01】Pipeline

【Redis01】Pipeline

作者头像
JuneBao
发布2022-10-26 14:36:08
1600
发布2022-10-26 14:36:08
举报
文章被收录于专栏:JuneBao

Redis-Pipeline

正常的Redis 命令的生命周期是 Client 给 Server 发一条命令,Server 执行后把结果反馈给 Client,但这个过程中, Client 和 Server 之间的通信会花费大量时间,pipeline 的思路就是每一次 Client 和 Server 通信不再是一次发一条命令(/结果)而是把一批命令打包传输给 Server,然后Server把这些命令按顺序执行的结果反馈给 Client

pipeline 并不是原子命令,在执行时,他是以子命令的形式穿插在Redis正在执行的其他命令之间的

通过python测试,效果非常明显

代码语言:javascript
复制
import redis
# run_time 是我自己写的一个装饰器,用来返回被装饰对象的执行时间
from my_python_package.Modifys import run_time


def redis_connection(address: str, port: int, password: str):
    return redis.StrictRedis(address, port, password=password)


@run_time
def execute_multiple_commands_without_pipes(client):
    for i in range(1000):
        client.set(f"string{i}", i)


@run_time
def execute_multiple_commands_with_pipes(client):
    pipeline = client.pipeline()
    for i in range(1000):
        pipeline.set(f"string with pipes{i}", i)
    pipeline.execute()


if __name__ == '__main__':
    client = redis_connection("0.0.0.0", 8100, "password")
    # execute_multiple_commands_without_pipes(client)  # execute_multiple_commands_without_pipes runs at:35.10040497779846 s
    execute_multiple_commands_with_pipes(client)  # execute_multiple_commands_with_pipes runs at:0.17360401153564453 s

同样是执行1000次写入,使用pipeline效率提高了两百多倍。多出来的这些时间就是节省的网络传输的时间,

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-2-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Redis-Pipeline
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档