gpt4 book ai didi

node.js - 如何在 Redis key 过期时收到通知?

转载 作者:IT王子 更新时间:2023-10-29 06:00:48 24 4
gpt4 key购买 nike

为什么使用以下代码在 key 过期时无法收到通知?

我想使用redis,当key过期时通知我。那我可以做点什么。

var Redis = require('ioredis')
var sub = new Redis()
var pub = new Redis()

var subKey = '__keyevent@0__:del'

sub.subscribe(subKey, function () {
console.log('subscribe success !')
})

sub.on('message', function (channel, message) {
console.log(channel, message, '======')
})

var testKey = 'test'
setTimeout(function () {
pub.multi()
.set(testKey, 'test redis notify')
.expire(testKey, 5)
.exec(function (err) {
if (err) {
console.log(err, 'why err ?')
return
}
console.log('.....')
})
}, 2000)

最佳答案

您需要服务器端对键空间通知的支持。

redis-cli config set notify-keyspace-events KEA

设置完成后,可以运行以下命令查看:

redis-cli config get notify-keyspace-events

如果您收到如下消息:

1) "notify-keyspace-events"
2) "AKE"

然后,您可以运行您的代码并获得您想要的结果。

更进一步,关注timing-of-expired-events .

关于node.js - 如何在 Redis key 过期时收到通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36615540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com