UNPKG

memcached-node

Version:
443 lines (442 loc) 22.1 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); }; 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var events_1 = require("events"); var connection_1 = require("./connection"); var error_1 = require("./error"); var defaultOptions = __assign(__assign({}, connection_1.defaultConnectionOptions), { initSize: 1, poolSize: 10, wait: false, waitTimeout: 3000 }); var Memcached = /** @class */ (function (_super) { __extends(Memcached, _super); function Memcached(args, options) { var _this = _super.call(this) || this; _this._connectionId = 0; _this._connectionPool = []; _this._connectionConfig = args; _this._options = __assign(__assign({}, defaultOptions), options); for (var i = 0; i < _this._options.initSize; i++) { var connectionOptions = options; var connection = new connection_1.Connection(args, ++_this._connectionId, connectionOptions); connection.on("close", function (connection) { return _this.emit("close", connection); }); connection.on("drop", function (connection, server) { return _this.emit("drop", connection, server); }); _this._connectionPool.push(connection); } return _this; } Memcached.prototype.clean = function () { this._connectionPool.forEach(function (connection) { connection.close(true); }); }; Memcached.prototype.createPool = function () { return __awaiter(this, void 0, void 0, function () { var connTry; return __generator(this, function (_a) { switch (_a.label) { case 0: connTry = this._connectionPool.map(function (connection) { return connection.connect(connection_1.Status.IDLE); }); return [4 /*yield*/, Promise.all(connTry)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Memcached.prototype.getConnection = function () { if (this._connectionPool.length === 0) { var connection = new connection_1.Connection(this._connectionConfig, ++this._connectionId); this._connectionPool.push(connection); return connection; } var idleConnections = this._getIdleConnections(); if (idleConnections.length > 0) { var connection = idleConnections[0]; connection.status = connection_1.Status.RESEARVED; return idleConnections[0]; } throw new error_1.ConnectionError("no connection available", error_1.ConnectionErrorCode.ER_CONN_NO_AVAILABLE); }; Memcached.prototype._getIdleConnections = function () { return this._connectionPool.filter(function (connection) { return connection.isIdle(); }); }; Memcached.prototype._cmd = function (fn) { return __awaiter(this, void 0, void 0, function () { var connection, resp; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this._options.wait) return [3 /*break*/, 2]; return [4 /*yield*/, this._getConnection()]; case 1: connection = _a.sent(); return [3 /*break*/, 3]; case 2: connection = this.getConnection(); _a.label = 3; case 3: return [4 /*yield*/, fn(connection)]; case 4: resp = _a.sent(); connection.close(); return [2 /*return*/, resp]; } }); }); }; Memcached.prototype._getConnection = function () { return __awaiter(this, void 0, void 0, function () { var err_1, connErr, _a; var _this = this; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 1, , 5]); return [2 /*return*/, this.getConnection()]; case 1: err_1 = _b.sent(); connErr = err_1; _a = connErr.code; switch (_a) { case error_1.ConnectionErrorCode.ER_CONN_NO_AVAILABLE: return [3 /*break*/, 2]; } return [3 /*break*/, 4]; case 2: return [4 /*yield*/, new Promise(function (resolve, reject) { _this._connectionPool.forEach(function (conn) { var timeout = setTimeout(function () { reject(new error_1.ConnectionError("timout to get connection", error_1.ConnectionErrorCode.ER_CONN_TIMEOUT)); }, _this._options.waitTimeout); conn.once("changeStatus", function (id) { var connection = _this._connectionPool.find(function (conn) { return conn.id === id; }); // this shouldn't happen if (!connection) { throw err_1; } clearTimeout(timeout); connection.status = connection_1.Status.RESEARVED; resolve(connection); }); }); })]; case 3: _b.sent(); return [3 /*break*/, 4]; case 4: throw connErr; case 5: return [2 /*return*/]; } }); }); }; Memcached.prototype.createConnection = function () { return __awaiter(this, void 0, void 0, function () { var connectionOptions, connection; var _this = this; return __generator(this, function (_a) { if (this._connectionPool.length >= this._options.poolSize) { throw new error_1.ConnectionError("connection reached the pool size", error_1.ConnectionErrorCode.ER_CONN_MAX_CONNECTION); } connectionOptions = this._options; connection = new connection_1.Connection(this._connectionConfig, ++this._connectionId, connectionOptions); connection.on("close", function (connection) { return _this._handleClose(connection); }); connection.on("drop", function (connection, server) { return _this._handleDrop(connection, server); }); this._connectionPool.push(connection); if (this._connectionPool.length === this._options.poolSize) { this.emit("maxConnection", this._options.poolSize); } return [2 /*return*/, connection]; }); }); }; Memcached.prototype._handleClose = function (removedConnection) { this._connectionPool = this._connectionPool.filter(function (connection) { return connection.id !== removedConnection.id; }); this.emit("close", removedConnection); }; Memcached.prototype._handleDrop = function (belongingConnection, server) { this.emit("drop", belongingConnection, server); }; Memcached.prototype.get = function (keys, options) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._cmd(function (connection) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, connection.get(keys, options)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Memcached.prototype.gets = function (keys, options) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._cmd(function (connection) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, connection.gets(keys, options)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Memcached.prototype.set = function (key, value, options) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._cmd(function (connection) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, connection.set(key, value, options)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Memcached.prototype.add = function (key, value, options) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._cmd(function (connection) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, connection.add(key, value, options)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Memcached.prototype.append = function (key, value, options) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._cmd(function (connection) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, connection.append(key, value, options)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Memcached.prototype.prepend = function (key, value, options) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._cmd(function (connection) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, connection.prepend(key, value, options)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Memcached.prototype.replace = function (key, value, options) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._cmd(function (connection) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, connection.replace(key, value, options)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Memcached.prototype.cas = function (key, value, casId, options) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._cmd(function (connection) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, connection.cas(key, value, casId, options)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Memcached.prototype.delete = function (key) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._cmd(function (connection) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, connection.delete(key)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Memcached.prototype.gat = function (keys, expire, options) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._cmd(function (connection) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, connection.gat(keys, expire, options)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Memcached.prototype.gats = function (keys, expire, options) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._cmd(function (connection) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, connection.gats(keys, expire, options)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Memcached.prototype.touch = function (key, expires) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._cmd(function (connection) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, connection.touch(key, expires)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Memcached.prototype.stats = function () { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._cmd(function (connection) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, connection.stats()]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; return Memcached; }(events_1.EventEmitter)); exports.Memcached = Memcached; function createPool(args, options) { return new Memcached(args, options); } exports.createPool = createPool;