UNPKG

@iredium/butterfly

Version:
97 lines (96 loc) 3.83 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var database_1 = require("./database"); var redis = require("redis"); var connection = null; var Redis = /** @class */ (function (_super) { __extends(Redis, _super); function Redis() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.name = 'redis'; return _this; } Redis.prototype.get = function (key, callback) { var _this = this; if (callback === void 0) { callback = null; } return new Promise(function (resolve, reject) { _this.connection.get(key, function (err, reply) { if (callback) callback(err, reply); if (err) { reject(err); } else { resolve(reply ? reply.toString() : reply); } }); }); }; Redis.prototype.set = function (key, value, expireTimeInMiliseconds, callback) { var _a; if (expireTimeInMiliseconds === void 0) { expireTimeInMiliseconds = null; } if (callback === void 0) { callback = null; } var args = [key, value]; if (expireTimeInMiliseconds) args.push('PX', expireTimeInMiliseconds); if (callback) args.push(callback); (_a = this.connection).set.apply(_a, args); }; Redis.prototype.connect = function () { var config = this.config; if (!connection) { connection = redis.createClient(__assign(__assign({}, config), { retry_strategy: function (options) { if (options.error && options.error.code === 'ECONNREFUSED') { // End reconnecting on a specific error and flush all commands with // a individual error return new Error('The server refused the connection'); } if (options.total_retry_time > 1000 * 60 * 60) { // End reconnecting after a specific timeout and flush all commands // with a individual error return new Error('Retry time exhausted'); } if (options.attempt > 10) { // End reconnecting with built in error return undefined; } // reconnect after return Math.min(options.attempt * 100, 3000); } })); } this.adapter = redis; this.connection = connection; }; Redis.prototype.close = function () { this.connection.quit(); this.connection = null; connection = null; }; return Redis; }(database_1.Database)); exports.Redis = Redis;