UNPKG

pdmq

Version:
141 lines (140 loc) 5.2 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 }); exports.RedisService = void 0; class RedisService { constructor(_redisClient) { this._redisClient = _redisClient; } set(key, value) { return new Promise((res, rej) => { var _a; (_a = this._redisClient) === null || _a === void 0 ? void 0 : _a.set(key, value, (error, reply) => { if (error) rej(error); res(reply); }); }); } setWithIndex(key, value, indexName, index = 0) { return new Promise((res, rej) => { this._redisClient.multi().set(key, value).zadd(indexName, index, key).exec((error, reply) => { if (error) rej(error); res(reply); }); }); } zrange(indexName, index, end) { return __awaiter(this, void 0, void 0, function* () { return new Promise((res, rej) => { this._redisClient.zrange(indexName, index, end, (error, reply) => { if (error) return rej(error); res(reply); }); }); }); } zrangeRemoveMember(indexName, key) { return __awaiter(this, void 0, void 0, function* () { return new Promise((res, rej) => { this._redisClient.zrem(indexName, key, (error) => { if (error) return rej(error); res(); }); }); }); } searchKeys(matchWith, options) { return __awaiter(this, void 0, void 0, function* () { const scan = (searchKey, cursor) => { return new Promise((res, rej) => { this._redisClient.scan(cursor, "MATCH", searchKey, (error, reply) => { if (error) return rej(error); else if (!reply) return res(['0', []]); else return res(reply); }); }); }; let cursor = null, reply = [], maxLooping = 1000; while (cursor != '0' && maxLooping > 0) { const [tempCursor, tempReply] = yield scan(matchWith, cursor === null ? '0' : cursor); cursor = tempCursor; reply = reply.concat(tempReply); if ((options === null || options === void 0 ? void 0 : options.count) && reply.length >= options.count) { return reply.slice(0, options.count); } maxLooping--; } return reply; }); } getMulti(keys) { return new Promise((res, rej) => { var _a; (_a = this._redisClient) === null || _a === void 0 ? void 0 : _a.mget(keys, (error, reply) => { if (error) rej(error); res(reply); }); }); } copy(source, destination) { return new Promise((res, rej) => { var _a; (_a = this._redisClient) === null || _a === void 0 ? void 0 : _a.send_command(`COPY ${source} ${destination}`, (error, reply) => { if (error) rej(error); res(reply); }); }); } deleteWithIndex(key, indexName) { return new Promise((res, rej) => { this._redisClient .multi() .del(key) .zrem(indexName, key) .exec((error, reply) => { if (error) rej(error); res(reply); }); }); } deleteKeyInIndex(key, indexName) { return new Promise((res, rej) => { this._redisClient .zrem(indexName, key, (error, reply) => { if (error) rej(error); res(reply); }); }); } delete(keys) { return new Promise((res, rej) => { if (!(keys === null || keys === void 0 ? void 0 : keys.length)) res(1); this._redisClient.del(...keys, (error, reply) => { if (error) rej(error); res(reply); }); }); } } exports.RedisService = RedisService;