@hastehaul/common 2.0.43 → 2.0.45

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ import IORedis from 'ioredis';
2
+ export declare class RedisClient {
3
+ private master;
4
+ private replicas;
5
+ private _redisClient?;
6
+ private static instance;
7
+ private constructor();
8
+ static getInstance(masterHost: string, replicaHosts: string[]): RedisClient;
9
+ executeMasterOperation(command: string, ...args: any[]): Promise<any>;
10
+ executeReplicaOperation(command: string, ...args: any[]): Promise<any>;
11
+ executeOperation(client: IORedis, command: string, ...args: any[]): Promise<any>;
12
+ set(key: string, value: string): Promise<any>;
13
+ get(key: string): Promise<any>;
14
+ del(key: string): Promise<any>;
15
+ setex(key: string, value: string, seconds: number): Promise<any>;
16
+ quit(): void;
17
+ }
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.RedisClient = void 0;
36
+ const ioredis_1 = __importStar(require("ioredis"));
37
+ class RedisClient {
38
+ constructor(masterHost, replicaHosts) {
39
+ this.master = new ioredis_1.default({ host: masterHost, port: 6379 });
40
+ this.replicas = replicaHosts.map(replicaHost => new ioredis_1.default({ host: replicaHost, port: 6379 }));
41
+ }
42
+ // get client(): IORedis {
43
+ // if(!this._redisClient) throw new Error("Cannot access Redis Client before connecting")
44
+ // return this._redisClient;
45
+ // }
46
+ static getInstance(masterHost, replicaHosts) {
47
+ if (!RedisClient.instance) {
48
+ RedisClient.instance = new RedisClient(masterHost, replicaHosts);
49
+ }
50
+ return RedisClient.instance;
51
+ }
52
+ executeMasterOperation(command, ...args) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ return this.executeOperation(this.master, command, ...args);
55
+ });
56
+ }
57
+ executeReplicaOperation(command, ...args) {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ const replica = this.replicas[Math.floor(Math.random() * this.replicas.length)];
60
+ return this.executeOperation(replica, command, ...args);
61
+ });
62
+ }
63
+ executeOperation(client, command, ...args) {
64
+ return __awaiter(this, void 0, void 0, function* () {
65
+ return client.sendCommand(new ioredis_1.Command(command, args));
66
+ });
67
+ }
68
+ set(key, value) {
69
+ return this.executeMasterOperation('SET', key, value);
70
+ }
71
+ get(key) {
72
+ return this.executeReplicaOperation('GET', key);
73
+ }
74
+ del(key) {
75
+ return this.executeMasterOperation('DEL', key);
76
+ }
77
+ setex(key, value, seconds) {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ return this.executeMasterOperation('SETEX', key, seconds, value);
80
+ });
81
+ }
82
+ quit() {
83
+ this.master.quit();
84
+ this.replicas.forEach(replica => replica.quit());
85
+ }
86
+ }
87
+ exports.RedisClient = RedisClient;
88
+ RedisClient.instance = null;
package/build/index.d.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  * It also includes error handling and connection management features.
6
6
  */
7
7
  export * from "./connections-wrappers/redis-connection-wrapper";
8
+ export * from "./connections-wrappers/redis-wrapper";
8
9
  /**
9
10
  * Re-exports all the contents from the "socket-connection-wrapper" module.
10
11
  *
package/build/index.js CHANGED
@@ -22,6 +22,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
22
22
  * It also includes error handling and connection management features.
23
23
  */
24
24
  __exportStar(require("./connections-wrappers/redis-connection-wrapper"), exports);
25
+ __exportStar(require("./connections-wrappers/redis-wrapper"), exports);
25
26
  /**
26
27
  * Re-exports all the contents from the "socket-connection-wrapper" module.
27
28
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hastehaul/common",
3
- "version": "2.0.43",
3
+ "version": "2.0.45",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",