You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
821 B
35 lines
821 B
5 years ago
|
const logger = require('./logger');
|
||
|
const config = require('../config');
|
||
|
const redis = require('redis');
|
||
|
const { promisify } = require('util');
|
||
|
|
||
|
const options = {
|
||
|
host: config.redis.host,
|
||
|
port: config.redis.port,
|
||
|
password: config.redis.password,
|
||
|
};
|
||
|
if (!options.password) {
|
||
|
delete options.password;
|
||
|
}
|
||
|
const client = redis.createClient(options);
|
||
|
|
||
|
client.on('error', (e) => {
|
||
|
logger.error('Redis error: ', e);
|
||
|
});
|
||
|
|
||
|
client.on('connect', () => {
|
||
|
logger.info('Redis connected');
|
||
|
});
|
||
|
|
||
|
const getAsync = promisify(client.get).bind(client);
|
||
|
|
||
|
module.exports = {
|
||
|
set: (key, value, maxAge = 30 * 24 * 60 * 60) => {
|
||
|
logger.info('Set redis: ' + key);
|
||
|
client.setex(key, maxAge, value);
|
||
|
},
|
||
|
get: async (key) => await getAsync(key),
|
||
|
del: (key) => {
|
||
|
client.del(key);
|
||
|
},
|
||
|
};
|