@hastehaul/common 2.0.41 → 2.0.44
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
|
+
declare class RedisClient {
|
3
|
+
private master;
|
4
|
+
private replicas;
|
5
|
+
private static instance;
|
6
|
+
private constructor();
|
7
|
+
static getInstance(masterHost: string, replicaHosts: string[]): RedisClient;
|
8
|
+
executeMasterOperation(command: string, ...args: any[]): Promise<any>;
|
9
|
+
executeReplicaOperation(command: string, ...args: any[]): Promise<any>;
|
10
|
+
executeOperation(client: IORedis, command: string, ...args: any[]): Promise<any>;
|
11
|
+
set(key: string, value: string): Promise<any>;
|
12
|
+
get(key: string): Promise<any>;
|
13
|
+
del(key: string): Promise<any>;
|
14
|
+
setex(key: string, value: string, seconds: number): Promise<any>;
|
15
|
+
quit(): void;
|
16
|
+
}
|
17
|
+
export default RedisClient;
|
@@ -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
|
+
const ioredis_1 = __importStar(require("ioredis"));
|
36
|
+
class RedisClient {
|
37
|
+
constructor(masterHost, replicaHosts) {
|
38
|
+
this.master = new ioredis_1.default({ host: masterHost, port: 6379 });
|
39
|
+
this.replicas = replicaHosts.map(replicaHost => new ioredis_1.default({ host: replicaHost, port: 6379 }));
|
40
|
+
}
|
41
|
+
// get client(): IORedis {
|
42
|
+
// if(!this._redisClient) throw new Error("Cannot access Redis Client before connecting")
|
43
|
+
// return this._redisClient;
|
44
|
+
// }
|
45
|
+
static getInstance(masterHost, replicaHosts) {
|
46
|
+
if (!RedisClient.instance) {
|
47
|
+
RedisClient.instance = new RedisClient(masterHost, replicaHosts);
|
48
|
+
}
|
49
|
+
return RedisClient.instance;
|
50
|
+
}
|
51
|
+
executeMasterOperation(command, ...args) {
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
53
|
+
return this.executeOperation(this.master, command, ...args);
|
54
|
+
});
|
55
|
+
}
|
56
|
+
executeReplicaOperation(command, ...args) {
|
57
|
+
return __awaiter(this, void 0, void 0, function* () {
|
58
|
+
const replica = this.replicas[Math.floor(Math.random() * this.replicas.length)];
|
59
|
+
return this.executeOperation(replica, command, ...args);
|
60
|
+
});
|
61
|
+
}
|
62
|
+
executeOperation(client, command, ...args) {
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
64
|
+
return client.sendCommand(new ioredis_1.Command(command, args));
|
65
|
+
});
|
66
|
+
}
|
67
|
+
set(key, value) {
|
68
|
+
return this.executeMasterOperation('SET', key, value);
|
69
|
+
}
|
70
|
+
get(key) {
|
71
|
+
return this.executeReplicaOperation('GET', key);
|
72
|
+
}
|
73
|
+
del(key) {
|
74
|
+
return this.executeMasterOperation('DEL', key);
|
75
|
+
}
|
76
|
+
setex(key, value, seconds) {
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
78
|
+
return this.executeMasterOperation('SETEX', key, seconds, value);
|
79
|
+
});
|
80
|
+
}
|
81
|
+
quit() {
|
82
|
+
this.master.quit();
|
83
|
+
this.replicas.forEach(replica => replica.quit());
|
84
|
+
}
|
85
|
+
}
|
86
|
+
// private _redisClient?: IORedis
|
87
|
+
RedisClient.instance = null;
|
88
|
+
exports.default = RedisClient;
|
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/build/utils/utils.d.ts
CHANGED
@@ -6,5 +6,5 @@ export declare function hasElapsed(expirationDate: Date): boolean;
|
|
6
6
|
export declare function colorize(text: string, colorCode: number): string;
|
7
7
|
export declare function formatDatetimeToHumanReadable(datetime: Date, format?: string): string;
|
8
8
|
export declare function generateIdentifier(messageContent: string): string;
|
9
|
-
export declare function getValueFromSettings(settings: Setting[], key: string, envVar
|
9
|
+
export declare function getValueFromSettings(settings: Setting[], key: string, envVar?: string, defaultValue?: any): any;
|
10
10
|
export {};
|