Files
ScanCodeLogin/NodeServer/redis/index.js
2024-09-27 01:09:14 +08:00

46 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const redis = require("redis");
const BroadcastChannel = require("../BroadcastChannel");
class Redis {
constructor() {
this.client = redis.createClient({
host:'127.0.0.1',
port:'6379',
ttl: 5*60*1000
});
this.client.on("error", function(error) {
console.error(error);
});
this.client.send_command('config', ['set', 'notify-keyspace-events', 'Ex'], this.SubscribeExpired);
}
async get(key) {
return new Promise((resolve)=>{
this.client.get(key, (err,res) => resolve(res));
})
}
set(key, value="") {
this.client.set(key, value);
// 设置key的过期时间60秒其实也就是二维码的有效时间
this.client.expire(key, 60)
}
SubscribeExpired(e, r) {
let sub = redis.createClient(6379);
const expired_subKey = '__keyevent@0__:expired';
sub.subscribe(expired_subKey, function () {
sub.on('message', function (chan, msg) {
console.log(`二维码:${msg} 已超时过期... `);
// 二维码失效,通过 BroadcastChannel 通知 routes/socket.js
BroadcastChannel.SubscribeExpired.postMessage(msg);
});
})
}
}
module.exports = new Redis();