2016-04-14 86 views
3

为什么在使用以下代码过期时无法得到通知?如何在redis密钥过期时收到通知?

我想使用redis,并且当某个密钥过期时,通知我。然后我可以做点什么。

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

var subKey = '[email protected]__: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) 
+0

var subKey ='__keyevent @ 0 __:expired' – AsherTan

+0

欢迎来到Stack Overflow!我编辑了您的问题以删除任何非建设性内容。请编辑以提供您的代码示例正在做什么。祝你好运! – Wtower

回答

5

您需要服务器端的密钥空间通知支持。

redis-cli config set notify-keyspace-events KEA 

集后,你可能会遇到波纹管的命令检查:

redis-cli config get notify-keyspace-events 

如果你得到的消息,如:

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

然后,您可以运行你的代码,并得到你想要的。请注意0​​。

+0

谢谢!我已经明白了。 – AsherTan