码迷,mamicode.com
首页 > 其他好文 > 详细

使用pipeline管道执行redis命令

时间:2018-11-14 14:31:58      阅读:519      评论:0      收藏:0      [点我收藏+]

标签:col   失败   redis命令   false   ant   code   分享图片   end   com   

pipeline管道可以减少后端与redis的连接次数,从而实现了优化。

  • 原理如下:

技术分享图片

 

使用方法:

未使用pipeline前:

strict_redis = get_redis_connection(sms_codes)  # type:StrictRedis
strict_redis.setex(sms_%s % mobile,constants.SMS_CODE_REDIS_EXPIRES, sms_codes)
strict_redis.setex(send_flag_%s % mobile,constants.SEND_SMS_CODE_INTERVAL, 1)

使用pipeline后:

strict_redis = get_redis_connection(sms_codes)  # type:StrictRedis
pipeline = strict_redis.pipeline()  # type:pipeline
pipeline.setex(sms_%s % mobile,constants.SMS_CODE_REDIS_EXPIRES, sms_codes)
pipeline.setex(send_flag_%s % mobile,constants.SEND_SMS_CODE_INTERVAL, 1)
pipeline.execute()

拓展:

pipline.execute()有返回值,是一个列表,返回值的True或False,代表执行成功或失败

使用pipeline管道执行redis命令

标签:col   失败   redis命令   false   ant   code   分享图片   end   com   

原文地址:https://www.cnblogs.com/chichung/p/9957399.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!