@herbertgao/surgio
Version:
Generating rules for Surge, Clash, Quantumult like a PRO
63 lines • 2.13 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("@surgio/logger");
const ioredis_1 = __importDefault(require("ioredis"));
const constant_1 = require("./constant");
const logger = (0, logger_1.createLogger)({ service: 'surgio:redis' });
const prepareRedis = () => {
let client = null;
let redisURL = null;
return {
hasRedis: () => !!client,
createRedis(_redisURL, customRedis) {
if (client && redisURL) {
logger.debug('Redis client already created with URL: %s', redisURL);
return client;
}
redisURL = _redisURL;
if (customRedis) {
client = new customRedis(_redisURL);
}
else {
client = new ioredis_1.default(_redisURL.includes('?')
? `${_redisURL}&family=0`
: `${_redisURL}?family=0`);
}
return client;
},
getRedis() {
if (!client) {
throw new Error('Redis client is not initialized');
}
return client;
},
async destroyRedis() {
if (client) {
await client.quit();
client = null;
redisURL = null;
}
},
async cleanCache() {
if (!client) {
return;
}
const keysToRemove = await Promise.all(Object.keys(constant_1.CACHE_KEYS).map((key) => {
if (!client)
return;
return client.keys(`${constant_1.CACHE_KEYS[key]}:*`);
}));
await Promise.all(keysToRemove.map((keys) => {
if (!client || !keys || !keys.length)
return;
return client.del(keys);
}));
},
};
};
const redis = prepareRedis();
exports.default = redis;
//# sourceMappingURL=redis.js.map