Redis:存储没有过期时间的键值对

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

Redis: storing key/value pair without expiration time

问题

以下是您要求的翻译:

能否有人解释一下如何在Redis中存储键值对,并保持永久有效?

我试图在stackoverflow和谷歌上找到一些信息,但我什么都没找到。

我正在使用redisTemplate,我看到那里只有一个expire方法,需要指定超时时间。我可以最终将超时时间设置为999999999天,但我认为这并不是最佳解决方案。

英文:

Can someone explain me how to store key/value pair in redis with unlimited exipire time?

I was trying to find some information at stackoverflow and google and I found nothing.

I'm using redisTemplate and I see there is only expire method there with timeout to specify. I can eventually set the timeout to value like 999999999 days, but I think thats not the best solution.

答案1

得分: 1

默认情况下,密钥没有设置过期时间。也就是说,如果您没有自己指定过期时间,您的密钥将无限期存储而不会过期。

set myname Ankit
OK

ttl myname
(integer) -1

返回 -1 告诉我们密钥 myname 没有设置过期时间。

英文:

By default, no expiry is set on the keys. That is, if you do not specify an expiry yourself, your key will be stored indefinetely without any expiry.

set myname Ankit
OK

ttl myname
(integer) -1

A return of -1 tell us that there is no expiry set on the key myname

答案2

得分: 0

最后我找到了两个答案来解决这个问题 - 首先我可以使用带有 -1 值的 expire 方法作为超时:

```java
redisTemplate.expire("mykey", -1);

第二种方法是在键上使用 persist 方法:

redisTemplate.persist("mykey");

这将移除 "mykey" 键的过期时间, effectively 设置它永不过期。


<details>
<summary>英文:</summary>

Finally I found two answers to this question - at first I can use expire method with -1 value as a timeout:

redisTemplate.expire("mykey", -1);


and the second way is to use persist method on key:

redisTemplate.persist("mykey");


This will remove the expiry time for the &quot;mykey&quot; key, effectively setting it to never expire.

</details>



huangapple
  • 本文由 发表于 2023年4月17日 16:04:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032927.html
匿名

发表评论

匿名网友

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

确定