@hastehaul/common 2.0.49 → 2.0.51
Sign up to get free protection for your applications and to get access to all the features.
@@ -5,13 +5,10 @@ export declare class RedisClient {
|
|
5
5
|
get masterClient(): IORedis;
|
6
6
|
get relicaClients(): IORedis[];
|
7
7
|
connect(masterHost: string, replicaHosts: string[]): Promise<void>;
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
get(key: string): Promise<any>;
|
13
|
-
del(key: string): Promise<any>;
|
14
|
-
setex(key: string, value: string, seconds: number): Promise<any>;
|
8
|
+
set(key: string, value: any): Promise<'OK'>;
|
9
|
+
get(key: string): Promise<string | null>;
|
10
|
+
del(key: string): Promise<number>;
|
11
|
+
setex(key: string, seconds: number, value: string): Promise<'OK'>;
|
15
12
|
quit(): void;
|
16
13
|
}
|
17
14
|
export declare const rWrapper: RedisClient;
|
@@ -1,27 +1,4 @@
|
|
1
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
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
26
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
27
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
@@ -31,9 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
31
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
32
9
|
});
|
33
10
|
};
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
|
+
};
|
34
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
35
15
|
exports.rWrapper = exports.RedisClient = void 0;
|
36
|
-
const ioredis_1 =
|
16
|
+
const ioredis_1 = __importDefault(require("ioredis"));
|
37
17
|
class RedisClient {
|
38
18
|
get masterClient() {
|
39
19
|
if (!this.master)
|
@@ -47,8 +27,8 @@ class RedisClient {
|
|
47
27
|
}
|
48
28
|
connect(masterHost, replicaHosts) {
|
49
29
|
return __awaiter(this, void 0, void 0, function* () {
|
50
|
-
this.master = new ioredis_1.default({ host: masterHost, port: 6379 });
|
51
|
-
this.replicas = replicaHosts.map(replicaHost => new ioredis_1.default({ host: replicaHost, port: 6379 }));
|
30
|
+
this.master = new ioredis_1.default({ host: masterHost, port: 6379, maxRetriesPerRequest: null, });
|
31
|
+
this.replicas = replicaHosts.map(replicaHost => new ioredis_1.default({ host: replicaHost, port: 6379, maxRetriesPerRequest: null, }));
|
52
32
|
const masterConnected = new Promise((resolve) => {
|
53
33
|
this.master.on('connect', () => {
|
54
34
|
console.log('Master connected');
|
@@ -76,35 +56,18 @@ class RedisClient {
|
|
76
56
|
return console.log('All connections established');
|
77
57
|
});
|
78
58
|
}
|
79
|
-
executeMasterOperation(command, ...args) {
|
80
|
-
return __awaiter(this, void 0, void 0, function* () {
|
81
|
-
return this.executeOperation(this.master, command, ...args);
|
82
|
-
});
|
83
|
-
}
|
84
|
-
executeReplicaOperation(command, ...args) {
|
85
|
-
return __awaiter(this, void 0, void 0, function* () {
|
86
|
-
const replica = this.replicas[Math.floor(Math.random() * this.replicas.length)];
|
87
|
-
return this.executeOperation(replica, command, ...args);
|
88
|
-
});
|
89
|
-
}
|
90
|
-
executeOperation(client, command, ...args) {
|
91
|
-
return __awaiter(this, void 0, void 0, function* () {
|
92
|
-
return client.sendCommand(new ioredis_1.Command(command, args));
|
93
|
-
});
|
94
|
-
}
|
95
59
|
set(key, value) {
|
96
|
-
return this.
|
60
|
+
return this.master.set(key, value);
|
97
61
|
}
|
98
62
|
get(key) {
|
99
|
-
|
63
|
+
const replica = this.replicas[Math.floor(Math.random() * this.replicas.length)];
|
64
|
+
return replica.get(key);
|
100
65
|
}
|
101
66
|
del(key) {
|
102
|
-
return this.
|
67
|
+
return this.master.del(key);
|
103
68
|
}
|
104
|
-
setex(key,
|
105
|
-
return
|
106
|
-
return this.executeMasterOperation('SETEX', key, seconds, value);
|
107
|
-
});
|
69
|
+
setex(key, seconds, value) {
|
70
|
+
return this.master.setex(key, seconds, value);
|
108
71
|
}
|
109
72
|
quit() {
|
110
73
|
this.master.quit();
|