2025-02-20 17:39:12 -05:00
|
|
|
const fs = require('fs');
|
|
|
|
const ioredis = require('ioredis');
|
2025-04-12 18:46:36 -04:00
|
|
|
const KeyvRedis = require('@keyv/redis').default;
|
2023-12-16 20:45:27 -05:00
|
|
|
const { isEnabled } = require('~/server/utils');
|
2025-02-10 21:56:08 +01:00
|
|
|
const logger = require('~/config/winston');
|
2023-10-11 17:05:47 -04:00
|
|
|
|
2025-02-20 17:39:12 -05:00
|
|
|
const { REDIS_URI, USE_REDIS, USE_REDIS_CLUSTER, REDIS_CA, REDIS_KEY_PREFIX, REDIS_MAX_LISTENERS } =
|
|
|
|
process.env;
|
2023-10-11 17:05:47 -04:00
|
|
|
|
|
|
|
let keyvRedis;
|
2025-02-20 17:39:12 -05:00
|
|
|
const redis_prefix = REDIS_KEY_PREFIX || '';
|
2025-03-21 14:14:45 -04:00
|
|
|
const redis_max_listeners = Number(REDIS_MAX_LISTENERS) || 40;
|
2025-02-20 17:39:12 -05:00
|
|
|
|
|
|
|
function mapURI(uri) {
|
|
|
|
const regex =
|
|
|
|
/^(?:(?<scheme>\w+):\/\/)?(?:(?<user>[^:@]+)(?::(?<password>[^@]+))?@)?(?<host>[\w.-]+)(?::(?<port>\d{1,5}))?$/;
|
|
|
|
const match = uri.match(regex);
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
const { scheme, user, password, host, port } = match.groups;
|
|
|
|
|
|
|
|
return {
|
|
|
|
scheme: scheme || 'none',
|
|
|
|
user: user || null,
|
|
|
|
password: password || null,
|
|
|
|
host: host || null,
|
|
|
|
port: port || null,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
const parts = uri.split(':');
|
|
|
|
if (parts.length === 2) {
|
|
|
|
return {
|
|
|
|
scheme: 'none',
|
|
|
|
user: null,
|
|
|
|
password: null,
|
|
|
|
host: parts[0],
|
|
|
|
port: parts[1],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
scheme: 'none',
|
|
|
|
user: null,
|
|
|
|
password: null,
|
|
|
|
host: uri,
|
|
|
|
port: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2023-10-11 17:05:47 -04:00
|
|
|
|
2023-12-16 20:45:27 -05:00
|
|
|
if (REDIS_URI && isEnabled(USE_REDIS)) {
|
2025-02-20 17:39:12 -05:00
|
|
|
let redisOptions = null;
|
2025-04-12 18:46:36 -04:00
|
|
|
/** @type {import('@keyv/redis').KeyvRedisOptions} */
|
2025-02-20 17:39:12 -05:00
|
|
|
let keyvOpts = {
|
|
|
|
useRedisSets: false,
|
|
|
|
keyPrefix: redis_prefix,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (REDIS_CA) {
|
|
|
|
const ca = fs.readFileSync(REDIS_CA);
|
|
|
|
redisOptions = { tls: { ca } };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isEnabled(USE_REDIS_CLUSTER)) {
|
|
|
|
const hosts = REDIS_URI.split(',').map((item) => {
|
|
|
|
var value = mapURI(item);
|
|
|
|
|
|
|
|
return {
|
|
|
|
host: value.host,
|
|
|
|
port: value.port,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
const cluster = new ioredis.Cluster(hosts, { redisOptions });
|
|
|
|
keyvRedis = new KeyvRedis(cluster, keyvOpts);
|
|
|
|
} else {
|
|
|
|
keyvRedis = new KeyvRedis(REDIS_URI, keyvOpts);
|
|
|
|
}
|
2025-04-12 18:46:36 -04:00
|
|
|
keyvRedis.on('ready', () => {
|
|
|
|
logger.info('KeyvRedis connection ready');
|
|
|
|
});
|
|
|
|
keyvRedis.on('reconnecting', () => {
|
|
|
|
logger.info('KeyvRedis connection reconnecting');
|
|
|
|
});
|
|
|
|
keyvRedis.on('end', () => {
|
|
|
|
logger.info('KeyvRedis connection ended');
|
|
|
|
});
|
|
|
|
keyvRedis.on('close', () => {
|
|
|
|
logger.info('KeyvRedis connection closed');
|
|
|
|
});
|
2023-12-14 07:49:27 -05:00
|
|
|
keyvRedis.on('error', (err) => logger.error('KeyvRedis connection error:', err));
|
2025-02-20 17:39:12 -05:00
|
|
|
keyvRedis.setMaxListeners(redis_max_listeners);
|
2023-12-16 20:45:27 -05:00
|
|
|
logger.info(
|
2025-03-20 22:56:57 -04:00
|
|
|
'[Optional] Redis initialized. If you have issues, or seeing older values, disable it or flush cache to refresh values.',
|
2023-12-16 20:45:27 -05:00
|
|
|
);
|
2024-01-11 11:37:54 -05:00
|
|
|
} else {
|
2025-03-20 22:56:57 -04:00
|
|
|
logger.info('[Optional] Redis not initialized.');
|
2023-10-11 17:05:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = keyvRedis;
|