skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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 key key1 is overwritten, that is why it’s expiry is removed. (Note: Commands like APPEND, 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search