【发布时间】:2018-12-06 04:46:00
【问题描述】:
我想设置一些hash key的过期时间,如果是第一次hset key,我希望设置一个过期时间,否则我宁愿保留第一次设置的过期时间.
由于有大量的哈希键,我更喜欢在管道中进行,但是下面的函数不能很好地工作。
pipe.exists(hkey)这行返回一个管道的obj,它总是True,所以if子句总是去一个部分,不管hash key是否存在。
我的问题是:有没有一种方法可以通过管道的哈希键的存在来设置哈希键的到期?
def test1(hkey, v):
with r.pipeline() as pipe:
# tmp = pipe.exists(hkey)
# pipe.exists(hkey) is a pipe obj, which is always True,
# this line not work as expected and the two lines below it will never be excuted.
if not pipe.exists(hkey):
pipe.hset(hkey, v, v)
pipe.expire(hkey, 3600)
else:
# no matter whether the hash key is exist or not, the if else statment always goes to this line.
pipe.hset(hkey, v, v)
pipe.execute()
【问题讨论】: