|
|
|
@ -2,11 +2,32 @@ |
|
|
|
|
|
|
|
|
|
const Redis = require('ioredis') |
|
|
|
|
, configs = require(__dirname+'/configs/main.js') |
|
|
|
|
, client = new Redis(configs.redis); |
|
|
|
|
, client = new Redis(configs.redis) |
|
|
|
|
, publisher = new Redis(configs.redis); |
|
|
|
|
|
|
|
|
|
client.subscribe('config', (err, count) => { |
|
|
|
|
if (err) { |
|
|
|
|
return console.error(err); |
|
|
|
|
} |
|
|
|
|
console.log(`Redis subscribed to ${count} channels`); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
client.on("message", (channel, message) => { |
|
|
|
|
switch (channel) { |
|
|
|
|
case 'config': |
|
|
|
|
//TODO: something, change the configs import to a new config handler class/module
|
|
|
|
|
void 0; |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
console.error(`Unhandled pubsub channel ${channel} message: ${message}`); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
|
|
|
|
|
|
|
redisClient: client, |
|
|
|
|
redisPublisher: publisher, |
|
|
|
|
|
|
|
|
|
//get a value with key
|
|
|
|
|
get: (key) => { |
|
|
|
@ -16,15 +37,15 @@ module.exports = { |
|
|
|
|
//set a value on key
|
|
|
|
|
set: (key, value, ttl) => { |
|
|
|
|
if (ttl) { |
|
|
|
|
client.set(key, JSON.stringify(value), 'EX', ttl); |
|
|
|
|
return client.set(key, JSON.stringify(value), 'EX', ttl); |
|
|
|
|
} else { |
|
|
|
|
client.set(key, JSON.stringify(value)); |
|
|
|
|
return client.set(key, JSON.stringify(value)); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
//set a value on key if not exist
|
|
|
|
|
setnx: (key, value) => { |
|
|
|
|
client.setnx(key, JSON.stringify(value)); |
|
|
|
|
return client.setnx(key, JSON.stringify(value)); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
//add items to a set
|
|
|
|
|