2018-02-06 23:31:18 +01:00
|
|
|
const promisify = require('util').promisify;
|
|
|
|
|
|
|
|
module.exports = function(config) {
|
|
|
|
const redis_lib =
|
|
|
|
config.env === 'development' && config.redis_host === 'localhost'
|
|
|
|
? 'redis-mock'
|
|
|
|
: 'redis';
|
|
|
|
|
|
|
|
//eslint-disable-next-line security/detect-non-literal-require
|
|
|
|
const redis = require(redis_lib);
|
2021-04-20 18:37:19 +02:00
|
|
|
|
|
|
|
var client_config = {
|
2018-02-06 23:31:18 +01:00
|
|
|
host: config.redis_host,
|
2021-04-18 05:08:35 +02:00
|
|
|
port: config.redis_port,
|
2019-05-02 22:56:28 +02:00
|
|
|
retry_strategy: options => {
|
2020-04-21 22:30:39 +02:00
|
|
|
if (options.total_retry_time > config.redis_retry_time) {
|
2019-05-02 22:56:28 +02:00
|
|
|
client.emit('error', 'Retry time exhausted');
|
|
|
|
return new Error('Retry time exhausted');
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:30:39 +02:00
|
|
|
return config.redis_retry_delay;
|
2019-05-02 22:56:28 +02:00
|
|
|
}
|
2021-04-20 18:37:19 +02:00
|
|
|
};
|
2021-05-19 11:59:35 +02:00
|
|
|
if (config.redis_user != null && config.redis_user.length > 0)
|
|
|
|
client_config.user = config.redis_user;
|
2021-04-20 18:37:19 +02:00
|
|
|
if (config.redis_password != null && config.redis_password.length > 0)
|
|
|
|
client_config.password = config.redis_password;
|
2021-05-19 11:59:35 +02:00
|
|
|
if (config.redis_db != null && config.redis_db.length > 0)
|
|
|
|
client_config.db = config.redis_db;
|
2021-04-20 18:37:19 +02:00
|
|
|
const client = redis.createClient(client_config);
|
2018-02-06 23:31:18 +01:00
|
|
|
|
|
|
|
client.ttlAsync = promisify(client.ttl);
|
|
|
|
client.hgetallAsync = promisify(client.hgetall);
|
|
|
|
client.hgetAsync = promisify(client.hget);
|
|
|
|
client.pingAsync = promisify(client.ping);
|
|
|
|
return client;
|
|
|
|
};
|