"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const redis = require("redis"); const loggers_1 = require("./loggers"); const logger = loggers_1.getLogger('redis'); class default_1 { constructor(opt) { this.chatAsString = (chat) => `${chat.chatType}:${chat.chatID.toString()}`; this.cacheContent = (contentId, content) => new Promise((resolve, reject) => this.client.set(`content/${contentId}`, content, 'EX', 3600 * 24, (err, res) => err ? reject(err) : resolve(res))).then(res => { logger.debug(`cached content ${contentId}, result: ${res}`); }).catch((err) => { logger.error(`failed to cache content ${contentId}, error: ${err}`); }); this.cacheForChat = (postId, target) => { const targetStr = this.chatAsString(target); return new Promise((resolve, reject) => this.client.set(`sent/${targetStr}/${postId}`, 'true', 'EX', this.expireAfter, (err, res) => err ? reject(err) : resolve(res))).then(res => { logger.debug(`cached post ${postId} for ${targetStr}, result: ${res}`); }).catch((err) => { logger.error(`failed to cache post ${postId} for ${targetStr}, error: ${err}`); }); }; this.getContent = (contentId) => new Promise((resolve, reject) => this.client.get(`content/${contentId}`, (err, res) => err ? reject(err) : resolve(res))).then(res => { logger.debug(`retrieved cached content ${contentId}, result: ${res}`); return res; }).catch((err) => { logger.error(`failed to retrieve cached content ${contentId}, error: ${err}`); throw err; }); this.isCachedForChat = (postId, target) => { const targetStr = this.chatAsString(target); return new Promise((resolve, reject) => this.client.exists(`sent/${targetStr}/${postId}`, (err, res) => err ? reject(err) : resolve(res))).then(res => { logger.debug(`retrieved status of post ${postId} for ${targetStr}, result: ${res}`); return Boolean(res); }).catch((err) => { logger.error(`failed to retrieve status of post ${postId} for ${targetStr}, error: ${err}`); return false; }); }; this.client = redis.createClient({ host: opt.redisHost, port: opt.redisPort, }); this.expireAfter = opt.redisExpireTime; logger.info(`loaded redis service at ${opt.redisHost}:${opt.redisPort}`); } } exports.default = default_1;