UNPKG

rankmycache

Version:

An easy-to-use cache providing service.

183 lines 6.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IORedisAdapter = void 0; const Bluebird = require("bluebird"); const ioredis_1 = require("ioredis"); class IORedisAdapter { constructor({ host, port, password, keyPrefix = '', requestTimeout = 150, ttl = false, }) { this.settings = { host, port, password, keyPrefix, ttl, }; this.client = this.getInstance(); ioredis_1.default.Promise = Bluebird; this.timeout = requestTimeout; } getInstance() { try { const instance = new ioredis_1.default(Object.assign(Object.assign({}, this.settings), { retryStrategy: () => null, reconnectOnError: () => false, connectTimeout: 200, keepAlive: 1000 })); instance.on('error', (error) => this.handleError(error)); instance.on('close', () => this.disconnect()); return instance; } catch (_a) { return null; } } async get(key) { var _a; try { if (!this.client) { this.client = this.getInstance(); } if (((_a = this.client) === null || _a === void 0 ? void 0 : _a.status) !== 'ready') { return null; } const redisPromise = this.client.get(key).timeout(this.timeout, 'ERR_TIMEOUT'); const data = await redisPromise; const parsedData = JSON.parse(data); return parsedData; } catch (err) { return this.handleError(err); } } async set(key, data, ttl = this.settings.ttl) { var _a; try { if (!this.client) { this.client = this.getInstance(); } if (((_a = this.client) === null || _a === void 0 ? void 0 : _a.status) !== 'ready') { return null; } const encodedData = JSON.stringify(data); const redisPromise = (ttl ? this.client.set(key, encodedData, 'EX', ttl) : this.client.set(key, encodedData)).timeout(this.timeout, 'ERR_TIMEOUT'); await redisPromise; return null; } catch (err) { return this.handleError(err); } } async delete(key) { var _a; try { if (!this.client) { this.client = this.getInstance(); } if (((_a = this.client) === null || _a === void 0 ? void 0 : _a.status) !== 'ready') { return null; } const redisPromise = this.client.del(key).timeout(this.timeout, 'ERR_TIMEOUT'); await redisPromise; return null; } catch (error) { return this.handleError(error); } } disconnect() { this.client = null; } handleError(err) { if (err.message !== 'ERR_TIMEOUT') { this.disconnect(); } return null; } async getSetMembers(key) { var _a; try { if (!this.client) { this.client = this.getInstance(); } if (((_a = this.client) === null || _a === void 0 ? void 0 : _a.status) !== 'ready') { return null; } const redisPromise = this.client.smembers(key).timeout(this.timeout, 'ERR_TIMEOUT'); const data = await redisPromise; return data.length > 0 ? data : null; } catch (err) { return this.handleError(err); } } async addToSet(key, value) { var _a; try { if (!this.client) { this.client = this.getInstance(); } if (((_a = this.client) === null || _a === void 0 ? void 0 : _a.status) !== 'ready') { return null; } const valuesToAdd = Array.isArray(value) ? value : [value]; const redisPromise = this.client.sadd(key, valuesToAdd).timeout(this.timeout, 'ERR_TIMEOUT'); await redisPromise; return null; } catch (err) { return this.handleError(err); } } async removeFromSet(key, value) { var _a; try { if (!this.client) { this.client = this.getInstance(); } if (((_a = this.client) === null || _a === void 0 ? void 0 : _a.status) !== 'ready') { return null; } const valuesToRemove = Array.isArray(value) ? value : [value]; const redisPromise = this.client.srem(key, valuesToRemove).timeout(this.timeout, 'ERR_TIMEOUT'); await redisPromise; return null; } catch (error) { return this.handleError(error); } } async isSetMember(key, value) { var _a; try { if (!this.client) { this.client = this.getInstance(); } if (((_a = this.client) === null || _a === void 0 ? void 0 : _a.status) !== 'ready') { return null; } const redisPromise = this.client.sismember(key, String(value)).timeout(this.timeout, 'ERR_TIMEOUT'); const data = await redisPromise; return Boolean(data); } catch (err) { return this.handleError(err); } } async expire(key, ttl) { var _a; try { if (!this.client) { this.client = this.getInstance(); } if (((_a = this.client) === null || _a === void 0 ? void 0 : _a.status) !== 'ready') { return null; } const redisPromise = this.client.expire(key, ttl).timeout(this.timeout, 'ERR_TIMEOUT'); await redisPromise; return null; } catch (err) { return this.handleError(err); } } } exports.IORedisAdapter = IORedisAdapter; //# sourceMappingURL=ioredis-adapter.js.map