@samet-it/be-redis-common 1.1.3 → 1.1.5
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.
|
@@ -3,6 +3,7 @@ import type { RedisChannelProps, RedisChannelLike, RedisChannelOpt } from "./ind
|
|
|
3
3
|
import { type Entity } from "@samet-it/be-base-common";
|
|
4
4
|
import { CacheChannel } from "@samet-it/be-cache-common";
|
|
5
5
|
import type { KeyValue } from "@leyyo/common";
|
|
6
|
+
import type { CacheExecOpt } from "@samet-it/be-cache-common/dist/connection";
|
|
6
7
|
/**
|
|
7
8
|
* Redis abstract channel class
|
|
8
9
|
*
|
|
@@ -10,7 +11,7 @@ import type { KeyValue } from "@leyyo/common";
|
|
|
10
11
|
* - 0-`ENT`: entity {@link Entity}
|
|
11
12
|
* - 1-`ID`: id type {@link KeyValue}
|
|
12
13
|
* */
|
|
13
|
-
export declare
|
|
14
|
+
export declare class RedisChannel<ENT extends Entity<ID>, ID extends KeyValue> extends CacheChannel<RedisConnectionLike, ENT, ID> implements RedisChannelLike<ENT, ID> {
|
|
14
15
|
/** @inheritDoc */
|
|
15
16
|
protected _props: RedisChannelProps;
|
|
16
17
|
/**
|
|
@@ -23,15 +24,21 @@ export declare abstract class RedisChannel<ENT extends Entity<ID>, ID extends Ke
|
|
|
23
24
|
/** @inheritDoc */
|
|
24
25
|
get props(): Readonly<RedisChannelProps>;
|
|
25
26
|
/** @inheritDoc */
|
|
26
|
-
$get(
|
|
27
|
+
$get(path: string, _opt?: CacheExecOpt): Promise<string | undefined>;
|
|
27
28
|
/** @inheritDoc */
|
|
28
|
-
$
|
|
29
|
+
$getMore(paths: Array<string>, _opt?: CacheExecOpt): Promise<Array<string | undefined>>;
|
|
29
30
|
/** @inheritDoc */
|
|
30
|
-
$
|
|
31
|
+
$set(path: string, value: string, _opt?: CacheExecOpt): Promise<boolean>;
|
|
31
32
|
/** @inheritDoc */
|
|
32
|
-
$
|
|
33
|
+
$setMore(map: Record<string, string>, _opt?: CacheExecOpt): Promise<number>;
|
|
33
34
|
/** @inheritDoc */
|
|
34
|
-
$
|
|
35
|
+
$delete(path: string, _opt?: CacheExecOpt): Promise<boolean>;
|
|
35
36
|
/** @inheritDoc */
|
|
36
|
-
$
|
|
37
|
+
$deleteMore(paths: string[], _opt?: CacheExecOpt): Promise<number>;
|
|
38
|
+
/** @inheritDoc */
|
|
39
|
+
$addLinks(idPath: string, paths: Array<string>, _opt?: CacheExecOpt): Promise<number>;
|
|
40
|
+
/** @inheritDoc */
|
|
41
|
+
$expire(path: string, seconds: number, _opt?: CacheExecOpt): Promise<boolean>;
|
|
42
|
+
/** @inheritDoc */
|
|
43
|
+
$getLinks(idPath: string, _opt?: CacheExecOpt): Promise<Array<string>>;
|
|
37
44
|
}
|
|
@@ -50,7 +50,26 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
50
50
|
// endregion getter
|
|
51
51
|
// region native-calls
|
|
52
52
|
/** @inheritDoc */
|
|
53
|
-
$get(
|
|
53
|
+
$get(path, _opt) {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
const { client, conn } = this._props;
|
|
56
|
+
if (!client) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
if (typeof path !== 'string') {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
return yield client.get(path);
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
conn.checkError(e, { name: 'GET', method: '$get' });
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/** @inheritDoc */
|
|
72
|
+
$getMore(paths, _opt) {
|
|
54
73
|
return __awaiter(this, void 0, void 0, function* () {
|
|
55
74
|
const { client, conn } = this._props;
|
|
56
75
|
if (!client) {
|
|
@@ -60,19 +79,46 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
60
79
|
return [];
|
|
61
80
|
}
|
|
62
81
|
if (paths.length == 1) {
|
|
63
|
-
|
|
64
|
-
|
|
82
|
+
try {
|
|
83
|
+
const rec = (yield client.get(paths[0]));
|
|
84
|
+
return rec ? [rec] : [];
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
conn.checkError(e, { name: 'GET', method: '$getMore' });
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
65
90
|
}
|
|
66
91
|
try {
|
|
67
92
|
return (yield client.mGet(paths));
|
|
68
93
|
}
|
|
69
94
|
catch (e) {
|
|
70
|
-
conn.checkError(e, { name: 'MGET', method: '$
|
|
95
|
+
conn.checkError(e, { name: 'MGET', method: '$getMore' });
|
|
96
|
+
return [];
|
|
71
97
|
}
|
|
72
98
|
});
|
|
73
99
|
}
|
|
74
100
|
/** @inheritDoc */
|
|
75
|
-
$set(
|
|
101
|
+
$set(path, value, _opt) {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
const { client, conn } = this._props;
|
|
104
|
+
if (!client) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
if (typeof path !== 'string' || typeof value !== 'string') {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
yield client.set(path, value);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
conn.checkError(e, { name: 'SET', method: '$set' });
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/** @inheritDoc */
|
|
121
|
+
$setMore(map, _opt) {
|
|
76
122
|
return __awaiter(this, void 0, void 0, function* () {
|
|
77
123
|
const { client, conn } = this._props;
|
|
78
124
|
if (!client) {
|
|
@@ -91,7 +137,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
91
137
|
yield client.set(key, map[key]);
|
|
92
138
|
}
|
|
93
139
|
catch (e) {
|
|
94
|
-
conn.checkError(e, { name: 'SET', method: '$
|
|
140
|
+
conn.checkError(e, { name: 'SET', method: '$setMore' });
|
|
95
141
|
}
|
|
96
142
|
return 1;
|
|
97
143
|
default:
|
|
@@ -99,14 +145,34 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
99
145
|
yield client.mSet(map);
|
|
100
146
|
}
|
|
101
147
|
catch (e) {
|
|
102
|
-
conn.checkError(e, { name: 'MSET', method: '$
|
|
148
|
+
conn.checkError(e, { name: 'MSET', method: '$setMore' });
|
|
103
149
|
}
|
|
104
150
|
return keys.length;
|
|
105
151
|
}
|
|
106
152
|
});
|
|
107
153
|
}
|
|
108
154
|
/** @inheritDoc */
|
|
109
|
-
$delete(
|
|
155
|
+
$delete(path, _opt) {
|
|
156
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
+
const { client, conn } = this._props;
|
|
158
|
+
if (!client) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
if (typeof path !== 'string') {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
const result = yield client.del(path);
|
|
166
|
+
return result > 0;
|
|
167
|
+
}
|
|
168
|
+
catch (e) {
|
|
169
|
+
conn.checkError(e, { name: 'DEL', method: '$delete' });
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
/** @inheritDoc */
|
|
175
|
+
$deleteMore(paths, _opt) {
|
|
110
176
|
return __awaiter(this, void 0, void 0, function* () {
|
|
111
177
|
const { client, conn } = this._props;
|
|
112
178
|
if (!client) {
|
|
@@ -119,13 +185,13 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
119
185
|
yield client.del(paths);
|
|
120
186
|
}
|
|
121
187
|
catch (e) {
|
|
122
|
-
conn.checkError(e, { name: 'DEL', method: '$
|
|
188
|
+
conn.checkError(e, { name: 'DEL', method: '$deleteMore' });
|
|
123
189
|
}
|
|
124
190
|
return paths.length;
|
|
125
191
|
});
|
|
126
192
|
}
|
|
127
193
|
/** @inheritDoc */
|
|
128
|
-
$addLinks(idPath, paths) {
|
|
194
|
+
$addLinks(idPath, paths, _opt) {
|
|
129
195
|
return __awaiter(this, void 0, void 0, function* () {
|
|
130
196
|
const { client, conn } = this._props;
|
|
131
197
|
if (!client) {
|
|
@@ -144,7 +210,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
144
210
|
});
|
|
145
211
|
}
|
|
146
212
|
/** @inheritDoc */
|
|
147
|
-
$expire(path, seconds) {
|
|
213
|
+
$expire(path, seconds, _opt) {
|
|
148
214
|
return __awaiter(this, void 0, void 0, function* () {
|
|
149
215
|
const { client, conn } = this._props;
|
|
150
216
|
if (!client) {
|
|
@@ -159,7 +225,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
159
225
|
});
|
|
160
226
|
}
|
|
161
227
|
/** @inheritDoc */
|
|
162
|
-
$getLinks(idPath) {
|
|
228
|
+
$getLinks(idPath, _opt) {
|
|
163
229
|
return __awaiter(this, void 0, void 0, function* () {
|
|
164
230
|
var _a;
|
|
165
231
|
const { client, conn } = this._props;
|
|
@@ -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();
|
|
@@ -74,18 +74,19 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
74
74
|
* Read credentials from environment
|
|
75
75
|
* */
|
|
76
76
|
_readFromEnv() {
|
|
77
|
-
var _a, _b, _c, _d, _e, _f;
|
|
77
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
78
78
|
const { _props: props } = this;
|
|
79
79
|
let env = config_1.redisCommonConfig.valueShort;
|
|
80
80
|
if (props.envVariant) {
|
|
81
81
|
env = config_1.redisCommonConfig.configure.getVariation(props.envVariant).valueShort;
|
|
82
82
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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;
|
|
89
90
|
}
|
|
90
91
|
/**
|
|
91
92
|
* Build url from credentials
|
|
@@ -124,22 +125,27 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
124
125
|
/** {@inheritDoc} */
|
|
125
126
|
connect() {
|
|
126
127
|
return __awaiter(this, void 0, void 0, function* () {
|
|
127
|
-
|
|
128
|
+
const { _props: props } = this;
|
|
129
|
+
const { enabled, isConnected, tryCount, producedUrl } = props;
|
|
130
|
+
if (!enabled) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
if (isConnected) {
|
|
128
134
|
return true;
|
|
129
135
|
}
|
|
130
|
-
if (
|
|
131
|
-
const err = new be_cache_common_1.CacheError('Maximum try county', {
|
|
132
|
-
this.logger.error(be_base_common_1.errorHandler.common.logText(err, 'connect', 'try',
|
|
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));
|
|
133
139
|
return false;
|
|
134
140
|
}
|
|
135
141
|
try {
|
|
136
|
-
|
|
137
|
-
|
|
142
|
+
props.client = redis.createClient({ url: producedUrl });
|
|
143
|
+
props.client
|
|
138
144
|
.on("error", err => {
|
|
139
145
|
this.checkError(err, { silent: true, name: 'connection' });
|
|
140
|
-
|
|
141
|
-
const old =
|
|
142
|
-
|
|
146
|
+
props.tryCount++;
|
|
147
|
+
const old = isConnected;
|
|
148
|
+
props.isConnected = false;
|
|
143
149
|
if (old) {
|
|
144
150
|
this._triggerOnCase('disconnected', this._onDisconnected, false);
|
|
145
151
|
}
|
|
@@ -147,19 +153,19 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
147
153
|
})
|
|
148
154
|
.on('connect', () => {
|
|
149
155
|
this.logger.info(`on[connect]`);
|
|
150
|
-
|
|
151
|
-
|
|
156
|
+
props.isConnected = true;
|
|
157
|
+
props.tryCount = 0;
|
|
152
158
|
this._triggerOnCase('connected', this._onConnected, false);
|
|
153
|
-
if (
|
|
159
|
+
if (props.isFirst === undefined) {
|
|
154
160
|
this._triggerOnCase('first-connected', this._onFirstConnected, true);
|
|
155
|
-
|
|
161
|
+
props.isFirst = false;
|
|
156
162
|
}
|
|
157
163
|
})
|
|
158
164
|
.on('ready', () => this.logger.info('on[ready]'))
|
|
159
165
|
.on('close', () => this.logger.info('on[close]'))
|
|
160
166
|
.on('reconnecting', () => this.logger.info('on[reconnecting]'))
|
|
161
167
|
.on('end', () => this.logger.info('on[end]'));
|
|
162
|
-
yield
|
|
168
|
+
yield props.client.connect();
|
|
163
169
|
this.logger.log('Connected');
|
|
164
170
|
setTimeout(() => this.ping(true).then(), 30000);
|
|
165
171
|
}
|
|
@@ -172,11 +178,14 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
172
178
|
/** {@inheritDoc} */
|
|
173
179
|
ping(next) {
|
|
174
180
|
return __awaiter(this, void 0, void 0, function* () {
|
|
175
|
-
|
|
181
|
+
const { enabled, isConnected, client } = this._props;
|
|
182
|
+
if (!enabled) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
176
185
|
let result = false;
|
|
177
|
-
if (
|
|
186
|
+
if (isConnected) {
|
|
178
187
|
try {
|
|
179
|
-
yield
|
|
188
|
+
yield client.ping();
|
|
180
189
|
result = true;
|
|
181
190
|
}
|
|
182
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.5",
|
|
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.4",
|
|
71
|
+
"@leyyo/env": "^1.2.5",
|
|
72
|
+
"@samet-it/be-base-common": "^1.1.4",
|
|
73
|
+
"@samet-it/be-cache-common": "^1.1.5",
|
|
74
74
|
"redis": "^5.10.0"
|
|
75
75
|
}
|
|
76
76
|
}
|