使用redis-cli
設定mykey
, 並且設定其過期時間為40s
, 然後, 我再重新設定mykey
, 這時mykey
的值mykey, 我不明白的是被重置為啥
也同時重置了?如果我僅僅想重置值, 不更改過期時間有沒辦法?
源碼🎜:🎜SET mykey "Hello"
EXPIRE mykey 40
EXISTS mykey
SET mykey "Hello"
EXISTS mykey
EXISTS mykey
大家讲道理2017-04-27 09:04:48
上官方網站看了一下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。
點擊連結
怪我咯2017-04-27 09:04:48
EXPIREAT和EXPIRE 更新value都會重置過期時間。
set之前透過ttl取得到key的過期時間
set之後再把ttl的值給設成過期時間
192.168.1.9:6379> set key value
OK
192.168.1.9:6379> expire key 100
(integer) 1
192.168.1.9:6379> ttl key
(integer) 98
192.168.1.9:6379> set key value1
OK
192.168.1.9:6379> expire key 98
(integer) 1
192.168.1.9:6379> ttl key
(integer) 96
192.168.1.9:6379>
但這樣子會有誤差,不知道其他人有沒有更好的方法。