`persist` 和 `set` 在 Redis 中有什么区别?

huangapple go评论39阅读模式
英文:

What is the difference between persist and set in redis?

问题

在Redis中,我可以像这样设置带有过期时间的键:

set key value ex 100


如果我想要移除过期时间,可以这样做:

set key value

**或者**

persist key


这两者之间有什么区别?
英文:

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?

答案1

得分: 2

PERSIST key 移除过期时间,无需重新指定值。

SET key value 设置新的值。过期时间会作为副作用被移除。

英文:

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.

答案2

得分: 0

那两个命令在删除键的过期时间方面产生相同的结果,但命令本身非常不同。

在Redis中,任何被覆盖的键-它的过期逻辑会被移除。当我们运行 SET key1 value1 时,因为键 key1 的值被覆盖,所以它的过期时间被移除。(注意:像 APPENDSETRANGE 这样的命令不会移除过期时间,因为它们不会覆盖键)

但是如果您想要在不覆盖键的情况下删除键的过期时间怎么办?这时您可以使用 PERSIST。它会移除过期时间并使键在数据库中永久存在。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月14日 18:02:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定