Redis官方解释
上官方网站看了一下expire的说明:
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。
我的需求
我的目的很简单,比如现在当天的更新次数,每次更新则key的value+1,隔天value归零
试过redis 的set后发现set是对原先的key进行覆盖,那么我也就无法做到隔天归零
解决方法
解决方法很简单,在key中带上当天的日期,每次设置过期时间为 Ymd 23:59:59 - time()
这样避免每日的key在redis中无意义的持久化
很简单对吧,只是思路要对,不然只会将事情复杂化