redis pipeline实现,合并多个请求,可有效降低redis访问延迟

发布时间:2024年01月23日

上代码

import redis

try:
    pool = redis.ConnectionPool(host=host, port=port)
    r = redis.Redis(connection_pool=pool)
except Exception as e:
    print(f"Failed to connect to {host} with error: {e}")
try:
    pipeline = r.pipeline(transaction=False)  # Use the last Redis connection for the pipeline
    pipeline.get("twotoone_test")
    pipeline.zrevrange("twotoone_test", 0, -1, withscores=True)
    results = pipeline.execute()
    print(results)
except Exception as e:
    print(f"Failed to execute pipeline with error: {e}")

注意,r.pipeline(transaction=False)需要加上该参数,否则可能会有报错:

Err unknown or unsupported command ‘exec

文章来源:https://blog.csdn.net/qq_40450969/article/details/135767575
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。