使用pipeline管道執行redis命令
阿新 • • 發佈:2018-11-14
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,代表執行成功或失敗