Redis密钥空间通知到期未触发

时间:2018-07-25 19:22:25

标签: redis

我正在查看此页面: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仅查找“过期”事件,并且在(且仅当)通知通用命令时始终看到它们。这对于我的应用来说实在太闲谈了我只想看看过期。

感谢您的帮助。谢谢!

2 个答案:

答案 0 :(得分:2)

您订阅了错误的频道。

有两种通知:

  • 键空间通知:频道为__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

答案 1 :(得分:0)

您需要订阅“ __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