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.
上官方網站看了一下expire的說明:
這樣解釋的:
如果用DEL, SET, GETSET會將key對應儲存的值替換成新的,命令也會清除掉超時時間;如果list結構中新增一個資料或改變hset資料的一個欄位是不會清除逾時時間的;如果想要透過set去覆蓋值那就必須重新設定expire。
點擊連結
EXPIREAT和EXPIRE 更新value都會重置過期時間。
set之前透過ttl取得到key的過期時間
set之後再把ttl的值給設成過期時間
但這樣子會有誤差,不知道其他人有沒有更好的方法。