1. 程式人生 > >Redis 更新(set) key值 會重置過期時間問題

Redis 更新(set) key值 會重置過期時間問題

今天在開發的過程中遇到了一個Redis的問題: 當你在redis中插入一個key值,並且設定了對應過期時間. 當過期時間還沒到的時候重新 更新 key值會導致 過期時間被重新整理, 針對這個問題: 我查看了下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。

 

所以: 重新set , getset 會重置過期時間, 希望各位遇到這類問題,做好準備.