@wasserstoff/mangi-tg-bot
Version:
A powerful Telegram Bot SDK with built-in authentication, session management, and database integration
76 lines • 2.42 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisManager = void 0;
const ioredis_1 = __importDefault(require("ioredis"));
const logger_1 = require("../logger");
class RedisManager {
static instance;
redisClient;
sdkLogger;
isDev;
constructor(isDev = false) {
this.isDev = isDev;
this.sdkLogger = (0, logger_1.createSdkLogger)(isDev);
}
static getInstance(isDev = false) {
if (!RedisManager.instance) {
RedisManager.instance = new RedisManager(isDev);
}
return RedisManager.instance;
}
async connect(uri) {
try {
this.redisClient = new ioredis_1.default(uri);
this.redisClient.on("connect", () => {
if (this.isDev) {
this.sdkLogger.info("Redis connected successfully");
}
});
this.redisClient.on("error", (err) => {
if (this.isDev) {
this.sdkLogger.error("Redis connection error:", err);
}
});
this.redisClient.on("close", () => {
if (this.isDev) {
this.sdkLogger.info("Redis connection closed");
}
});
// Test the connection
await this.redisClient.ping();
}
catch (error) {
if (this.isDev) {
this.sdkLogger.error("Error connecting to Redis:", error);
}
throw error;
}
}
async disconnect() {
try {
if (this.redisClient) {
await this.redisClient.quit();
if (this.isDev) {
this.sdkLogger.info("Redis disconnected successfully");
}
}
}
catch (error) {
if (this.isDev) {
this.sdkLogger.error("Error disconnecting from Redis:", error);
}
throw error;
}
}
getClient() {
if (!this.redisClient) {
throw new Error("Redis client not initialized. Call connect(uri) first.");
}
return this.redisClient;
}
}
exports.RedisManager = RedisManager;
//# sourceMappingURL=RedisManager.js.map