32 lines
851 B
JavaScript
32 lines
851 B
JavaScript
const Redis = require('ioredis');
|
|
const logger = require('./logger');
|
|
|
|
const REDIS_HOST = process.env.REDIS_HOST || '127.0.0.1';
|
|
const REDIS_PORT = parseInt(process.env.REDIS_PORT || '6379', 10);
|
|
const REDIS_PASSWORD = process.env.REDIS_PASSWORD || '';
|
|
const REDIS_EQ_DB = parseInt(process.env.REDIS_EQ_DB || '1', 10);
|
|
|
|
let eqCacheClient = null;
|
|
|
|
function getEqCacheRedis() {
|
|
if (!eqCacheClient) {
|
|
const options = {
|
|
host: REDIS_HOST,
|
|
port: REDIS_PORT,
|
|
db: REDIS_EQ_DB,
|
|
maxRetriesPerRequest: 2,
|
|
connectTimeout: 10000,
|
|
};
|
|
if (REDIS_PASSWORD) {
|
|
options.password = REDIS_PASSWORD;
|
|
}
|
|
eqCacheClient = new Redis(options);
|
|
eqCacheClient.on('error', (err) => {
|
|
logger.error(`Redis EQ cache error: ${err.message}`);
|
|
});
|
|
}
|
|
return eqCacheClient;
|
|
}
|
|
|
|
module.exports = { getEqCacheRedis };
|