In Redis, I can set a key with expire time like
set key value ex 100
If I want to remove the expire time, I can do like
set key value
OR
persist key
What is the difference between this two?
In Redis, I can set a key with expire time like
set key value ex 100
If I want to remove the expire time, I can do like
set key value
OR
persist key
What is the difference between this two?
2
Answers
PERSIST key
removes the expiration without the need to re-specify the value.SET key value
sets a new value. The expiration is removed as a side-effect.Those two commands have same results on a key in terms of removing the expiry of any key, but the commands themselves are very different.
In Redis, any key which is overwritten – it’s expiry logic is removed. When we run
SET key1 value1
– because the value of keykey1
is overwritten, that is why it’s expiry is removed. (Note: Commands likeAPPEND
,SETRANGE
do not remove the expiry because they don’t overwrite the key)But what if you want to remove the expiry of the key without overwriting it? That’s when you use
PERSIST
. It removes the expiry and makes the key permanent in the DB.