使用pipeline管道执行redis命令

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,代表执行成功或失败

猜你喜欢

转载自www.cnblogs.com/chichung/p/9957399.html