我想设置一些散列键的过期时间,如果是第一次hset键,我希望设置一个过期,反之,我更喜欢保留第一次设置的过期。在
因为散列键太多了,我更喜欢在流水线中实现,但是下面的函数效果不好。在
第pipe.exists(hkey)行返回管道的obj,该值始终为True,因此if子句总是转到一个部分,而不管哈希键是否存在。在
我的问题是:有没有一种方法可以根据hash key的存在来设置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()