UNPKG

@joshbetz/memcached

Version:

Memcached client for modern Node JS

124 lines (123 loc) 4.43 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const generic_pool_1 = require("generic-pool"); const events_1 = require("events"); const memcached_1 = require("./memcached"); class Pool extends events_1.EventEmitter { constructor(port, host, opts) { super(); this.opts = Object.assign({ // Pool options max: 10, min: 2, acquireTimeoutMillis: 200, destroyTimeoutMillis: 200, maxWaitingClients: 2, idleTimeoutMillis: 30000, // Connection options socketTimeout: 100, }, opts); this.opts.autostart = true; this.opts.fifo = true; this.opts.evictionRunIntervalMillis = 0; this.pool = (0, generic_pool_1.createPool)({ create: () => __awaiter(this, void 0, void 0, function* () { const memcached = new memcached_1.default(port, host, this.opts); memcached.on('error', (error) => { if (this.listeners('error').length) { this.emit('error', error); } }); yield memcached.ready(); return memcached; }), destroy: (memcached) => __awaiter(this, void 0, void 0, function* () { memcached.removeAllListeners(); return memcached.end(); }), }, this.opts); } ready() { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => { const isReady = () => { if (this.pool.available >= this.pool.min) { resolve(true); } else { setTimeout(isReady, 100).unref(); } }; isReady(); }); }); } use(fn) { return __awaiter(this, void 0, void 0, function* () { let value; try { value = yield this.pool.use(fn); } catch (error) { return false; } return value; }); } flush() { return __awaiter(this, void 0, void 0, function* () { return this.use((client) => client.flush()); }); } set(key, value, ttl = 0) { return __awaiter(this, void 0, void 0, function* () { return this.use((client) => client.set(key, value, ttl)); }); } add(key, value, ttl = 0) { return __awaiter(this, void 0, void 0, function* () { return this.use((client) => client.add(key, value, ttl)); }); } get(key) { return __awaiter(this, void 0, void 0, function* () { return this.use((client) => client.get(key)); }); } del(key) { return __awaiter(this, void 0, void 0, function* () { return this.use((client) => client.del(key)); }); } incr(key, value = 1) { return __awaiter(this, void 0, void 0, function* () { return this.use((client) => client.incr(key, value)); }); } decr(key, value = 1) { return __awaiter(this, void 0, void 0, function* () { return this.use((client) => client.decr(key, value)); }); } ping() { return __awaiter(this, void 0, void 0, function* () { return this.use((client) => client.ping()); }); } end() { return __awaiter(this, void 0, void 0, function* () { yield this.pool.drain(); yield this.pool.clear(); }); } } exports.default = Pool;