【问题标题】:Redis keyspace notification expiration not firingRedis键空间通知到期未触发
【发布时间】:2020-08-11 21:38:41
【问题描述】:

我在看这个页面:https://redis.io/topics/notifications

我在我的配置文件中设置了以下行:

notify-keyspace-events "Kx"

当我这样做(然后运行我的应用程序最终使某些密钥过期)时,我看不到任何事件:

redis-cli --csv psubscribe '__keyspace*__:*expire*'

但是,当我将配置设置为:

notify-keyspace-events "Kg"

然后运行相同的应用程序和 redis-cli 命令,我确实看到了事件:

"pmessage","__keyspace*__:*expire*","__keyspace@0__:spring:session:wca:sessions:expires:9d73fefc-459d-4bf4-83ef-b8fcbf06b997","expire"
"pmessage","__keyspace*__:*expire*","__keyspace@0__:spring:session:wca:sessions:expires:9d73fefc-459d-4bf4-83ef-b8fcbf06b997","expire"
"pmessage","__keyspace*__:*expire*","__keyspace@0__:spring:session:idp:sessions:expires:52dbe449-4616-41ef-bfa6-1d7a16a89f8a","expire"
"pmessage","__keyspace*__:*expire*","__keyspace@0__:spring:session:idp:sessions:expires:52dbe449-4616-41ef-bfa6-1d7a16a89f8a","expire"
"pmessage","__keyspace*__:*expire*","__keyspace@0__:spring:session:idp:sessions:expires:52dbe449-4616-41ef-bfa6-1d7a16a89f8a","expire"
"pmessage","__keyspace*__:*expire*","__keyspace@0__:spring:session:idp:sessions:expires:52dbe449-4616-41ef-bfa6-1d7a16a89f8a","expire"

我了解,到期不一定会在 TTL 已过时发生。但我不确定这是否解释了我所看到的——我的 redis-cli 只寻找“过期”事件,并且它始终会在我通知通用命令时(且仅当)看到它们。这对我的应用来说太健谈了;我只是想看看过期。

感谢任何帮助。谢谢!

【问题讨论】:

  • 我很好奇当你这样做时会发生什么AK
  • @Thymine IIRC 我今天早些时候尝试过。它有效,但非常健谈。

标签: redis


【解决方案1】:

您订阅了错误的频道。

有两种通知:

  • 键空间通知:频道为__keyspace@<db>__:<key>
  • 按键事件通知:频道为__keyevent@<db>__:<event>

如果您想获得所有过期密钥通知,您有两种选择:

使用键空间通知

  1. 启用键空间通知:config set notify-keyspace-events Kx

  2. 订阅频道:psubscribe __keyspace@*__:*

使用按键事件通知

  1. 启用按键事件通知:config set notify-keyspace-events Ex

  2. 订阅频道:psubscribe __keyevent@*__:expired

【讨论】:

  • 谢谢。我做了“notify-keyspace-events Kx”(根据文档),我什么也没看到。
  • 正如我在答案中提到的,您订阅了错误的频道。你应该psubscribe __keyspace@*__:*
  • 啊。我看到了区别。所以我想我的问题是:当我添加“g”时它起作用了。通用“过期”与“x”捕获的过期不同吗?如果不是,我不明白为什么完全相同的 psubscribe 仅在我添加 g 时才看到“过期”事件。 (即,当我感兴趣的只是过期时,为什么我必须通知所有通用事件?)
【解决方案2】:

您需要订阅“__keyevent@0__:expired”而不是“__keyevent@0__:expire

127.0.0.1:6379> subscribe __keyevent@0__:expired
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "__keyevent@0__:expired"
3) (integer) 1

1) "message"
2) "__keyevent@0__:expired"
3) "mykey"

请参阅此文档。我也面临同样的问题。我尝试了此链接中的解决方案,它有效https://github.com/redis/redis/issues/1855

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    相关资源
    最近更新 更多