redis中使用set更新一个有有效时间的key,会使有效时间变成永久有效

最近在开发中遇到一个问题,使用redis中的set更新一个含有有效时间的key时,会时这个key的有效时间变成永久有效
用客户端尝试:

127.0.0.1:6379> set test test
OK
127.0.0.1:6379> expire test 1000
(integer) 1
127.0.0.1:6379> set test test
OK
127.0.0.1:6379> ttl test
(integer) -1
127.0.0.1:6379>

查阅redis官方文档是这样介绍的
The timeout will only be cleared by commands that delete or overwrite the contents of the key, including DEL, SET, GETSET and all the *STORE commands. This means that all the operations that conceptually alter the value stored at the key without replacing it with a new one will leave the timeout untouched. For instance, incrementing the value of a key with INCR, pushing a new value into a list with LPUSH, or altering the field value of a hash with HSET are all operations that will leave the timeout untouched.

翻译:

如果用DEL, SET, GETSET会将key对应存储的值替换成新的,命令也会清除掉超时时间;如果list结构中添加一个数据或者改变hset数据的一个字段是不会清除超时时间的;如果想要通过set去覆盖值那就必须重新设置expire。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值