UNPKG

16.8 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13 return new (P || (P = Promise))(function (resolve, reject) {
14 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
15 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
16 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
17 step((generator = generator.apply(thisArg, _arguments || [])).next());
18 });
19};
20var __generator = (this && this.__generator) || function (thisArg, body) {
21 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
22 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
23 function verb(n) { return function (v) { return step([n, v]); }; }
24 function step(op) {
25 if (f) throw new TypeError("Generator is already executing.");
26 while (_) try {
27 if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
28 if (y = 0, t) op = [0, t.value];
29 switch (op[0]) {
30 case 0: case 1: t = op; break;
31 case 4: _.label++; return { value: op[1], done: false };
32 case 5: _.label++; y = op[1]; op = [0]; continue;
33 case 7: op = _.ops.pop(); _.trys.pop(); continue;
34 default:
35 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
36 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
37 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
38 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
39 if (t[2]) _.ops.pop();
40 _.trys.pop(); continue;
41 }
42 op = body.call(thisArg, _);
43 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
44 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
45 }
46};
47Object.defineProperty(exports, "__esModule", { value: true });
48var redis = require("redis");
49var events_1 = require("events");
50var BBPromise = require("bluebird");
51var transaction_1 = require("./transaction");
52var batch_1 = require("./batch");
53BBPromise.promisifyAll(redis.RedisClient.prototype);
54BBPromise.promisifyAll(redis.Multi.prototype);
55var SCAN_LIMIT = 1000;
56var Redis = (function (_super) {
57 __extends(Redis, _super);
58 /**
59 * Constructor
60 */
61 function Redis(options) {
62 var _this = _super.call(this) || this;
63 _this._prefix = options.prefix || '';
64 _this._client = redis.createClient(options);
65 return _this;
66 }
67 Object.defineProperty(Redis.prototype, "client", {
68 /**
69 * Get the underlying redis client
70 */
71 get: function () {
72 return this._client;
73 },
74 enumerable: true,
75 configurable: true
76 });
77 /**
78 * Close the client
79 */
80 Redis.prototype.dispose = function () {
81 return __awaiter(this, void 0, void 0, function () {
82 return __generator(this, function (_a) {
83 switch (_a.label) {
84 case 0: return [4 /*yield*/, this._client.quitAsync()];
85 case 1:
86 _a.sent();
87 this._client = null;
88 return [2 /*return*/];
89 }
90 });
91 });
92 };
93 Object.defineProperty(Redis, "Transaction", {
94 /**
95 * Return the transaction prototype
96 */
97 get: function () {
98 return transaction_1.default;
99 },
100 enumerable: true,
101 configurable: true
102 });
103 /**
104 * Get a transaction
105 */
106 Redis.prototype.transaction = function () {
107 this._checkDisposed();
108 return new transaction_1.default(this);
109 };
110 /**
111 * Watch a key
112 */
113 Redis.prototype.watch = function (key) {
114 return __awaiter(this, void 0, void 0, function () {
115 return __generator(this, function (_a) {
116 this._checkDisposed();
117 return [2 /*return*/, this._client.watchAsync(key)];
118 });
119 });
120 };
121 /**
122 * Begin a batch of commands
123 */
124 Redis.prototype.batch = function () {
125 this._checkDisposed();
126 return new batch_1.default(this);
127 };
128 /**
129 * Key exists
130 */
131 Redis.prototype.exists = function (key) {
132 return __awaiter(this, void 0, void 0, function () {
133 return __generator(this, function (_a) {
134 this._checkDisposed();
135 return [2 /*return*/, this._client.existsAsync(key)];
136 });
137 });
138 };
139 /**
140 * Get a key value
141 */
142 Redis.prototype.get = function (key) {
143 return __awaiter(this, void 0, void 0, function () {
144 return __generator(this, function (_a) {
145 this._checkDisposed();
146 return [2 /*return*/, this._client.getAsync(key)];
147 });
148 });
149 };
150 /**
151 * Set a key value
152 */
153 Redis.prototype.set = function (key, value, options) {
154 return __awaiter(this, void 0, void 0, function () {
155 return __generator(this, function (_a) {
156 switch (_a.label) {
157 case 0:
158 this._checkDisposed();
159 return [4 /*yield*/, this._client.setAsync(key, value)];
160 case 1:
161 _a.sent();
162 return [2 /*return*/, this._checkExpiry(key, options)];
163 }
164 });
165 });
166 };
167 /**
168 * Increment an integer value
169 */
170 Redis.prototype.incrby = function (key, value, options) {
171 return __awaiter(this, void 0, void 0, function () {
172 return __generator(this, function (_a) {
173 switch (_a.label) {
174 case 0:
175 this._checkDisposed();
176 return [4 /*yield*/, this._client.incrbyAsync(key, value)];
177 case 1:
178 _a.sent();
179 return [2 /*return*/, this._checkExpiry(key, options)];
180 }
181 });
182 });
183 };
184 /**
185 * Increment a float value
186 */
187 Redis.prototype.incrbyfloat = function (key, value, options) {
188 return __awaiter(this, void 0, void 0, function () {
189 return __generator(this, function (_a) {
190 switch (_a.label) {
191 case 0:
192 this._checkDisposed();
193 return [4 /*yield*/, this._client.incrbyfloatAsync(key, value)];
194 case 1:
195 _a.sent();
196 return [2 /*return*/, this._checkExpiry(key, options)];
197 }
198 });
199 });
200 };
201 /**
202 * Increment an integer value in a hash set
203 */
204 Redis.prototype.hincrby = function (key, hashKey, value, options) {
205 return __awaiter(this, void 0, void 0, function () {
206 return __generator(this, function (_a) {
207 switch (_a.label) {
208 case 0:
209 this._checkDisposed();
210 return [4 /*yield*/, this._client.hincrbyAsync(key, hashKey, value)];
211 case 1:
212 _a.sent();
213 return [2 /*return*/, this._checkExpiry(key, options)];
214 }
215 });
216 });
217 };
218 /**
219 * Increment a float value in a hash set
220 */
221 Redis.prototype.hincrbyfloat = function (key, hashKey, value, options) {
222 return __awaiter(this, void 0, void 0, function () {
223 return __generator(this, function (_a) {
224 switch (_a.label) {
225 case 0:
226 this._checkDisposed();
227 return [4 /*yield*/, this._client.hincrbyfloatAsync(key, hashKey, value)];
228 case 1:
229 _a.sent();
230 return [2 /*return*/, this._checkExpiry(key, options)];
231 }
232 });
233 });
234 };
235 /**
236 * Hash map set
237 */
238 Redis.prototype.hmset = function (key, hash, options) {
239 return __awaiter(this, void 0, void 0, function () {
240 return __generator(this, function (_a) {
241 switch (_a.label) {
242 case 0:
243 this._checkDisposed();
244 return [4 /*yield*/, this._client.hmsetAsync(key, hash)];
245 case 1:
246 _a.sent();
247 return [2 /*return*/, this._checkExpiry(key, options)];
248 }
249 });
250 });
251 };
252 /**
253 * Get a hash set
254 */
255 Redis.prototype.hgetall = function (key) {
256 return __awaiter(this, void 0, void 0, function () {
257 return __generator(this, function (_a) {
258 this._checkDisposed();
259 return [2 /*return*/, this._client.hgetallAsync(key)];
260 });
261 });
262 };
263 /**
264 * Delete a key
265 */
266 Redis.prototype.del = function (key) {
267 return __awaiter(this, void 0, void 0, function () {
268 return __generator(this, function (_a) {
269 this._checkDisposed();
270 return [2 /*return*/, this._client.delAsync(key)];
271 });
272 });
273 };
274 /**
275 * Add to a set
276 */
277 Redis.prototype.sadd = function (key, value, options) {
278 return __awaiter(this, void 0, void 0, function () {
279 return __generator(this, function (_a) {
280 switch (_a.label) {
281 case 0:
282 this._checkDisposed();
283 return [4 /*yield*/, this._client.saddAsync(key, value)];
284 case 1:
285 _a.sent();
286 return [2 /*return*/, this._checkExpiry(key, options)];
287 }
288 });
289 });
290 };
291 /**
292 * Get a set
293 */
294 Redis.prototype.smembers = function (key) {
295 return __awaiter(this, void 0, void 0, function () {
296 return __generator(this, function (_a) {
297 this._checkDisposed();
298 return [2 /*return*/, this._client.smembersAsync(key)];
299 });
300 });
301 };
302 /**
303 * Remove from set
304 */
305 Redis.prototype.srem = function (key, value) {
306 return __awaiter(this, void 0, void 0, function () {
307 return __generator(this, function (_a) {
308 this._checkDisposed();
309 return [2 /*return*/, this._client.sremAsync(key, value)];
310 });
311 });
312 };
313 /**
314 * Scan and delete keys
315 */
316 Redis.prototype.scanDel = function (pattern) {
317 return __awaiter(this, void 0, void 0, function () {
318 var _this = this;
319 var self;
320 return __generator(this, function (_a) {
321 this._checkDisposed();
322 self = this;
323 return [2 /*return*/, this.scan(pattern, function (keys) { return __awaiter(_this, void 0, void 0, function () {
324 var _this = this;
325 var keysToDel;
326 return __generator(this, function (_a) {
327 self.emit('info', { message: 'redis#scanDel', data: { keys: keys } });
328 keysToDel = keys.map(function (key) {
329 return key.replace(_this._prefix, '');
330 });
331 return [2 /*return*/, self._client.delAsync(keysToDel)];
332 });
333 }); })];
334 });
335 });
336 };
337 /**
338 * Scan keys
339 */
340 Redis.prototype.scan = function (pattern, delegate) {
341 return __awaiter(this, void 0, void 0, function () {
342 function _scan() {
343 return __awaiter(this, void 0, void 0, function () {
344 function processCycle() {
345 return __awaiter(this, void 0, void 0, function () {
346 return __generator(this, function (_a) {
347 switch (_a.label) {
348 case 0:
349 if (!(cursor !== '0')) return [3 /*break*/, 2];
350 return [4 /*yield*/, _scan()];
351 case 1:
352 _a.sent();
353 _a.label = 2;
354 case 2: return [2 /*return*/];
355 }
356 });
357 });
358 }
359 var res, keys;
360 return __generator(this, function (_a) {
361 switch (_a.label) {
362 case 0: return [4 /*yield*/, self._client.scanAsync(cursor, 'MATCH', resolvedPattern, 'COUNT', SCAN_LIMIT)];
363 case 1:
364 res = _a.sent();
365 cursor = res[0];
366 cycle++;
367 keys = res[1];
368 if (!(keys.length > 0)) return [3 /*break*/, 4];
369 return [4 /*yield*/, delegate(keys)];
370 case 2:
371 _a.sent();
372 self.emit('info', { message: 'redis#scan', data: { cycle: cycle, scanned: SCAN_LIMIT * cycle } });
373 return [4 /*yield*/, processCycle()];
374 case 3:
375 _a.sent();
376 return [3 /*break*/, 6];
377 case 4:
378 self.emit('info', { message: 'redis#scan', data: { cycle: cycle, scanned: SCAN_LIMIT * cycle } });
379 return [4 /*yield*/, processCycle()];
380 case 5:
381 _a.sent();
382 _a.label = 6;
383 case 6: return [2 /*return*/];
384 }
385 });
386 });
387 }
388 var self, resolvedPattern, cursor, cycle;
389 return __generator(this, function (_a) {
390 this._checkDisposed();
391 self = this;
392 resolvedPattern = this._prefix + pattern;
393 cursor = '0';
394 cycle = 0;
395 return [2 /*return*/, _scan()];
396 });
397 });
398 };
399 /**
400 * Get the remaining time of a key
401 */
402 Redis.prototype.ttl = function (key) {
403 return __awaiter(this, void 0, void 0, function () {
404 return __generator(this, function (_a) {
405 this._checkDisposed();
406 return [2 /*return*/, this._client.ttlAsync(key)];
407 });
408 });
409 };
410 /**
411 * Set the expiry in seconds
412 */
413 Redis.prototype._checkExpiry = function (key, options) {
414 return __awaiter(this, void 0, void 0, function () {
415 return __generator(this, function (_a) {
416 if (options && options.expire) {
417 return [2 /*return*/, this._client.expireAsync(key, options.expire)];
418 }
419 return [2 /*return*/];
420 });
421 });
422 };
423 /**
424 * Check to make sure this transaction is still open
425 */
426 Redis.prototype._checkDisposed = function () {
427 if (!this._client) {
428 throw new Error('Client disposed');
429 }
430 };
431 return Redis;
432}(events_1.EventEmitter));
433exports.default = Redis;
434//# sourceMappingURL=client.js.map
\No newline at end of file