@samet-it/be-redis-common 1.1.2 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/channel/redis.channel.d.ts +1 -8
- package/dist/channel/redis.channel.js +13 -32
- package/dist/config/index.types.d.ts +4 -0
- package/dist/config/redis-common.config.js +24 -7
- package/dist/connection/redis.connection.d.ts +5 -0
- package/dist/connection/redis.connection.js +39 -23
- package/package.json +5 -5
|
@@ -2,7 +2,7 @@ import type { RedisConnectionLike } from "../connection";
|
|
|
2
2
|
import type { RedisChannelProps, RedisChannelLike, RedisChannelOpt } from "./index.types";
|
|
3
3
|
import { type Entity } from "@samet-it/be-base-common";
|
|
4
4
|
import { CacheChannel } from "@samet-it/be-cache-common";
|
|
5
|
-
import type { KeyValue
|
|
5
|
+
import type { KeyValue } from "@leyyo/common";
|
|
6
6
|
/**
|
|
7
7
|
* Redis abstract channel class
|
|
8
8
|
*
|
|
@@ -20,13 +20,6 @@ export declare abstract class RedisChannel<ENT extends Entity<ID>, ID extends Ke
|
|
|
20
20
|
* @param {RedisChannelOpt} opt - options
|
|
21
21
|
* */
|
|
22
22
|
protected constructor(conn: RedisConnectionLike, opt?: RedisChannelOpt);
|
|
23
|
-
/**
|
|
24
|
-
* @Check error
|
|
25
|
-
*
|
|
26
|
-
* @param {Error} e - error instance
|
|
27
|
-
* @param {Opt} params - parameters
|
|
28
|
-
* */
|
|
29
|
-
protected _checkError(e: Error, params?: Opt): void;
|
|
30
23
|
/** @inheritDoc */
|
|
31
24
|
get props(): Readonly<RedisChannelProps>;
|
|
32
25
|
/** @inheritDoc */
|
|
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.RedisChannel = void 0;
|
|
13
|
-
const be_base_common_1 = require("@samet-it/be-base-common");
|
|
14
13
|
const be_cache_common_1 = require("@samet-it/be-cache-common");
|
|
15
14
|
// noinspection JSUnusedGlobalSymbols
|
|
16
15
|
/**
|
|
@@ -43,24 +42,6 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
43
42
|
this._props.isConnected = false;
|
|
44
43
|
}));
|
|
45
44
|
}
|
|
46
|
-
// region protected-methods
|
|
47
|
-
/**
|
|
48
|
-
* @Check error
|
|
49
|
-
*
|
|
50
|
-
* @param {Error} e - error instance
|
|
51
|
-
* @param {Opt} params - parameters
|
|
52
|
-
* */
|
|
53
|
-
_checkError(e, params) {
|
|
54
|
-
be_base_common_1.errorHandler.addStat(e);
|
|
55
|
-
if (e instanceof be_cache_common_1.CacheError) {
|
|
56
|
-
// todo append params
|
|
57
|
-
throw e;
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
throw be_base_common_1.errorHandler.common.castForClass(be_cache_common_1.CacheError, e, params);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
// endregion protected-methods
|
|
64
45
|
// region getter
|
|
65
46
|
/** @inheritDoc */
|
|
66
47
|
get props() {
|
|
@@ -71,7 +52,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
71
52
|
/** @inheritDoc */
|
|
72
53
|
$get(...paths) {
|
|
73
54
|
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
-
const { client } = this._props;
|
|
55
|
+
const { client, conn } = this._props;
|
|
75
56
|
if (!client) {
|
|
76
57
|
return [];
|
|
77
58
|
}
|
|
@@ -86,14 +67,14 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
86
67
|
return (yield client.mGet(paths));
|
|
87
68
|
}
|
|
88
69
|
catch (e) {
|
|
89
|
-
|
|
70
|
+
conn.checkError(e, { name: 'MGET', method: '$get' });
|
|
90
71
|
}
|
|
91
72
|
});
|
|
92
73
|
}
|
|
93
74
|
/** @inheritDoc */
|
|
94
75
|
$set(map) {
|
|
95
76
|
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
-
const { client } = this._props;
|
|
77
|
+
const { client, conn } = this._props;
|
|
97
78
|
if (!client) {
|
|
98
79
|
return 0;
|
|
99
80
|
}
|
|
@@ -110,7 +91,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
110
91
|
yield client.set(key, map[key]);
|
|
111
92
|
}
|
|
112
93
|
catch (e) {
|
|
113
|
-
|
|
94
|
+
conn.checkError(e, { name: 'SET', method: '$set' });
|
|
114
95
|
}
|
|
115
96
|
return 1;
|
|
116
97
|
default:
|
|
@@ -118,7 +99,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
118
99
|
yield client.mSet(map);
|
|
119
100
|
}
|
|
120
101
|
catch (e) {
|
|
121
|
-
|
|
102
|
+
conn.checkError(e, { name: 'MSET', method: '$set' });
|
|
122
103
|
}
|
|
123
104
|
return keys.length;
|
|
124
105
|
}
|
|
@@ -127,7 +108,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
127
108
|
/** @inheritDoc */
|
|
128
109
|
$delete(...paths) {
|
|
129
110
|
return __awaiter(this, void 0, void 0, function* () {
|
|
130
|
-
const { client } = this._props;
|
|
111
|
+
const { client, conn } = this._props;
|
|
131
112
|
if (!client) {
|
|
132
113
|
return 0;
|
|
133
114
|
}
|
|
@@ -138,7 +119,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
138
119
|
yield client.del(paths);
|
|
139
120
|
}
|
|
140
121
|
catch (e) {
|
|
141
|
-
|
|
122
|
+
conn.checkError(e, { name: 'DEL', method: '$delete' });
|
|
142
123
|
}
|
|
143
124
|
return paths.length;
|
|
144
125
|
});
|
|
@@ -146,7 +127,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
146
127
|
/** @inheritDoc */
|
|
147
128
|
$addLinks(idPath, paths) {
|
|
148
129
|
return __awaiter(this, void 0, void 0, function* () {
|
|
149
|
-
const { client } = this._props;
|
|
130
|
+
const { client, conn } = this._props;
|
|
150
131
|
if (!client) {
|
|
151
132
|
return 0;
|
|
152
133
|
}
|
|
@@ -157,7 +138,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
157
138
|
yield client.sAdd(idPath, paths);
|
|
158
139
|
}
|
|
159
140
|
catch (e) {
|
|
160
|
-
|
|
141
|
+
conn.checkError(e, { name: 'SADD', method: '$addLinks' });
|
|
161
142
|
}
|
|
162
143
|
return paths.length;
|
|
163
144
|
});
|
|
@@ -165,7 +146,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
165
146
|
/** @inheritDoc */
|
|
166
147
|
$expire(path, seconds) {
|
|
167
148
|
return __awaiter(this, void 0, void 0, function* () {
|
|
168
|
-
const { client } = this._props;
|
|
149
|
+
const { client, conn } = this._props;
|
|
169
150
|
if (!client) {
|
|
170
151
|
return false;
|
|
171
152
|
}
|
|
@@ -173,7 +154,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
173
154
|
return (yield client.expire(path, seconds)) > 0;
|
|
174
155
|
}
|
|
175
156
|
catch (e) {
|
|
176
|
-
|
|
157
|
+
conn.checkError(e, { name: 'EXPIRE', method: '$expire' });
|
|
177
158
|
}
|
|
178
159
|
});
|
|
179
160
|
}
|
|
@@ -181,7 +162,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
181
162
|
$getLinks(idPath) {
|
|
182
163
|
return __awaiter(this, void 0, void 0, function* () {
|
|
183
164
|
var _a;
|
|
184
|
-
const { client } = this._props;
|
|
165
|
+
const { client, conn } = this._props;
|
|
185
166
|
if (!client) {
|
|
186
167
|
return [];
|
|
187
168
|
}
|
|
@@ -189,7 +170,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
189
170
|
return (_a = (yield client.sMembers(idPath))) !== null && _a !== void 0 ? _a : [];
|
|
190
171
|
}
|
|
191
172
|
catch (e) {
|
|
192
|
-
|
|
173
|
+
conn.checkError(e, { name: 'SMEMBERS', method: '$getLinks' });
|
|
193
174
|
}
|
|
194
175
|
});
|
|
195
176
|
}
|
|
@@ -8,11 +8,28 @@ const env_1 = require("@leyyo/env");
|
|
|
8
8
|
exports.redisCommonConfig = env_1.envCore.configure
|
|
9
9
|
.scope('RedisCommon', 'REDIS')
|
|
10
10
|
.start()
|
|
11
|
-
|
|
12
|
-
.field('
|
|
13
|
-
.field('
|
|
14
|
-
.
|
|
15
|
-
.
|
|
16
|
-
.field('
|
|
17
|
-
.
|
|
11
|
+
// @formatter:off
|
|
12
|
+
.field('ENABLED').boolean().def(true).end()
|
|
13
|
+
.field('PROTOCOL')
|
|
14
|
+
.off(v => v.ENABLED)
|
|
15
|
+
.text().def('redis').end()
|
|
16
|
+
.field('HOST')
|
|
17
|
+
.off(v => v.ENABLED)
|
|
18
|
+
.text().required().end()
|
|
19
|
+
.field('PORT')
|
|
20
|
+
.off(v => v.ENABLED)
|
|
21
|
+
.integer().def(6379).end()
|
|
22
|
+
.field('USER')
|
|
23
|
+
.off(v => v.ENABLED)
|
|
24
|
+
.text().end()
|
|
25
|
+
.field('PASS')
|
|
26
|
+
.off(v => v.ENABLED)
|
|
27
|
+
.text().end()
|
|
28
|
+
.field('PREFIX')
|
|
29
|
+
.off(v => v.ENABLED)
|
|
30
|
+
.text().end()
|
|
31
|
+
.field('DB_NUMBER')
|
|
32
|
+
.off(v => v.ENABLED)
|
|
33
|
+
.integer().end()
|
|
34
|
+
// @formatter:on
|
|
18
35
|
.finish();
|
|
@@ -24,6 +24,11 @@ export declare abstract class RedisConnection extends CacheConnection<RedisExecO
|
|
|
24
24
|
private static readonly TRY_COUNT;
|
|
25
25
|
/** {@inheritDoc} */
|
|
26
26
|
protected _props: RedisConnProps;
|
|
27
|
+
/**
|
|
28
|
+
* Constructor
|
|
29
|
+
*
|
|
30
|
+
* @param {RedisConnOpt} opt - options
|
|
31
|
+
* */
|
|
27
32
|
protected constructor(opt?: RedisConnOpt);
|
|
28
33
|
/** @inheritDoc */
|
|
29
34
|
get props(): Readonly<RedisConnProps>;
|
|
@@ -46,11 +46,17 @@ exports.RedisConnection = void 0;
|
|
|
46
46
|
const redis = __importStar(require("redis"));
|
|
47
47
|
const config_1 = require("../config");
|
|
48
48
|
const be_cache_common_1 = require("@samet-it/be-cache-common");
|
|
49
|
+
const be_base_common_1 = require("@samet-it/be-base-common");
|
|
49
50
|
/**
|
|
50
51
|
* Redis connection abstract class
|
|
51
52
|
* */
|
|
52
53
|
class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
53
54
|
// endregion protected-property
|
|
55
|
+
/**
|
|
56
|
+
* Constructor
|
|
57
|
+
*
|
|
58
|
+
* @param {RedisConnOpt} opt - options
|
|
59
|
+
* */
|
|
54
60
|
constructor(opt) {
|
|
55
61
|
super(opt);
|
|
56
62
|
this._props.tryCount = 0;
|
|
@@ -68,18 +74,19 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
68
74
|
* Read credentials from environment
|
|
69
75
|
* */
|
|
70
76
|
_readFromEnv() {
|
|
71
|
-
var _a, _b, _c, _d, _e, _f;
|
|
77
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
72
78
|
const { _props: props } = this;
|
|
73
79
|
let env = config_1.redisCommonConfig.valueShort;
|
|
74
80
|
if (props.envVariant) {
|
|
75
81
|
env = config_1.redisCommonConfig.configure.getVariation(props.envVariant).valueShort;
|
|
76
82
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
+
props.enabled = (_a = props.enabled) !== null && _a !== void 0 ? _a : env.ENABLED;
|
|
84
|
+
props.protocol = (_b = props.protocol) !== null && _b !== void 0 ? _b : env.PROTOCOL;
|
|
85
|
+
props.host = (_c = props.host) !== null && _c !== void 0 ? _c : env.HOST;
|
|
86
|
+
props.port = (_d = props.port) !== null && _d !== void 0 ? _d : env.PORT;
|
|
87
|
+
props.username = (_e = props.username) !== null && _e !== void 0 ? _e : env.USER;
|
|
88
|
+
props.password = (_f = props.password) !== null && _f !== void 0 ? _f : env.PASS;
|
|
89
|
+
props.dbNumber = (_g = props.dbNumber) !== null && _g !== void 0 ? _g : env.DB_NUMBER;
|
|
83
90
|
}
|
|
84
91
|
/**
|
|
85
92
|
* Build url from credentials
|
|
@@ -118,21 +125,27 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
118
125
|
/** {@inheritDoc} */
|
|
119
126
|
connect() {
|
|
120
127
|
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
-
|
|
128
|
+
const { _props: props } = this;
|
|
129
|
+
const { enabled, isConnected, tryCount, producedUrl } = props;
|
|
130
|
+
if (!enabled) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
if (isConnected) {
|
|
122
134
|
return true;
|
|
123
135
|
}
|
|
124
|
-
if (
|
|
125
|
-
|
|
136
|
+
if (tryCount > RedisConnection.TRY_COUNT) {
|
|
137
|
+
const err = new be_cache_common_1.CacheError('Maximum try county', { tryCount });
|
|
138
|
+
this.logger.error(be_base_common_1.errorHandler.common.logText(err, 'connect', 'try', tryCount));
|
|
126
139
|
return false;
|
|
127
140
|
}
|
|
128
141
|
try {
|
|
129
|
-
|
|
130
|
-
|
|
142
|
+
props.client = redis.createClient({ url: producedUrl });
|
|
143
|
+
props.client
|
|
131
144
|
.on("error", err => {
|
|
132
145
|
this.checkError(err, { silent: true, name: 'connection' });
|
|
133
|
-
|
|
134
|
-
const old =
|
|
135
|
-
|
|
146
|
+
props.tryCount++;
|
|
147
|
+
const old = isConnected;
|
|
148
|
+
props.isConnected = false;
|
|
136
149
|
if (old) {
|
|
137
150
|
this._triggerOnCase('disconnected', this._onDisconnected, false);
|
|
138
151
|
}
|
|
@@ -140,19 +153,19 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
140
153
|
})
|
|
141
154
|
.on('connect', () => {
|
|
142
155
|
this.logger.info(`on[connect]`);
|
|
143
|
-
|
|
144
|
-
|
|
156
|
+
props.isConnected = true;
|
|
157
|
+
props.tryCount = 0;
|
|
145
158
|
this._triggerOnCase('connected', this._onConnected, false);
|
|
146
|
-
if (
|
|
159
|
+
if (props.isFirst === undefined) {
|
|
147
160
|
this._triggerOnCase('first-connected', this._onFirstConnected, true);
|
|
148
|
-
|
|
161
|
+
props.isFirst = false;
|
|
149
162
|
}
|
|
150
163
|
})
|
|
151
164
|
.on('ready', () => this.logger.info('on[ready]'))
|
|
152
165
|
.on('close', () => this.logger.info('on[close]'))
|
|
153
166
|
.on('reconnecting', () => this.logger.info('on[reconnecting]'))
|
|
154
167
|
.on('end', () => this.logger.info('on[end]'));
|
|
155
|
-
yield
|
|
168
|
+
yield props.client.connect();
|
|
156
169
|
this.logger.log('Connected');
|
|
157
170
|
setTimeout(() => this.ping(true).then(), 30000);
|
|
158
171
|
}
|
|
@@ -165,11 +178,14 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
165
178
|
/** {@inheritDoc} */
|
|
166
179
|
ping(next) {
|
|
167
180
|
return __awaiter(this, void 0, void 0, function* () {
|
|
168
|
-
|
|
181
|
+
const { enabled, isConnected, client } = this._props;
|
|
182
|
+
if (!enabled) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
169
185
|
let result = false;
|
|
170
|
-
if (
|
|
186
|
+
if (isConnected) {
|
|
171
187
|
try {
|
|
172
|
-
yield
|
|
188
|
+
yield client.ping();
|
|
173
189
|
result = true;
|
|
174
190
|
}
|
|
175
191
|
catch (err) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@samet-it/be-redis-common",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "Redis common component",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"redis",
|
|
@@ -67,10 +67,10 @@
|
|
|
67
67
|
}
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
|
-
"@leyyo/common": "^1.2.
|
|
71
|
-
"@leyyo/env": "^1.2.
|
|
72
|
-
"@samet-it/be-base-common": "^1.1.
|
|
73
|
-
"@samet-it/be-cache-common": "^1.1.
|
|
70
|
+
"@leyyo/common": "^1.2.3",
|
|
71
|
+
"@leyyo/env": "^1.2.5",
|
|
72
|
+
"@samet-it/be-base-common": "^1.1.3",
|
|
73
|
+
"@samet-it/be-cache-common": "^1.1.4",
|
|
74
74
|
"redis": "^5.10.0"
|
|
75
75
|
}
|
|
76
76
|
}
|