UNPKG

@voicenter-team/mysql-dynamic-cluster

Version:

Galera cluster with implementation of dynamic choose mysql server for queries, caching, hashing it and metrics

108 lines 4.17 kB
"use strict"; /** * Created by Bohdan on Jan, 2022 */ 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Logger_1 = __importDefault(require("../utils/Logger")); const crypto_1 = require("crypto"); class Redis { constructor() { this.isReady = false; } init(newRedis, clusterName, redisSettings) { this.redis = newRedis; this.redisSettings = redisSettings; this.redisSettings.keyPrefix = `${clusterName}_${this.redisSettings.keyPrefix}`; this.connectEvents(); Logger_1.default.debug("Initialized Redis"); } clearAll() { var _a; (_a = this.redis) === null || _a === void 0 ? void 0 : _a.keys(this.redisSettings.keyPrefix + "*").then((keys) => { const pipeline = this.redis.pipeline(); keys.forEach((key) => { pipeline.del(key); }); return pipeline.exec(); }); Logger_1.default.debug("Redis cache cleared related to project"); } connectEvents() { var _a; if (!this.redis) return; (_a = this.redis) === null || _a === void 0 ? void 0 : _a.on("ready", () => { this.isReady = true; if (this.redisSettings.clearOnStart) this.clearAll(); Logger_1.default.info("Redis is ready"); }); } isEnabled() { return this.redis && this.isReady; } connect(callback) { var _a; return __awaiter(this, void 0, void 0, function* () { if (this.isEnabled()) return; yield ((_a = this.redis) === null || _a === void 0 ? void 0 : _a.connect(callback)); Logger_1.default.info("Redis connected"); }); } disconnect(reconnect) { var _a; (_a = this.redis) === null || _a === void 0 ? void 0 : _a.disconnect(reconnect); this.isReady = false; Logger_1.default.info("Redis disconnected"); } set(key, value, queryExpire) { return __awaiter(this, void 0, void 0, function* () { if (!this.isEnabled()) return null; try { const expire = queryExpire || this.redisSettings.expire || 100; const expireMode = this.redisSettings.expiryMode || 'EX'; key = this.redisSettings.keyPrefix + this.hash(key); Logger_1.default.debug(`Redis set data by key: ${key}`); return yield this.redis.set(key, value, expireMode, expire); } catch (e) { Logger_1.default.error(e); } }); } get(key) { return __awaiter(this, void 0, void 0, function* () { if (!this.isEnabled()) return null; try { key = this.redisSettings.keyPrefix + this.hash(key); Logger_1.default.debug(`Redis get data by key: ${key}`); return yield this.redis.get(key); } catch (e) { Logger_1.default.error(e); } }); } hash(data) { return (0, crypto_1.createHash)(this.redisSettings.algorithm) .update(data) .digest(this.redisSettings.encoding); } } exports.default = new Redis(); //# sourceMappingURL=Redis.js.map