"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());
    });
};
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 });
exports.RedisClient = void 0;
var Redis = require("redis");
var promisify = require("util").promisify;
var commands = require('redis-commands');
/**
 * Redis操作工具库
 * @date 2020-10-29 09:17:19
 * @author 吴锋霖
 * @doc https://github.com/NodeRedis/node-redis
 *      https://www.runoob.com/redis/redis-tutorial.html
 */
var RedisClient = /** @class */ (function () {
    /**
     * 创建Redis客户端
     * @param host 127.0.0.1
     * @param port  6379
     * @param password 可选
     * @param database 可选 默认0
     */
    function RedisClient(host, port, password, database) {
        if (password === void 0) { password = ''; }
        if (database === void 0) { database = 0; }
        this.redisCommands = {};
        var options = {
            host: host,
            port: port,
            no_ready_check: true
        };
        this.client = Redis.createClient(options);
        if (password && password !== "") {
            this.client.auth(password, function () {
            });
        }
        if (database && database > 0) {
            this.client.select(database, function (err, data) {
                if (err) {
                    console.log("redis select db error: %s", err);
                }
            });
        }
        this.client.on("error", function (err) {
            console.log("redis[%s:%s/][db:%s][pwd:%s]连接失败: %s", host, port, database, password, err);
        });
        this.client.on("connect", function () {
            console.log("redis[%s:%s]连接成功!", host, port);
        });
        //当前版本Redis不支持promise，用 util.promisify 转换
        for (var _i = 0, _a = commands.list; _i < _a.length; _i++) {
            var command = _a[_i];
            this.redisCommands[command] = promisify(this.client[command]).bind(this.client);
        }
    }
    /**
     * SET
     * @param key key
     * @param value value
     * @param expires 过期时间(秒),可选参数，默认不过期
     */
    RedisClient.prototype.set = function (key, value, expires) {
        if (expires === void 0) { expires = 0; }
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!(expires > 0)) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.redisCommands['set'](key, value, "EX", expires)];
                    case 1: return [2 /*return*/, _a.sent()];
                    case 2: return [4 /*yield*/, this.redisCommands['set'](key, value)];
                    case 3: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * setNX
     * @param key key
     * @param value value
     */
    RedisClient.prototype.setNX = function (key, value, expires) {
        if (expires === void 0) { expires = 0; }
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!(expires > 0)) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.redisCommands['set'](key, value, "EX", expires, 'NX')];
                    case 1: return [2 /*return*/, _a.sent()];
                    case 2: return [4 /*yield*/, this.redisCommands['setnx'](key, value)];
                    case 3: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 获取
     * @param key key
     */
    RedisClient.prototype.get = function (key) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['get'](key)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 删除
     * @param key key
     */
    RedisClient.prototype.delete = function (key) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['del'](key)];
                    case 1: return [2 /*return*/, (_a.sent()) === 1];
                }
            });
        });
    };
    /**
     * 判断Key是否存在
     * @param key key
     */
    RedisClient.prototype.exists = function (key) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['exists'](key)];
                    case 1: return [2 /*return*/, (_a.sent()) === 1];
                }
            });
        });
    };
    /**
     * 用于查找所有符合给定模式 pattern 的 key
     * @param pattern
     */
    RedisClient.prototype.keys = function (pattern) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['keys'](pattern)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 设置过期时间
     * @param key key
     * @param expires 过期时间(秒)
     */
    RedisClient.prototype.expire = function (key, expires) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['expire'](key, expires)];
                    case 1: return [2 /*return*/, (_a.sent()) === 1];
                }
            });
        });
    };
    /**
     * 将一个或多个值插入到列表头部
     * @param key key
     * @param value 值
     */
    RedisClient.prototype.lpush = function (key, value) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['lpush'](key, value)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 获取列表长度
     * @param key key
     */
    RedisClient.prototype.llen = function (key) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['llen'](key)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 将一个值插入到已存在的列表头部
     * @param key key
     * @param value 值
     */
    RedisClient.prototype.lpushx = function (key, value) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['lpushx'](key, value)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 获取列表指定范围内的元素
     * @param key key
     * @param start 开始索引
     * @param stop 截止索引
     */
    RedisClient.prototype.lrange = function (key, start, stop) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['lrange'](key, start, stop)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 移出并获取列表的第一个元素
     * @param key key
     */
    RedisClient.prototype.lpop = function (key) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['lpop'](key)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 根据参数 COUNT 的值，移除列表中与参数 VALUE 相等的元素。
     * @param key key
     * @param count
     *  count > 0 : 从表头开始向表尾搜索，移除与 VALUE 相等的元素，数量为 COUNT 。
     *  count < 0 : 从表尾开始向表头搜索，移除与 VALUE 相等的元素，数量为 COUNT 的绝对值。
     *  count = 0 : 移除表中所有与 VALUE 相等的值。
     * @param value
     */
    RedisClient.prototype.lrem = function (key, count, value) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['lrem'](key, count, value)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 向集合添加一个或多个成员
     * @param key
     * @param value
     */
    RedisClient.prototype.sadd = function (key, value) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['sadd'](key, value)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 获取集合的成员数
     * @param key
     */
    RedisClient.prototype.scard = function (key) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['scard'](key)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 返回第一个集合与其他集合之间的差异。
     * @param key1
     * @param key2
     */
    RedisClient.prototype.sdiff = function (key1, key2) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['sdiff'](key1, key2)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 判断 member 元素是否是集合 key 的成员
     * @param key
     * @param member
     */
    RedisClient.prototype.sismember = function (key, member) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['sismember'](key, member)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 返回集合中的所有成员
     * @param key
     */
    RedisClient.prototype.smembers = function (key) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['smembers'](key)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 移除并返回集合中的一个随机元素
     * @param key
     */
    RedisClient.prototype.spop = function (key) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['spop'](key)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 返回集合中一个或多个随机数
     * @param key
     * @param count
     */
    RedisClient.prototype.srandmember = function (key, count) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['srandmember'](key, count)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 移除集合中一个或多个成员
     * @param key
     * @param member
     */
    RedisClient.prototype.srem = function (key, member) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['srem'](key, member)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 返回所有给定集合的并集
     * @param key
     */
    RedisClient.prototype.sunion = function (key) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['sunion'](key)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 获取存储在哈希表中指定字段的值
     * @param key
     * @param field 字段名称
     */
    RedisClient.prototype.hget = function (key, field) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['hget'](key, field)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 获取在哈希表中指定 key 的所有字段和值
     * @param key
     */
    RedisClient.prototype.hgetall = function (key) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['hgetall'](key)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 为哈希表 key 中的指定字段的整数值加上增量 increment
     * @param key
     * @param field
     * @param increment
     */
    RedisClient.prototype.hincrby = function (key, field, increment) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['hincrby'](key, field, increment)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 将哈希表 key 中的字段 field 的值设为 value 。
     * @param key
     * @param field
     * @param value
     */
    RedisClient.prototype.hset = function (key, field, value) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['hset'](key, field, value)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 同时将多个 field-value (域-值)对设置到哈希表 key 中。
     * @param key
     * @param ...args [field1,value1,field2,value2 ]
     */
    RedisClient.prototype.hmset = function (key) {
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['hmset'](key, args)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 删除存储在哈希表中指定字段的值
     * @param key
     * @param ...args [field1,value1,field2,value2 ]
     */
    RedisClient.prototype.hdel = function (key) {
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands['hdel'](key, args)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    /**
     * 执行某个Redis指令
     * @param command 指令名称,例： set
     * @param ...args 指令参数,例： [key value 'EX' expires ]
     */
    RedisClient.prototype.exec = function (command) {
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.redisCommands[command](args)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    return RedisClient;
}());
exports.RedisClient = RedisClient;
//# sourceMappingURL=RedisClient.js.map