python使用pipeline批量读写redis的方法

1.插入数据

复制1. >>> import redis
2.  
3. >>> conn = redis.Redis(host='192.168.8.176',port=6379)
4.  
5. >>> pipe = conn.pipeline()
6.  
7. >>> pipe.hset("hash_key","leizhu900516",8)
8. Pipeline<ConnectionPool<Connection<host=192.168.8.176,port=6379,db=0>>>
9.  
10. >>> pipe.hset("hash_key","chenhuachao",9)
11. Pipeline<ConnectionPool<Connection<host=192.168.8.176,port=6379,db=0>>>
12.  
13. >>> pipe.hset("hash_key","wanger",10)
14. Pipeline<ConnectionPool<Connection<host=192.168.8.176,port=6379,db=0>>>
15.  
16. >>> pipe.execute()
17. [1L, 1L, 1L]
>>>

2.批量读取数据

复制1. &gt;>> pipe.hget("hash_key","leizhu900516")
2. Pipeline<ConnectionPool<Connection<host=192.168.8.176,port=6379,db=0>>>
3.  
4. >>> pipe.hget("hash_key","chenhuachao")
5. Pipeline<ConnectionPool<Connection<host=192.168.8.176,port=6379,db=0>>>
6.  
7. >>> pipe.hget("hash_key","wanger")
8. Pipeline<ConnectionPool<Connection<host=192.168.8.176,port=6379,db=0>>>
9.  
10. >>> result = pipe.execute()
11.  
12. >>> print result
13. ['8', '9', '10'] #有序的列表
14. >>>

3.###生产环境批量读写
总结:redis的pipeline就是这么简单,实际生产环境,
根据需要去编写相应的代码,思路同理。如下:

复制1. redis_db = redis.Redis(host='127.0.0.1',port=6379)
2. data = ['zhangsan', 'lisi', 'wangwu']
3.  
4. with redis_db.pipeline(transaction=False) as pipe:
5.      for i in data:
6.      pipe.zscore(self.key, i)
7.  
8.      result = pipe.execute()
9.  
10. print result
11. # [100, 80, 78]

线上的redis一般都是集群模式,集群模式下使用pipeline的时候,在创建pipeline的对象时,需要指定

复制pipe =conn.pipeline(transaction=False)

经过线上实测,利用pipeline取值3500条数据,大约需要900ms,如果配合线程or协程来使用,每秒返回1W数据是没有问题的,基本能满足大部分业务。



复制如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载!
posted @   莫贞俊晗  阅读(548)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2021-07-05 阿里、腾讯、网易、极验、顶象滑块验证码识别
2021-07-05 Python3 日期时间格式化(带毫秒)
点击右上角即可分享
微信分享提示