python判断redis中key是否存在_如何根据密钥的存在情况在redis中设置hash key过期

我想设置一些散列键的过期时间,如果是第一次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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值