@samet-it/be-redis-common 1.1.7 → 1.1.8
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/index.types.d.ts +4 -6
- package/dist/channel/redis-direct.channel.js +1 -1
- package/dist/channel/redis.channel.d.ts +3 -0
- package/dist/channel/redis.channel.js +35 -52
- package/dist/connection/index.types.d.ts +4 -0
- package/dist/connection/redis-direct.connection.js +1 -1
- package/dist/connection/redis.connection.d.ts +3 -0
- package/dist/connection/redis.connection.js +21 -16
- package/package.json +3 -3
|
@@ -13,6 +13,10 @@ import type { KeyValue } from "@leyyo/common";
|
|
|
13
13
|
export interface RedisChannelLike<ENT extends Entity<ID>, ID extends KeyValue> extends CacheChannelLike<RedisConnectionLike, ENT, ID> {
|
|
14
14
|
/** @inheritDoc */
|
|
15
15
|
get props(): Readonly<RedisChannelProps>;
|
|
16
|
+
/**
|
|
17
|
+
* Shortcut to {@link RedisConnProps#client}
|
|
18
|
+
* */
|
|
19
|
+
get client(): RedisClientType;
|
|
16
20
|
}
|
|
17
21
|
/**
|
|
18
22
|
* Redis channel option
|
|
@@ -23,12 +27,6 @@ export interface RedisChannelOpt extends CacheChannelOpt {
|
|
|
23
27
|
* Redis channel props
|
|
24
28
|
* */
|
|
25
29
|
export interface RedisChannelProps extends CacheChannelProps<RedisConnectionLike>, RedisChannelOpt {
|
|
26
|
-
/**
|
|
27
|
-
* Redis client
|
|
28
|
-
*
|
|
29
|
-
* @type {RedisClientType}
|
|
30
|
-
* */
|
|
31
|
-
client: RedisClientType;
|
|
32
30
|
}
|
|
33
31
|
/**
|
|
34
32
|
* Redis direct channel options
|
|
@@ -19,7 +19,7 @@ class RedisDirectChannel extends redis_channel_1.RedisChannel {
|
|
|
19
19
|
* */
|
|
20
20
|
constructor(conn, opt) {
|
|
21
21
|
super(conn, opt);
|
|
22
|
-
this.logger = be_base_common_1.logger.
|
|
22
|
+
this.logger = be_base_common_1.logger.create(`RedisDirect${opt.name ? '#' + opt.name : ''}`);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
exports.RedisDirectChannel = RedisDirectChannel;
|
|
@@ -4,6 +4,7 @@ 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
6
|
import type { CacheExecOpt } from "@samet-it/be-cache-common/dist/connection";
|
|
7
|
+
import type { RedisClientType } from "redis";
|
|
7
8
|
/**
|
|
8
9
|
* Redis abstract channel class
|
|
9
10
|
*
|
|
@@ -24,6 +25,8 @@ export declare class RedisChannel<ENT extends Entity<ID>, ID extends KeyValue> e
|
|
|
24
25
|
/** @inheritDoc */
|
|
25
26
|
get props(): Readonly<RedisChannelProps>;
|
|
26
27
|
/** @inheritDoc */
|
|
28
|
+
get client(): RedisClientType;
|
|
29
|
+
/** @inheritDoc */
|
|
27
30
|
$get(path: string, _opt?: CacheExecOpt): Promise<string | undefined>;
|
|
28
31
|
/** @inheritDoc */
|
|
29
32
|
$getMore(paths: Array<string>, _opt?: CacheExecOpt): Promise<Array<string | undefined>>;
|
|
@@ -29,41 +29,32 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
29
29
|
* */
|
|
30
30
|
constructor(conn, opt) {
|
|
31
31
|
super(conn, opt);
|
|
32
|
-
conn.onFirstConnected(() => __awaiter(this, void 0, void 0, function* () {
|
|
33
|
-
this._props.client = conn.props.client;
|
|
34
|
-
this._props.isConnected = true;
|
|
35
|
-
}));
|
|
36
|
-
conn.onConnected(() => __awaiter(this, void 0, void 0, function* () {
|
|
37
|
-
this._props.client = conn.props.client;
|
|
38
|
-
this._props.isConnected = true;
|
|
39
|
-
}));
|
|
40
|
-
conn.onDisconnected(() => __awaiter(this, void 0, void 0, function* () {
|
|
41
|
-
this._props.client = undefined;
|
|
42
|
-
this._props.isConnected = false;
|
|
43
|
-
}));
|
|
44
32
|
}
|
|
45
33
|
// region getter
|
|
46
34
|
/** @inheritDoc */
|
|
47
35
|
get props() {
|
|
48
36
|
return this._props;
|
|
49
37
|
}
|
|
38
|
+
/** @inheritDoc */
|
|
39
|
+
get client() {
|
|
40
|
+
return this._props.conn.client;
|
|
41
|
+
}
|
|
50
42
|
// endregion getter
|
|
51
43
|
// region native-calls
|
|
52
44
|
/** @inheritDoc */
|
|
53
45
|
$get(path, _opt) {
|
|
54
46
|
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
-
|
|
56
|
-
if (!client) {
|
|
47
|
+
if (!this.isConnected) {
|
|
57
48
|
return undefined;
|
|
58
49
|
}
|
|
59
50
|
if (typeof path !== 'string') {
|
|
60
51
|
return undefined;
|
|
61
52
|
}
|
|
62
53
|
try {
|
|
63
|
-
return yield client.get(path);
|
|
54
|
+
return yield this.client.get(path);
|
|
64
55
|
}
|
|
65
56
|
catch (e) {
|
|
66
|
-
|
|
57
|
+
this.checkError(e, { name: 'GET', method: '$get' });
|
|
67
58
|
}
|
|
68
59
|
return undefined;
|
|
69
60
|
});
|
|
@@ -71,8 +62,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
71
62
|
/** @inheritDoc */
|
|
72
63
|
$getMore(paths, _opt) {
|
|
73
64
|
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
-
|
|
75
|
-
if (!client) {
|
|
65
|
+
if (!this.isConnected) {
|
|
76
66
|
return [];
|
|
77
67
|
}
|
|
78
68
|
if (paths.length < 1) {
|
|
@@ -80,19 +70,19 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
80
70
|
}
|
|
81
71
|
if (paths.length == 1) {
|
|
82
72
|
try {
|
|
83
|
-
const rec = (yield client.get(paths[0]));
|
|
73
|
+
const rec = (yield this.client.get(paths[0]));
|
|
84
74
|
return rec ? [rec] : [];
|
|
85
75
|
}
|
|
86
76
|
catch (e) {
|
|
87
|
-
|
|
77
|
+
this.checkError(e, { name: 'GET', method: '$getMore' });
|
|
88
78
|
return [];
|
|
89
79
|
}
|
|
90
80
|
}
|
|
91
81
|
try {
|
|
92
|
-
return (yield client.mGet(paths));
|
|
82
|
+
return (yield this.client.mGet(paths));
|
|
93
83
|
}
|
|
94
84
|
catch (e) {
|
|
95
|
-
|
|
85
|
+
this.checkError(e, { name: 'MGET', method: '$getMore' });
|
|
96
86
|
return [];
|
|
97
87
|
}
|
|
98
88
|
});
|
|
@@ -100,19 +90,18 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
100
90
|
/** @inheritDoc */
|
|
101
91
|
$set(path, value, _opt) {
|
|
102
92
|
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
-
|
|
104
|
-
if (!client) {
|
|
93
|
+
if (!this.isConnected) {
|
|
105
94
|
return false;
|
|
106
95
|
}
|
|
107
96
|
if (typeof path !== 'string' || typeof value !== 'string') {
|
|
108
97
|
return false;
|
|
109
98
|
}
|
|
110
99
|
try {
|
|
111
|
-
yield client.set(path, value);
|
|
100
|
+
yield this.client.set(path, value);
|
|
112
101
|
return true;
|
|
113
102
|
}
|
|
114
103
|
catch (e) {
|
|
115
|
-
|
|
104
|
+
this.checkError(e, { name: 'SET', method: '$set' });
|
|
116
105
|
}
|
|
117
106
|
return false;
|
|
118
107
|
});
|
|
@@ -120,8 +109,7 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
120
109
|
/** @inheritDoc */
|
|
121
110
|
$setMore(map, _opt) {
|
|
122
111
|
return __awaiter(this, void 0, void 0, function* () {
|
|
123
|
-
|
|
124
|
-
if (!client) {
|
|
112
|
+
if (!this.isConnected) {
|
|
125
113
|
return 0;
|
|
126
114
|
}
|
|
127
115
|
if (!map || typeof map !== 'object' || Array.isArray(map)) {
|
|
@@ -134,18 +122,18 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
134
122
|
case 1:
|
|
135
123
|
const key = keys[0];
|
|
136
124
|
try {
|
|
137
|
-
yield client.set(key, map[key]);
|
|
125
|
+
yield this.client.set(key, map[key]);
|
|
138
126
|
}
|
|
139
127
|
catch (e) {
|
|
140
|
-
|
|
128
|
+
this.checkError(e, { name: 'SET', method: '$setMore' });
|
|
141
129
|
}
|
|
142
130
|
return 1;
|
|
143
131
|
default:
|
|
144
132
|
try {
|
|
145
|
-
yield client.mSet(map);
|
|
133
|
+
yield this.client.mSet(map);
|
|
146
134
|
}
|
|
147
135
|
catch (e) {
|
|
148
|
-
|
|
136
|
+
this.checkError(e, { name: 'MSET', method: '$setMore' });
|
|
149
137
|
}
|
|
150
138
|
return keys.length;
|
|
151
139
|
}
|
|
@@ -154,19 +142,18 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
154
142
|
/** @inheritDoc */
|
|
155
143
|
$delete(path, _opt) {
|
|
156
144
|
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
-
|
|
158
|
-
if (!client) {
|
|
145
|
+
if (!this.isConnected) {
|
|
159
146
|
return false;
|
|
160
147
|
}
|
|
161
148
|
if (typeof path !== 'string') {
|
|
162
149
|
return false;
|
|
163
150
|
}
|
|
164
151
|
try {
|
|
165
|
-
const result = yield client.del(path);
|
|
152
|
+
const result = yield this.client.del(path);
|
|
166
153
|
return result > 0;
|
|
167
154
|
}
|
|
168
155
|
catch (e) {
|
|
169
|
-
|
|
156
|
+
this.checkError(e, { name: 'DEL', method: '$delete' });
|
|
170
157
|
}
|
|
171
158
|
return false;
|
|
172
159
|
});
|
|
@@ -174,18 +161,17 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
174
161
|
/** @inheritDoc */
|
|
175
162
|
$deleteMore(paths, _opt) {
|
|
176
163
|
return __awaiter(this, void 0, void 0, function* () {
|
|
177
|
-
|
|
178
|
-
if (!client) {
|
|
164
|
+
if (!this.isConnected) {
|
|
179
165
|
return 0;
|
|
180
166
|
}
|
|
181
167
|
if (paths.length < 1) {
|
|
182
168
|
return 0;
|
|
183
169
|
}
|
|
184
170
|
try {
|
|
185
|
-
yield client.del(paths);
|
|
171
|
+
yield this.client.del(paths);
|
|
186
172
|
}
|
|
187
173
|
catch (e) {
|
|
188
|
-
|
|
174
|
+
this.checkError(e, { name: 'DEL', method: '$deleteMore' });
|
|
189
175
|
}
|
|
190
176
|
return paths.length;
|
|
191
177
|
});
|
|
@@ -193,18 +179,17 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
193
179
|
/** @inheritDoc */
|
|
194
180
|
$addLinks(idPath, paths, _opt) {
|
|
195
181
|
return __awaiter(this, void 0, void 0, function* () {
|
|
196
|
-
|
|
197
|
-
if (!client) {
|
|
182
|
+
if (!this.isConnected) {
|
|
198
183
|
return 0;
|
|
199
184
|
}
|
|
200
185
|
if (paths.length < 1) {
|
|
201
186
|
return 0;
|
|
202
187
|
}
|
|
203
188
|
try {
|
|
204
|
-
yield client.sAdd(idPath, paths);
|
|
189
|
+
yield this.client.sAdd(idPath, paths);
|
|
205
190
|
}
|
|
206
191
|
catch (e) {
|
|
207
|
-
|
|
192
|
+
this.checkError(e, { name: 'SADD', method: '$addLinks' });
|
|
208
193
|
}
|
|
209
194
|
return paths.length;
|
|
210
195
|
});
|
|
@@ -212,15 +197,14 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
212
197
|
/** @inheritDoc */
|
|
213
198
|
$expire(path, seconds, _opt) {
|
|
214
199
|
return __awaiter(this, void 0, void 0, function* () {
|
|
215
|
-
|
|
216
|
-
if (!client) {
|
|
200
|
+
if (!this.isConnected) {
|
|
217
201
|
return false;
|
|
218
202
|
}
|
|
219
203
|
try {
|
|
220
|
-
return (yield client.expire(path, seconds)) > 0;
|
|
204
|
+
return (yield this.client.expire(path, seconds)) > 0;
|
|
221
205
|
}
|
|
222
206
|
catch (e) {
|
|
223
|
-
|
|
207
|
+
this.checkError(e, { name: 'EXPIRE', method: '$expire' });
|
|
224
208
|
}
|
|
225
209
|
});
|
|
226
210
|
}
|
|
@@ -228,15 +212,14 @@ class RedisChannel extends be_cache_common_1.CacheChannel {
|
|
|
228
212
|
$getLinks(idPath, _opt) {
|
|
229
213
|
return __awaiter(this, void 0, void 0, function* () {
|
|
230
214
|
var _a;
|
|
231
|
-
|
|
232
|
-
if (!client) {
|
|
215
|
+
if (!this.isConnected) {
|
|
233
216
|
return [];
|
|
234
217
|
}
|
|
235
218
|
try {
|
|
236
|
-
return (_a = (yield client.sMembers(idPath))) !== null && _a !== void 0 ? _a : [];
|
|
219
|
+
return (_a = (yield this.client.sMembers(idPath))) !== null && _a !== void 0 ? _a : [];
|
|
237
220
|
}
|
|
238
221
|
catch (e) {
|
|
239
|
-
|
|
222
|
+
this.checkError(e, { name: 'SMEMBERS', method: '$getLinks' });
|
|
240
223
|
}
|
|
241
224
|
});
|
|
242
225
|
}
|
|
@@ -10,6 +10,10 @@ import { KeyValue } from "@leyyo/common";
|
|
|
10
10
|
export interface RedisConnectionLike extends CacheConnectionLike<RedisExecOpt> {
|
|
11
11
|
/** @inheritDoc */
|
|
12
12
|
get props(): Readonly<RedisConnProps>;
|
|
13
|
+
/**
|
|
14
|
+
* Shortcut to {@link RedisConnProps#client}
|
|
15
|
+
* */
|
|
16
|
+
get client(): RedisClientType;
|
|
13
17
|
}
|
|
14
18
|
/**
|
|
15
19
|
* Redis direct connection interface
|
|
@@ -15,7 +15,7 @@ class RedisDirectConnection extends redis_connection_1.RedisConnection {
|
|
|
15
15
|
* */
|
|
16
16
|
constructor(opt) {
|
|
17
17
|
super(opt);
|
|
18
|
-
this.logger = be_base_common_1.logger.
|
|
18
|
+
this.logger = be_base_common_1.logger.create(`RedisConnection${(opt === null || opt === void 0 ? void 0 : opt.name) ? '#' + (opt === null || opt === void 0 ? void 0 : opt.name) : ''}`);
|
|
19
19
|
}
|
|
20
20
|
/** @inheritDoc */
|
|
21
21
|
newChannel(opt) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { RedisConnectionLike, RedisExecOpt, RedisConnProps, RedisConnOpt } from "./index.types";
|
|
2
2
|
import { CacheConnection } from "@samet-it/be-cache-common";
|
|
3
|
+
import type { RedisClientType } from "redis";
|
|
3
4
|
/**
|
|
4
5
|
* Redis connection abstract class
|
|
5
6
|
* */
|
|
@@ -32,6 +33,8 @@ export declare abstract class RedisConnection extends CacheConnection<RedisExecO
|
|
|
32
33
|
protected constructor(opt?: RedisConnOpt);
|
|
33
34
|
/** @inheritDoc */
|
|
34
35
|
get props(): Readonly<RedisConnProps>;
|
|
36
|
+
/** @inheritDoc */
|
|
37
|
+
get client(): RedisClientType;
|
|
35
38
|
/**
|
|
36
39
|
* Read credentials from environment
|
|
37
40
|
* */
|
|
@@ -59,16 +59,23 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
59
59
|
* */
|
|
60
60
|
constructor(opt) {
|
|
61
61
|
super(opt);
|
|
62
|
+
if (!this.logger) {
|
|
63
|
+
this.logger = be_base_common_1.logger.create(this);
|
|
64
|
+
}
|
|
62
65
|
this._props.tryCount = 0;
|
|
63
66
|
this._readFromEnv();
|
|
64
67
|
this._buildUrl();
|
|
65
68
|
}
|
|
66
|
-
// region getter
|
|
69
|
+
// region getter-shortcut
|
|
67
70
|
/** @inheritDoc */
|
|
68
71
|
get props() {
|
|
69
72
|
return this._props;
|
|
70
73
|
}
|
|
71
|
-
|
|
74
|
+
/** @inheritDoc */
|
|
75
|
+
get client() {
|
|
76
|
+
return this._props.client;
|
|
77
|
+
}
|
|
78
|
+
// endregion getter-shortcut
|
|
72
79
|
// region protected-method
|
|
73
80
|
/**
|
|
74
81
|
* Read credentials from environment
|
|
@@ -80,7 +87,7 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
80
87
|
if (props.envVariant) {
|
|
81
88
|
env = config_1.redisCommonConfig.configure.getVariation(props.envVariant).valueShort;
|
|
82
89
|
}
|
|
83
|
-
props.
|
|
90
|
+
props.isEnabled = (_a = props.isEnabled) !== null && _a !== void 0 ? _a : env.ENABLED;
|
|
84
91
|
props.protocol = (_b = props.protocol) !== null && _b !== void 0 ? _b : env.PROTOCOL;
|
|
85
92
|
props.host = (_c = props.host) !== null && _c !== void 0 ? _c : env.HOST;
|
|
86
93
|
props.port = (_d = props.port) !== null && _d !== void 0 ? _d : env.PORT;
|
|
@@ -126,25 +133,24 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
126
133
|
connect() {
|
|
127
134
|
return __awaiter(this, void 0, void 0, function* () {
|
|
128
135
|
const { _props: props } = this;
|
|
129
|
-
|
|
130
|
-
if (!enabled) {
|
|
136
|
+
if (!this.isEnabled) {
|
|
131
137
|
return false;
|
|
132
138
|
}
|
|
133
|
-
if (isConnected) {
|
|
139
|
+
if (this.isConnected) {
|
|
134
140
|
return true;
|
|
135
141
|
}
|
|
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));
|
|
142
|
+
if (this._props.tryCount > RedisConnection.TRY_COUNT) {
|
|
143
|
+
const err = new be_cache_common_1.CacheError('Maximum try county', { tryCount: this._props.tryCount });
|
|
144
|
+
this.logger.error(be_base_common_1.errorHandler.common.logText(err, 'connect', 'try', this._props.tryCount));
|
|
139
145
|
return false;
|
|
140
146
|
}
|
|
141
147
|
try {
|
|
142
|
-
props.client = redis.createClient({ url: producedUrl });
|
|
148
|
+
props.client = redis.createClient({ url: this._props.producedUrl });
|
|
143
149
|
props.client
|
|
144
150
|
.on("error", err => {
|
|
145
151
|
this.checkError(err, { silent: true, name: 'connection' });
|
|
146
152
|
props.tryCount++;
|
|
147
|
-
const old = isConnected;
|
|
153
|
+
const old = this.isConnected;
|
|
148
154
|
props.isConnected = false;
|
|
149
155
|
if (old) {
|
|
150
156
|
this._triggerOnCase('disconnected', this._onDisconnected, false);
|
|
@@ -166,7 +172,7 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
166
172
|
.on('reconnecting', () => this.logger.info('on[reconnecting]'))
|
|
167
173
|
.on('end', () => this.logger.info('on[end]'));
|
|
168
174
|
yield props.client.connect();
|
|
169
|
-
this.logger.
|
|
175
|
+
this.logger.info('Connected');
|
|
170
176
|
setTimeout(() => this.ping(true).then(), 30000);
|
|
171
177
|
}
|
|
172
178
|
catch (e) {
|
|
@@ -178,14 +184,13 @@ class RedisConnection extends be_cache_common_1.CacheConnection {
|
|
|
178
184
|
/** {@inheritDoc} */
|
|
179
185
|
ping(next) {
|
|
180
186
|
return __awaiter(this, void 0, void 0, function* () {
|
|
181
|
-
|
|
182
|
-
if (!enabled) {
|
|
187
|
+
if (!this.isEnabled) {
|
|
183
188
|
return false;
|
|
184
189
|
}
|
|
185
190
|
let result = false;
|
|
186
|
-
if (isConnected) {
|
|
191
|
+
if (this.isConnected) {
|
|
187
192
|
try {
|
|
188
|
-
yield client.ping();
|
|
193
|
+
yield this.client.ping();
|
|
189
194
|
result = true;
|
|
190
195
|
}
|
|
191
196
|
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.8",
|
|
4
4
|
"description": "Redis common component",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"redis",
|
|
@@ -69,8 +69,8 @@
|
|
|
69
69
|
"dependencies": {
|
|
70
70
|
"@leyyo/common": "^1.2.4",
|
|
71
71
|
"@leyyo/env": "^1.2.5",
|
|
72
|
-
"@samet-it/be-base-common": "^1.1.
|
|
73
|
-
"@samet-it/be-cache-common": "^1.1.
|
|
72
|
+
"@samet-it/be-base-common": "^1.1.5",
|
|
73
|
+
"@samet-it/be-cache-common": "^1.1.7",
|
|
74
74
|
"redis": "^5.10.0"
|
|
75
75
|
}
|
|
76
76
|
}
|