Redis keyspace notification expiration not firing

2019-08-18 17:17发布

I am looking at this page: https://redis.io/topics/notifications

I have the following line set in my config file:

notify-keyspace-events "Kx"

When I do this (and then run my application that eventually expires some keys), I see no events:

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

However, when I set my config to this:

notify-keyspace-events "Kg"

And then run the same app and redis-cli command, I do see events:

"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"

I understand that the expirations don't necessarily happen right when the TTL has elapsed. But I'm not sure that explains what I'm seeing -- my redis-cli is looking only for "expire" events, and it consistently sees them when (and only when) I notify generic commands. That's too chatty for my app; I just want to see expires.

Any help is appreciated. Thanks!

标签: redis
1条回答
beautiful°
2楼-- · 2019-08-18 18:07

You subscribed to the wrong channel.

There're two kinds of notification:

  • Key-space notification: the channel is __keyspace@<db>__:<key>
  • Key-event notification: the channel is __keyevent@<db>__:<event>

If you want to get all expired key notification, you have two choices:

use key-space notification

  1. enable key-space notification: config set notify-keyspace-events Kx

  2. subscribe to the channel: psubscribe __keyspace@*__:*

use key-event notification

  1. enable key-event notification: config set notify-keyspace-events Ex

  2. subscribe to the channel: psubscribe __keyevent@*__:expired

查看更多
登录 后发表回答