首页 > 解决方案 > hincrby 和 hget 在 Redis (python) 中返回 True 而不是实际值

问题描述

为了提高性能,我使用了 Redis 管道而不是单次插入。请找到相同的代码片段。

r = redis.Redis(connection_pool=redis_pool)
r_pipeline = r.pipeline()
for key in keys:
    r_pipeline.hincrby(key, hash, amount)
    r_pipeline.expire(key, Globals.Cache.ttl)
return r_pipeline.execute()

r_pipeline.execute() 的返回值是一个列表。根据文档,它应该增加并返回增加的值。但有时它实际上是返回值,有时它只是返回True

我浏览了文档并进行了谷歌搜索,但仍然无法弄清楚为什么 hincrby 在管道中返回 True。

有人可以帮忙吗。

标签: pythonredispipeline

解决方案


来自True管道中的过期调用。处于隔离状态:

>>> p.hincrby('key', 'val', 1)
Pipeline<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
>>> p.expire('key', 120)
Pipeline<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
>>> print(p.execute())
[1L, True]

推荐阅读