字段和值的 Redis 键空间通知订阅

Redis Keyspace Notifications subscription for field&value

我目前正在处理 Redis 过期事件

我的目标:在Redis中的数据已经过期后,获取值,字段在下一个过程中做一些事情

所以我找到了 Redis 键空间通知功能 允许客户端订阅 Redis 中的频道以接收影响重置数据的事件,如 Expire

所以我有一些示例代码:https://github.com/toygame/nodejs-redis-keyspace-notifications

subscriber.subscribe("__keyevent@0__:expired")
subscriber.on('message', async(channel, message) => {
        // do somethings
        console.log(message);       
    })

Result : Key0

这项工作找到了,但我得到的结果只是我设置到 redis 中并过期的密钥

我已经做了一些研究 https://medium.com/nerd-for-tech/redis-getting-notified-when-a-key-is-expired-or-changed-ca3e1f1c7f0a

但它只找到了事件,也许我可以得到结果,但不是我期望的值,字段

无论如何,他们是否会获得那些价值和领域?

仅供参考。文档 https://redis.io/topics/notifications

更新 根据这个

Keyspace notifications do not report the value, only the key's name and/or command performed are included in the published message.The main underlaying reasoning for this is that Redis values can become quite large.

If you really really really need this kind of behavior, well that's pretty easy actually. Because keyspace notifications are using Pub/Sub messages, you can just call PUBLISH yourself after each relevant operation, and with the information that you're interested in.

看起来我不能使用这个 Redis 键空间,但我必须自己发布它

您可以使用RedisGears处理键空间通知并获取键和值。

您可以在python中编写您的处理代码并注册到Redis中。 例如捕获每个键空间事件并存储到 Stream

GearsBuilder() \
.foreach(lambda x: execute('XADD', "notifications-stream", '*', *sum([[k,v] for k,v in x.items()],[]))) \
.register(prefix="person:*", eventTypes=['hset', 'hmset'], mode='sync')

您可以在此处阅读有关此示例的更多信息:https://oss.redis.com/redisgears/examples.html#reliable-keyspace-notification