I would like to set the expiration of some hash keys, and if it is the first time to hset the key, I wish to set an expiration, other wise, I prefer to keep the expiration that is set at the first time.
Since there are huge of hash keys, I prefer to do it in pipeline, however the function below not works well.
The line pipe.exists(hkey)
return an obj of pipe, which is always True, so the if clause always go to one part regardless of the existence of the hash key.
And my question is: is there a method to set the expiration of a hash key according to the existence of the hash key with pipeline?
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()