我的 redis 密钥不会过期 [英] my redis keys do not expire

查看:119
本文介绍了我的 redis 密钥不会过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当生存时间达到0时,我的redis服务器不会删除密钥.

My redis server does not delete keys when the time-to-live reaches 0.

这是一个示例代码:

redis-cli
>SET mykey "ismykey"
>EXPIRE mykey 20
#check TTL
>TTL mykey
>(integer) 17
> ...
>TTL mykey
>(integer) -1
#mykey chould have expired:
>EXISTS mykey
>(integer) 1
>#oh still there, check its value
>GET mykey
>"ismykey"

如果我检查 redis 返回的信息,它说 0 个密钥已过期.

If i check the info return by redis, it says 0 keys were expired.

有什么想法吗?

谢谢.

推荐答案

因为你在做一个...",所以很难确定,但我会说你在那个部分设置了 mykey,这将有效去除过期.

Since you're doing a '...' it's hard to say for sure, but I'd say you're setting mykey during that part, which will effectively remove the expiration.

来自 EXPIRE 手册

仅当使用 DEL 删除密钥时才会清除超时命令或使用 SET 或 GETSET 命令覆盖

The timeout is cleared only when the key is removed using the DEL command or overwritten using the SET or GETSET commands

另外,关于 TTL 的 -1 回复

返回值

整数回复:以秒为单位的 TTL 或 -1 时密钥不存在或没有超时.

Integer reply: TTL in seconds or -1 when key does not exist or does not have a timeout.

请注意,此行为在 Redis 2.8 中发生了变化

从 Redis 2.8 开始,发生错误时的返回值发生了变化:
如果密钥不存在,该命令返回 -2.
如果密钥存在但没有关联的过期,则该命令返回 -1.

Starting with Redis 2.8 the return value in case of error changed:
The command returns -2 if the key does not exist.
The command returns -1 if the key exists but has no associated expire.

换句话说,如果您的密钥存在,它似乎是持久的,即没有任何过期设置.

In other words, if your key exists, it would seem to be persistent, ie not have any expiration set.

如果我在 REDIS 从属服务器上创建密钥,我似乎可以重现这一点,从属服务器不会在没有主输入的情况下删除密钥,因为通常您不会在从属服务器上本地创建密钥.这里是这种情况吗?

It seems I can reproduce this if I create the key on a REDIS slave server, the slave will not delete the key without master input, since normally you would not create keys locally on a slave. Is this the case here?

然而,虽然连接到主站的从站不会使密钥过期独立(但会等待来自主人的 DEL),他们仍然会采用存在于数据集,所以当一个从站被选为主站时,它将能够使密钥独立过期,完全充当主人.

However while the slaves connected to a master will not expire keys independently (but will wait for the DEL coming from the master), they'll still take the full state of the expires existing in the dataset, so when a slave is elected to a master it will be able to expire the keys independently, fully acting as a master.

这篇关于我的 redis 密钥不会过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆