UNPKG

@sophons/redis

Version:

🏡 Encapsulate common business methods for Redis

58 lines (57 loc) 2.42 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.SuperRedisCluster = void 0; const method_1 = require("./method"); const common_1 = require("./common"); /** * Here you will use the lazy singleton pattern to create an class instance and wrap the methods for the instance */ class SuperRedisCluster { /** * Singleton constructor */ constructor(nodes, options = null) { this.client = new common_1.BaseRedis.Cluster(nodes, options); } /** * Singleton instantiation */ static init(nodes, options = null) { if (!this.superRedisCluster) this.superRedisCluster = new SuperRedisCluster(nodes, options); return this.superRedisCluster; } /** * A message is published to the specified channel */ publish(options = null) { return __awaiter(this, void 0, void 0, function* () { if (!options || !options.channel || !options.message) return; const { channel, message, setJson } = options; yield this.client.publish(channel, setJson ? JSON.stringify(message) : message); }); } /** * Passing in the function to be executed caches the result of the function's execution according to the configuration information */ cache(next, options) { return method_1.cacheDecorator(this.client, next, options); } /** * Passing in the function to be executed caches the result of the function execution into the hash structure with configuration information */ hashCache(next, options) { return method_1.hashCacheDecorator(this.client, next, options); } } exports.SuperRedisCluster = SuperRedisCluster;