Redis訂閱(目前項目用到的是刪除時,發(fā)送訂閱)
配置 notify-keyspace-events 的參數(shù)為 “Ex”(具體為什么是Ex 網(wǎng)上有很多)
-
直接上代碼
var Redis = require("ioredis")var redis=new Redis({ port: 6379, // Redis port host: '127.0.0.1', // Redis host family: 4, // 4 (IPv4) or 6 (IPv6) password: '', db: 5 }) redis.on("pmessage", function (pattern, channel, message) { console.log("("+ pattern +")" + " client received message on " + channel + ": " + message); switch (channel) { // blah blah blah // ... } }); redis.psubscribe('__keyevent@5__:expired',function (err, count) { var ddd=err; var ddddd=count; });
其中有坑的地方有2個
- 監(jiān)聽方法on的第一個參數(shù)監(jiān)聽的頻道必須是
pmessage
-
__keyevent@5__:expired
中的5是redis的DB 表示監(jiān)聽的哪個數(shù)據(jù)庫
以上2個參數(shù) 必須配置正確