@ckstack/ck-lib-ras
Version:
ckstack ras/redis library
871 lines • 62.9 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
var _ = __importStar(require("underscore"));
var JSON5 = __importStar(require("json5"));
var redis = __importStar(require("redis"));
var ck_lib_1 = require("@ckstack/ck-lib");
var ck_lib_sequelize_1 = require("@ckstack/ck-lib-sequelize");
var CkRedis = (function () {
function CkRedis() {
}
CkRedis.isValidConnect = function (clientNo, reqIpAndLhdAndHttpInfo) {
var lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
if (CkRedis.REDIS_CLIENT.length <= 0) {
ck_lib_1.CkLogger.errLog2('not connected redis function', lhd);
return ck_lib_1.CkValue.ERROR_TYPE.not_connect_redis;
}
if (clientNo > CkRedis.REDIS_CLIENT.length - 1) {
ck_lib_1.CkLogger.errLog2("invalid redis client num. max num [".concat(CkRedis.REDIS_CLIENT.length, "], request num [").concat(clientNo, "]"), lhd);
return ck_lib_1.CkValue.ERROR_TYPE.failed;
}
return true;
};
CkRedis.init = function (redisInfo) {
CkRedis.REDIS_INFO = redisInfo;
ck_lib_1.CkLogger.infoLog('CkRedis.init', CkRedis.LHD);
};
CkRedis.connect = function () {
if (CkRedis.connected) {
return true;
}
ck_lib_1.CkLogger.infoLog("redis info [".concat(JSON5.stringify(CkRedis.REDIS_INFO), "]"), CkRedis.LHD);
if (CkRedis.REDIS_INFO.length > 0 && !CkRedis.connected) {
for (var _i = 0, _a = CkRedis.REDIS_INFO; _i < _a.length; _i++) {
var element = _a[_i];
if (element.PASSWORD) {
CkRedis.REDIS_CLIENT.push(redis.createClient(element.PORT, element.HOST, { db: element.DB, password: element.PASSWORD }));
ck_lib_1.CkLogger.infoLog("connected auth redis host [".concat(element.HOST, "], port [").concat(element.PORT, "], db num [").concat(element.DB, "]"), CkRedis.LHD);
}
else {
CkRedis.REDIS_CLIENT.push(redis.createClient(element.PORT, element.HOST, { db: element.DB }));
ck_lib_1.CkLogger.infoLog("connected no auth redis host [".concat(element.HOST, "], port [").concat(element.PORT, "], db num [").concat(element.DB, "]"), CkRedis.LHD);
}
}
CkRedis.connected = true;
return true;
}
return false;
};
CkRedis.doCommand = function (clientNo, cmd, params, reqIpAndLhdAndHttpInfo, lang) {
return new Promise(function (resolve, reject) {
var lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
var valid = CkRedis.isValidConnect(clientNo, lhd);
if (_.isString(valid)) {
reject(new ck_lib_1.CkError(lhd, valid, 500, undefined, lang));
return;
}
var startTm = Date.now();
CkRedis.REDIS_CLIENT[clientNo].send_command(cmd, params, function (err, res) {
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "doCommand, clientNo [".concat(clientNo, "], cmd [").concat(cmd, "], params [").concat(JSON5.stringify(params), "]"));
if (err) {
ck_lib_1.CkLogger.errLog2("failed redis send_command. clientNo [".concat(clientNo, "], cmd [").concat(cmd, "], params [").concat(params, "], err [").concat(err.toString(), "]"), lhd);
reject(new ck_lib_1.CkError(lhd, ck_lib_1.CkValue.ERROR_TYPE.redis_error, 500, undefined, lang));
return;
}
ck_lib_1.CkLogger.debugLog2("redis send_command. clientNo [".concat(clientNo, "], cmd [").concat(cmd, "], params [").concat(params, "]"), lhd);
resolve(res);
});
});
};
CkRedis.remove = function (clientNo, key, reqIpAndLhdAndHttpInfo, lang) {
return new Promise(function (resolve, reject) {
var lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
var valid = CkRedis.isValidConnect(clientNo, lhd);
if (_.isString(valid)) {
reject(new ck_lib_1.CkError(lhd, valid, 500, undefined, lang));
return;
}
var startTm = Date.now();
CkRedis.REDIS_CLIENT[clientNo].del(key, function (err, res) {
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "remove, clientNo [".concat(clientNo, "], key [").concat(key, "]"));
if (err) {
ck_lib_1.CkLogger.errLog2("failed redis del. clientNo [".concat(clientNo, "], key [").concat(key, "], err [").concat(err.toString(), "]"), lhd);
reject(new ck_lib_1.CkError(lhd, ck_lib_1.CkValue.ERROR_TYPE.redis_error, 500, undefined, lang));
return;
}
ck_lib_1.CkLogger.debugLog2("redis del. clientNo [".concat(clientNo, "], key [").concat(key, "]"), lhd);
resolve(res);
});
});
};
CkRedis.removeByPattern = function (clientNo, key, reqIpAndLhdAndHttpInfo, lang) {
var _a, e_1, _b, _c, _d, e_2, _e, _f;
return __awaiter(this, void 0, void 0, function () {
var lhd, loopMaxRange, cursor, count, startTm, _g, loopMaxRange_1, loopMaxRange_1_1, i, result, _h, _j, _k, j, e_2_1, err_1, e_1_1;
return __generator(this, function (_l) {
switch (_l.label) {
case 0:
lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
loopMaxRange = _.range(0, 1000);
cursor = 0;
count = 0;
startTm = Date.now();
_l.label = 1;
case 1:
_l.trys.push([1, 28, 29, 34]);
_g = true, loopMaxRange_1 = __asyncValues(loopMaxRange);
_l.label = 2;
case 2: return [4, loopMaxRange_1.next()];
case 3:
if (!(loopMaxRange_1_1 = _l.sent(), _a = loopMaxRange_1_1.done, !_a)) return [3, 27];
_c = loopMaxRange_1_1.value;
_g = false;
_l.label = 4;
case 4:
_l.trys.push([4, , 25, 26]);
i = _c;
_l.label = 5;
case 5:
_l.trys.push([5, 23, , 24]);
return [4, CkRedis.doCommand(clientNo, 'scan', [cursor, 'match', key, 'count', 500], lhd, lang)];
case 6:
result = _l.sent();
if (result.length <= 0) {
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "removeByPattern[1], clientNo [".concat(clientNo, "], key [").concat(key, "]"));
ck_lib_1.CkLogger.errLog2('scan result is invalid. redis data removeByPattern', lhd);
return [2, count];
}
cursor = Number(result[0]);
if (!(_.isArray(result[1]) && result[1].length > 0)) return [3, 22];
_l.label = 7;
case 7:
_l.trys.push([7, 15, 16, 21]);
_h = true, _j = (e_2 = void 0, __asyncValues(_.range(0, result[1].length)));
_l.label = 8;
case 8: return [4, _j.next()];
case 9:
if (!(_k = _l.sent(), _d = _k.done, !_d)) return [3, 14];
_f = _k.value;
_h = false;
_l.label = 10;
case 10:
_l.trys.push([10, , 12, 13]);
j = _f;
return [4, CkRedis.remove(clientNo, result[1][j], lhd, lang)];
case 11:
_l.sent();
return [3, 13];
case 12:
_h = true;
return [7];
case 13: return [3, 8];
case 14: return [3, 21];
case 15:
e_2_1 = _l.sent();
e_2 = { error: e_2_1 };
return [3, 21];
case 16:
_l.trys.push([16, , 19, 20]);
if (!(!_h && !_d && (_e = _j.return))) return [3, 18];
return [4, _e.call(_j)];
case 17:
_l.sent();
_l.label = 18;
case 18: return [3, 20];
case 19:
if (e_2) throw e_2.error;
return [7];
case 20: return [7];
case 21:
count += result[1].length;
_l.label = 22;
case 22:
if (cursor === 0) {
return [3, 27];
}
return [3, 24];
case 23:
err_1 = _l.sent();
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "removeByPattern[2], clientNo [".concat(clientNo, "], key [").concat(key, "]"));
ck_lib_1.CkLogger.errLog2("failed redis data removeByPattern. err [".concat(err_1.toString(), "]"), lhd);
throw err_1;
case 24: return [3, 26];
case 25:
_g = true;
return [7];
case 26: return [3, 2];
case 27: return [3, 34];
case 28:
e_1_1 = _l.sent();
e_1 = { error: e_1_1 };
return [3, 34];
case 29:
_l.trys.push([29, , 32, 33]);
if (!(!_g && !_a && (_b = loopMaxRange_1.return))) return [3, 31];
return [4, _b.call(loopMaxRange_1)];
case 30:
_l.sent();
_l.label = 31;
case 31: return [3, 33];
case 32:
if (e_1) throw e_1.error;
return [7];
case 33: return [7];
case 34:
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "removeByPattern[3], clientNo [".concat(clientNo, "], key [").concat(key, "]"));
return [2, count];
}
});
});
};
CkRedis.rPush = function (clientNo, appId, eventType, userData, reqIpAndLhdAndHttpInfo, lang) {
return __awaiter(this, void 0, void 0, function () {
var lhd, now, ctime, data, startTm, ret, err_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
now = _.now();
ctime = Math.floor(now / 1000);
data = {
app_id: appId,
pid: 'ES',
et: eventType,
tid: "".concat(now),
ctime: ctime,
ud: userData,
};
startTm = Date.now();
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4, CkRedis.doCommand(clientNo, 'rpush', ['queue.event', JSON.stringify(data)], lhd, lang)];
case 2:
ret = _a.sent();
ck_lib_1.CkLogger.debugLog2("redis rpush. data [".concat(JSON5.stringify(data), "], result [").concat(ret, "]"), lhd);
return [3, 4];
case 3:
err_2 = _a.sent();
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "rPush[1], clientNo [".concat(clientNo, "], appId [").concat(appId, "], eventType [").concat(eventType, "], userData [").concat(JSON5.stringify(userData), "]"));
ck_lib_1.CkLogger.errLog2("redis rpush. data [".concat(JSON5.stringify(data), "], err [").concat(err_2.toString(), "]"), lhd);
throw err_2;
case 4:
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "rPush[2], clientNo [".concat(clientNo, "], appId [").concat(appId, "], eventType [").concat(eventType, "], userData [").concat(JSON5.stringify(userData), "]"));
return [2, ret];
}
});
});
};
CkRedis.upsertHash = function (clientNo, key, value, reqIpAndLhdAndHttpInfo, lang) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2, new Promise(function (resolve, reject) {
var lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
var valid = CkRedis.isValidConnect(clientNo, lhd);
if (_.isString(valid)) {
reject(new ck_lib_1.CkError(lhd, valid, 500, undefined, lang));
return;
}
var startTm = Date.now();
CkRedis.REDIS_CLIENT[clientNo].hmset(key, value, function (err, res) {
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "upsertHash, clientNo [".concat(clientNo, "], key [").concat(key, "], value [").concat(JSON5.stringify(value), "]"));
if (err) {
ck_lib_1.CkLogger.errLog2("failed redis hmset. clientNo [".concat(clientNo, "], key [").concat(key, "], value [").concat(JSON5.stringify(value), "], err [").concat(err.toString(), "]"), lhd);
reject(new ck_lib_1.CkError(lhd, ck_lib_1.CkValue.ERROR_TYPE.redis_error, 500, undefined, lang));
return;
}
ck_lib_1.CkLogger.debugLog2("redis hmset. clientNo [".concat(clientNo, "], key [").concat(key, "], value [").concat(JSON5.stringify(value), "]"), lhd);
resolve(res);
});
})];
});
});
};
CkRedis.upsertValue = function (clientNo, key, value, ttl, reqIpAndLhdAndHttpInfo, lang) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2, new Promise(function (resolve, reject) {
var lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
var valid = CkRedis.isValidConnect(clientNo, lhd);
if (_.isString(valid)) {
reject(new ck_lib_1.CkError(lhd, valid, 500, undefined, lang));
return;
}
var _value = value;
if (_.isObject(_value)) {
_value = JSON5.stringify(_value);
}
var startTm = Date.now();
if (ttl && ttl > 0) {
CkRedis.REDIS_CLIENT[clientNo].set(key, _value, 'EX', ttl, function (err, res) {
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "upsertValue[set-ex], clientNo [".concat(clientNo, "], key [").concat(key, "], value [").concat(JSON5.stringify(value), "], ttl [").concat(ttl, "]"));
if (err) {
ck_lib_1.CkLogger.errLog2("failed redis set. clientNo [".concat(clientNo, "], key [").concat(key, "], value [").concat(_value, "], ttl [").concat(ttl, " sec], err [").concat(err.toString(), "]"), lhd);
reject(new ck_lib_1.CkError(lhd, ck_lib_1.CkValue.ERROR_TYPE.redis_error, 500, undefined, lang));
return;
}
ck_lib_1.CkLogger.debugLog2("redis set. clientNo [".concat(clientNo, "], key [").concat(key, "], value [").concat(_value, "], ttl [").concat(ttl, " sec]"), lhd);
resolve(res);
});
return;
}
CkRedis.REDIS_CLIENT[clientNo].set(key, _value, function (err, res) {
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "upsertValue[set], clientNo [".concat(clientNo, "], key [").concat(key, "], value [").concat(JSON5.stringify(value), "], ttl [").concat(ttl, "]"));
if (err) {
ck_lib_1.CkLogger.errLog2("failed redis set. clientNo [".concat(clientNo, "], key [").concat(key, "], value [").concat(_value, "]. err [").concat(err.toString(), "]"), lhd);
reject(new ck_lib_1.CkError(lhd, ck_lib_1.CkValue.ERROR_TYPE.redis_error, 500, undefined, lang));
return;
}
ck_lib_1.CkLogger.debugLog2("redis set. clientNo [".concat(clientNo, "], key [").concat(key, "], value [").concat(_value, "]"), lhd);
resolve(res);
});
})];
});
});
};
CkRedis.getHashValue = function (clientNo, hashKey, reqIpAndLhdAndHttpInfo, lang) {
return __awaiter(this, void 0, void 0, function () {
var lhd, startTm, result, err_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
startTm = Date.now();
result = null;
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4, CkRedis.doCommand(clientNo, 'hgetall', [hashKey], lhd, lang)];
case 2:
result = _a.sent();
return [3, 4];
case 3:
err_3 = _a.sent();
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getHashValue-err, clientNo [".concat(clientNo, "], hashKey [").concat(hashKey, "]"));
ck_lib_1.CkLogger.errLog2("failed get hash value. err [".concat(err_3.toString(), "]"), lhd);
throw err_3;
case 4:
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getHashValue, clientNo [".concat(clientNo, "], hashKey [").concat(hashKey, "]"));
return [2, result];
}
});
});
};
CkRedis.getHashValues = function (clientNo, hashKey, rowKey, isJSON, reqIpAndLhdAndHttpInfo, lang) {
var _a, e_3, _b, _c;
return __awaiter(this, void 0, void 0, function () {
var lhd, loopMaxRange, cursor, datas, startTm, _loop_1, _d, loopMaxRange_2, loopMaxRange_2_1, state_1, e_3_1;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
loopMaxRange = _.range(0, 1000);
cursor = 0;
datas = [];
startTm = Date.now();
_e.label = 1;
case 1:
_e.trys.push([1, 7, 8, 13]);
_loop_1 = function () {
var i, params, result, rows_1, err_4;
return __generator(this, function (_f) {
switch (_f.label) {
case 0:
_c = loopMaxRange_2_1.value;
_d = false;
_f.label = 1;
case 1:
_f.trys.push([1, , 6, 7]);
i = _c;
_f.label = 2;
case 2:
_f.trys.push([2, 4, , 5]);
params = [hashKey, cursor];
if (rowKey) {
params.push('match');
params.push(rowKey);
}
params.push('count');
params.push(500);
return [4, CkRedis.doCommand(clientNo, 'hscan', params, lhd, lang)];
case 3:
result = _f.sent();
if (result.length <= 0) {
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getHashValues-err[1], clientNo [".concat(clientNo, "], hashKey [").concat(hashKey, "], rowKey [").concat(rowKey, "], isJSON [").concat(isJSON, "]"));
ck_lib_1.CkLogger.errLog2('hscan result is invalid on getHashValues', lhd);
return [2, { value: datas }];
}
cursor = Number(result[0]);
rows_1 = result[1];
if ((rows_1 === undefined || rows_1 === null || Number.isNaN(rows_1))
|| !_.isArray(rows_1)
|| rows_1.length < 2
|| rows_1.length % 2 !== 0) {
if (cursor !== 0) {
return [2, "continue"];
}
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getHashValues[1], clientNo [".concat(clientNo, "], hashKey [").concat(hashKey, "], rowKey [").concat(rowKey, "], isJSON [").concat(isJSON, "]"));
return [2, { value: datas }];
}
_.each(rows_1, function (row, idx) {
if (idx % 2 !== 0) {
return;
}
var data = {
key: row,
value: {},
};
if (isJSON) {
try {
data.value = JSON5.parse(rows_1[idx + 1]);
}
catch (err) {
data.value = {};
}
}
else {
data.value = rows_1[idx + 1];
}
datas.push(data);
});
if (cursor === 0) {
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getHashValues[2], clientNo [".concat(clientNo, "], hashKey [").concat(hashKey, "], rowKey [").concat(rowKey, "], isJSON [").concat(isJSON, "]"));
return [2, { value: datas }];
}
return [3, 5];
case 4:
err_4 = _f.sent();
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getHashValues-err[2], clientNo [".concat(clientNo, "], hashKey [").concat(hashKey, "], rowKey [").concat(rowKey, "], isJSON [").concat(isJSON, "]"));
ck_lib_1.CkLogger.errLog2("failed get hash values. err [".concat(err_4.toString(), "]"), lhd);
throw err_4;
case 5: return [3, 7];
case 6:
_d = true;
return [7];
case 7: return [2];
}
});
};
_d = true, loopMaxRange_2 = __asyncValues(loopMaxRange);
_e.label = 2;
case 2: return [4, loopMaxRange_2.next()];
case 3:
if (!(loopMaxRange_2_1 = _e.sent(), _a = loopMaxRange_2_1.done, !_a)) return [3, 6];
return [5, _loop_1()];
case 4:
state_1 = _e.sent();
if (typeof state_1 === "object")
return [2, state_1.value];
_e.label = 5;
case 5: return [3, 2];
case 6: return [3, 13];
case 7:
e_3_1 = _e.sent();
e_3 = { error: e_3_1 };
return [3, 13];
case 8:
_e.trys.push([8, , 11, 12]);
if (!(!_d && !_a && (_b = loopMaxRange_2.return))) return [3, 10];
return [4, _b.call(loopMaxRange_2)];
case 9:
_e.sent();
_e.label = 10;
case 10: return [3, 12];
case 11:
if (e_3) throw e_3.error;
return [7];
case 12: return [7];
case 13:
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getHashValues-null, clientNo [".concat(clientNo, "], hashKey [").concat(hashKey, "], rowKey [").concat(rowKey, "], isJSON [").concat(isJSON, "]"));
return [2, null];
}
});
});
};
CkRedis.countValue = function (clientNo, key, reqIpAndLhdAndHttpInfo, lang) {
var _a, e_4, _b, _c;
return __awaiter(this, void 0, void 0, function () {
var lhd, loopMaxRange, cursor, count, startTm, _d, loopMaxRange_3, loopMaxRange_3_1, i, result, err_5, e_4_1;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
loopMaxRange = _.range(0, 1000);
cursor = 0;
count = 0;
startTm = Date.now();
_e.label = 1;
case 1:
_e.trys.push([1, 12, 13, 18]);
_d = true, loopMaxRange_3 = __asyncValues(loopMaxRange);
_e.label = 2;
case 2: return [4, loopMaxRange_3.next()];
case 3:
if (!(loopMaxRange_3_1 = _e.sent(), _a = loopMaxRange_3_1.done, !_a)) return [3, 11];
_c = loopMaxRange_3_1.value;
_d = false;
_e.label = 4;
case 4:
_e.trys.push([4, , 9, 10]);
i = _c;
_e.label = 5;
case 5:
_e.trys.push([5, 7, , 8]);
return [4, CkRedis.doCommand(clientNo, 'scan', [cursor, 'match', key, 'count', 500], lhd, lang)];
case 6:
result = _e.sent();
if (result.length <= 0) {
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "countValue-warn, clientNo [".concat(clientNo, "], key [").concat(key, "]"));
ck_lib_1.CkLogger.warnLog2('scan result is invalid on countValue', lhd);
return [2, count];
}
cursor = Number(result[0]);
if (_.isArray(result[1])) {
count += result[1].length;
}
if (cursor === 0) {
return [3, 11];
}
return [3, 8];
case 7:
err_5 = _e.sent();
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "countValue-err, clientNo [".concat(clientNo, "], key [").concat(key, "]"));
ck_lib_1.CkLogger.errLog2("failed count value. err [".concat(err_5, "]"), lhd);
throw err_5;
case 8: return [3, 10];
case 9:
_d = true;
return [7];
case 10: return [3, 2];
case 11: return [3, 18];
case 12:
e_4_1 = _e.sent();
e_4 = { error: e_4_1 };
return [3, 18];
case 13:
_e.trys.push([13, , 16, 17]);
if (!(!_d && !_a && (_b = loopMaxRange_3.return))) return [3, 15];
return [4, _b.call(loopMaxRange_3)];
case 14:
_e.sent();
_e.label = 15;
case 15: return [3, 17];
case 16:
if (e_4) throw e_4.error;
return [7];
case 17: return [7];
case 18:
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "countValue, clientNo [".concat(clientNo, "], key [").concat(key, "]"));
return [2, count];
}
});
});
};
CkRedis.getValue = function (clientNo, key, isJSON, reqIpAndLhdAndHttpInfo, lang) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2, new Promise(function (resolve, reject) {
var lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
var valid = CkRedis.isValidConnect(clientNo, lhd);
if (_.isString(valid)) {
reject(new ck_lib_1.CkError(lhd, valid, 500, undefined, lang));
return;
}
var startTm = Date.now();
CkRedis.REDIS_CLIENT[clientNo].get(key, function (err, res) {
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getValue, clientNo [".concat(clientNo, "], key [").concat(key, "], isJSON [").concat(isJSON, "]"));
if (err) {
ck_lib_1.CkLogger.errLog2("failed redis getValue. clientNo [".concat(clientNo, "], key [").concat(key, "], err [").concat(err.toString(), "]"), lhd);
reject(new ck_lib_1.CkError(lhd, ck_lib_1.CkValue.ERROR_TYPE.redis_error, 500, undefined, lang));
return;
}
ck_lib_1.CkLogger.debugLog2("redis getValue. clientNo [".concat(clientNo, "], key [").concat(key, "]"), lhd);
if (!res) {
resolve(res);
return;
}
if (isJSON) {
try {
var _res = JSON5.parse(res);
resolve(_res);
}
catch (err2) {
ck_lib_1.CkLogger.warnLog2("redis getValue. value is not json. clientNo [".concat(clientNo, "], key [").concat(key, "], value [").concat(res, "]"), lhd);
resolve(null);
}
return;
}
resolve(res);
});
})];
});
});
};
CkRedis.getValueOnlyOne = function (clientNo, key, isJSON, isDeleteOther, reqIpAndLhdAndHttpInfo, lang) {
var _a, e_5, _b, _c, _d, e_6, _e, _f;
return __awaiter(this, void 0, void 0, function () {
var lhd, loopMaxRange, cursor, allRowKeys, startTm, _g, loopMaxRange_4, loopMaxRange_4_1, i, result, err_6, e_5_1, selectedKey, removeKeys, _h, removeKeys_1, removeKeys_1_1, rkey, err_7, e_6_1, selctedVal, err_8;
return __generator(this, function (_j) {
switch (_j.label) {
case 0:
lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
loopMaxRange = _.range(0, 1000);
cursor = 0;
allRowKeys = [];
startTm = Date.now();
_j.label = 1;
case 1:
_j.trys.push([1, 12, 13, 18]);
_g = true, loopMaxRange_4 = __asyncValues(loopMaxRange);
_j.label = 2;
case 2: return [4, loopMaxRange_4.next()];
case 3:
if (!(loopMaxRange_4_1 = _j.sent(), _a = loopMaxRange_4_1.done, !_a)) return [3, 11];
_c = loopMaxRange_4_1.value;
_g = false;
_j.label = 4;
case 4:
_j.trys.push([4, , 9, 10]);
i = _c;
_j.label = 5;
case 5:
_j.trys.push([5, 7, , 8]);
return [4, CkRedis.doCommand(clientNo, 'scan', [cursor, 'match', key, 'count', 500], lhd, lang)];
case 6:
result = _j.sent();
if (result.length <= 0) {
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getValueOnlyOne-warn, clientNo [".concat(clientNo, "], key [").concat(key, "], isJSON [").concat(isJSON, "], isDeleteOther [").concat(isDeleteOther, "]"));
ck_lib_1.CkLogger.warnLog2('scan result is invalid on getValueOnlyOne', lhd);
return [2, null];
}
cursor = Number(result[0]);
if (result[1] !== undefined && result[1] !== null && !Number.isNaN(result[1])
&& _.isArray(result[1])
&& result[1].length > 0) {
allRowKeys.push(result[1]);
}
if (cursor === 0) {
return [3, 11];
}
return [3, 8];
case 7:
err_6 = _j.sent();
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getValueOnlyOne-err[1], clientNo [".concat(clientNo, "], key [").concat(key, "], isJSON [").concat(isJSON, "], isDeleteOther [").concat(isDeleteOther, "]"));
ck_lib_1.CkLogger.errLog2("failed get value one. err [".concat(err_6.toString(), "]"), lhd);
throw err_6;
case 8: return [3, 10];
case 9:
_g = true;
return [7];
case 10: return [3, 2];
case 11: return [3, 18];
case 12:
e_5_1 = _j.sent();
e_5 = { error: e_5_1 };
return [3, 18];
case 13:
_j.trys.push([13, , 16, 17]);
if (!(!_g && !_a && (_b = loopMaxRange_4.return))) return [3, 15];
return [4, _b.call(loopMaxRange_4)];
case 14:
_j.sent();
_j.label = 15;
case 15: return [3, 17];
case 16:
if (e_5) throw e_5.error;
return [7];
case 17: return [7];
case 18:
allRowKeys = _.flatten(allRowKeys);
if (allRowKeys.length <= 0) {
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getValueOnlyOne[1], clientNo [".concat(clientNo, "], key [").concat(key, "], isJSON [").concat(isJSON, "], isDeleteOther [").concat(isDeleteOther, "]"));
return [2, null];
}
selectedKey = allRowKeys[0];
removeKeys = [];
if (isDeleteOther) {
removeKeys = allRowKeys.slice(1);
}
_j.label = 19;
case 19:
_j.trys.push([19, 30, 31, 36]);
_h = true, removeKeys_1 = __asyncValues(removeKeys);
_j.label = 20;
case 20: return [4, removeKeys_1.next()];
case 21:
if (!(removeKeys_1_1 = _j.sent(), _d = removeKeys_1_1.done, !_d)) return [3, 29];
_f = removeKeys_1_1.value;
_h = false;
_j.label = 22;
case 22:
_j.trys.push([22, , 27, 28]);
rkey = _f;
_j.label = 23;
case 23:
_j.trys.push([23, 25, , 26]);
return [4, CkRedis.remove(clientNo, rkey, lhd, lang)];
case 24:
_j.sent();
return [3, 26];
case 25:
err_7 = _j.sent();
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getValueOnlyOne-err[2], clientNo [".concat(clientNo, "], key [").concat(key, "], isJSON [").concat(isJSON, "], isDeleteOther [").concat(isDeleteOther, "]"));
ck_lib_1.CkLogger.errLog2("failed remove on getValueOnlyOne. err [".concat(err_7.toString(), "]"), lhd);
throw new ck_lib_1.CkError(lhd, ck_lib_1.CkValue.ERROR_TYPE.redis_error, 500, undefined, lang);
case 26: return [3, 28];
case 27:
_h = true;
return [7];
case 28: return [3, 20];
case 29: return [3, 36];
case 30:
e_6_1 = _j.sent();
e_6 = { error: e_6_1 };
return [3, 36];
case 31:
_j.trys.push([31, , 34, 35]);
if (!(!_h && !_d && (_e = removeKeys_1.return))) return [3, 33];
return [4, _e.call(removeKeys_1)];
case 32:
_j.sent();
_j.label = 33;
case 33: return [3, 35];
case 34:
if (e_6) throw e_6.error;
return [7];
case 35: return [7];
case 36:
selctedVal = null;
_j.label = 37;
case 37:
_j.trys.push([37, 39, , 40]);
return [4, CkRedis.getValue(clientNo, selectedKey, isJSON, lhd, lang)];
case 38:
selctedVal = _j.sent();
return [3, 40];
case 39:
err_8 = _j.sent();
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getValueOnlyOne-err[3], clientNo [".concat(clientNo, "], key [").concat(key, "], isJSON [").concat(isJSON, "], isDeleteOther [").concat(isDeleteOther, "]"));
ck_lib_1.CkLogger.errLog2("failed get value on getValueOnlyOne. err [".concat(err_8.toString(), "]"), lhd);
throw err_8;
case 40:
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getValueOnlyOne[2], clientNo [".concat(clientNo, "], key [").concat(key, "], isJSON [").concat(isJSON, "], isDeleteOther [").concat(isDeleteOther, "]"));
return [2, {
key: selectedKey,
value: selctedVal,
}];
}
});
});
};
CkRedis.setExpire = function (clientNo, key, sec, reqIpAndLhdAndHttpInfo, lang) {
return __awaiter(this, void 0, void 0, function () {
var lhd, ret, startTm, err_9;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
startTm = Date.now();
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4, CkRedis.doCommand(clientNo, 'expire', [key, sec], lhd, lang)];
case 2:
ret = _a.sent();
return [3, 4];
case 3:
err_9 = _a.sent();
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "setExpire-err, clientNo [".concat(clientNo, "], key [").concat(key, "], sec [").concat(sec, "]"));
ck_lib_1.CkLogger.errLog2("failed expire on setExpire. err [".concat(err_9.toString(), "]"), lhd);
throw err_9;
case 4:
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "setExpire, clientNo [".concat(clientNo, "], key [").concat(key, "], sec [").concat(sec, "]"));
return [2, ret];
}
});
});
};
CkRedis.getTTL = function (clientNo, key, reqIpAndLhdAndHttpInfo, lang) {
return __awaiter(this, void 0, void 0, function () {
var lhd, ret, startTm, err_10;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
lhd = reqIpAndLhdAndHttpInfo || ck_lib_1.CkValue.getDefaultLogKey(true);
startTm = Date.now();
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4, CkRedis.doCommand(clientNo, 'ttl', [key], lhd, lang)];
case 2:
ret = _a.sent();
return [3, 4];
case 3:
err_10 = _a.sent();
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getTTL-err, clientNo [".concat(clientNo, "], key [").concat(key, "]"));
ck_lib_1.CkLogger.errLog2("failed get ttl on getTTL. err [".concat(err_10.toString(), "]"), lhd);
throw err_10;
case 4:
ck_lib_1.CkLogger.printElapsedRedisLog2(lhd, startTm, "getTTL, clientNo [".concat(clientNo, "], key [").concat(key, "]"));
return [2, ret];
}
});
});
};
CkRedis.setEvent = function (client