descript-redis-cache 4.0.7 → 4.1.1
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/build/index.d.ts +18 -13
- package/build/index.js +41 -20
- package/package.json +9 -9
package/build/index.d.ts
CHANGED
@@ -8,6 +8,8 @@ export interface Options {
|
|
8
8
|
generation?: number;
|
9
9
|
/** read timeout in milliseconds (default: 100) */
|
10
10
|
readTimeout?: number;
|
11
|
+
/** use two clients (reader and writer) with Sentinel (default: false) */
|
12
|
+
useReaderAndWriterWithSentinel?: boolean;
|
11
13
|
/** redis config */
|
12
14
|
redis: RedisOptions | {
|
13
15
|
startupNodes: ClusterNode[];
|
@@ -20,65 +22,65 @@ interface Timers {
|
|
20
22
|
end: number;
|
21
23
|
}
|
22
24
|
export type LoggerEvent = ({
|
23
|
-
type: EVENT.REDIS_CACHE_INITIALIZED;
|
25
|
+
'type': EVENT.REDIS_CACHE_INITIALIZED;
|
24
26
|
options: Options;
|
25
27
|
} | {
|
26
|
-
type: EVENT.REDIS_CACHE_READ_START;
|
28
|
+
'type': EVENT.REDIS_CACHE_READ_START;
|
27
29
|
key: string;
|
28
30
|
normalizedKey: string;
|
29
31
|
} | {
|
30
|
-
type: EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND;
|
32
|
+
'type': EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND;
|
31
33
|
key: string;
|
32
34
|
normalizedKey: string;
|
33
35
|
timers: Timers;
|
34
36
|
} | {
|
35
|
-
type: EVENT.REDIS_CACHE_READ_TIMEOUT;
|
37
|
+
'type': EVENT.REDIS_CACHE_READ_TIMEOUT;
|
36
38
|
key: string;
|
37
39
|
normalizedKey: string;
|
38
40
|
timers: Timers;
|
39
41
|
} | {
|
40
|
-
type: EVENT.REDIS_CACHE_READ_ERROR;
|
42
|
+
'type': EVENT.REDIS_CACHE_READ_ERROR;
|
41
43
|
error: Error;
|
42
44
|
key: string;
|
43
45
|
normalizedKey: string;
|
44
46
|
timers: Timers;
|
45
47
|
} | {
|
46
|
-
type: EVENT.REDIS_CACHE_JSON_PARSING_FAILED;
|
48
|
+
'type': EVENT.REDIS_CACHE_JSON_PARSING_FAILED;
|
47
49
|
data: unknown;
|
48
50
|
error: unknown;
|
49
51
|
key: string;
|
50
52
|
normalizedKey: string;
|
51
53
|
timers: Timers;
|
52
54
|
} | {
|
53
|
-
type: EVENT.REDIS_CACHE_READ_DONE;
|
55
|
+
'type': EVENT.REDIS_CACHE_READ_DONE;
|
54
56
|
data: unknown;
|
55
57
|
key: string;
|
56
58
|
normalizedKey: string;
|
57
59
|
timers: Timers;
|
58
60
|
} | {
|
59
|
-
type: EVENT.REDIS_CACHE_WRITE_START;
|
61
|
+
'type': EVENT.REDIS_CACHE_WRITE_START;
|
60
62
|
key: string;
|
61
63
|
normalizedKey: string;
|
62
64
|
} | {
|
63
|
-
type: EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED;
|
65
|
+
'type': EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED;
|
64
66
|
data: unknown;
|
65
67
|
error: unknown;
|
66
68
|
key: string;
|
67
69
|
normalizedKey: string;
|
68
70
|
timers: Timers;
|
69
71
|
} | {
|
70
|
-
type: EVENT.REDIS_CACHE_WRITE_ERROR;
|
72
|
+
'type': EVENT.REDIS_CACHE_WRITE_ERROR;
|
71
73
|
error: Error;
|
72
74
|
key: string;
|
73
75
|
normalizedKey: string;
|
74
76
|
timers: Timers;
|
75
77
|
} | {
|
76
|
-
type: EVENT.REDIS_CACHE_WRITE_FAILED;
|
78
|
+
'type': EVENT.REDIS_CACHE_WRITE_FAILED;
|
77
79
|
key: string;
|
78
80
|
normalizedKey: string;
|
79
81
|
timers: Timers;
|
80
82
|
} | {
|
81
|
-
type: EVENT.REDIS_CACHE_WRITE_DONE;
|
83
|
+
'type': EVENT.REDIS_CACHE_WRITE_DONE;
|
82
84
|
data: string;
|
83
85
|
key: string;
|
84
86
|
normalizedKey: string;
|
@@ -87,7 +89,10 @@ export type LoggerEvent = ({
|
|
87
89
|
export declare class Cache<Result> implements CacheInterface<Result> {
|
88
90
|
#private;
|
89
91
|
constructor(options: Options, logger?: Logger);
|
90
|
-
getClient():
|
92
|
+
getClient(): {
|
93
|
+
reader: Cluster | Redis;
|
94
|
+
writer: Cluster | Redis;
|
95
|
+
};
|
91
96
|
get({ key }: {
|
92
97
|
key: string;
|
93
98
|
}): Promise<Result | undefined>;
|
package/build/index.js
CHANGED
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
10
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
12
12
|
};
|
13
|
-
var _Cache_instances,
|
13
|
+
var _Cache_instances, _Cache_writer, _Cache_reader, _Cache_logger, _Cache_options, _Cache_normalizeKey, _Cache_log;
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
15
15
|
exports.EVENT = exports.Cache = void 0;
|
16
16
|
const node_crypto_1 = require("node:crypto");
|
@@ -19,7 +19,8 @@ const ioredis_1 = require("ioredis");
|
|
19
19
|
class Cache {
|
20
20
|
constructor(options, logger) {
|
21
21
|
_Cache_instances.add(this);
|
22
|
-
|
22
|
+
_Cache_writer.set(this, void 0);
|
23
|
+
_Cache_reader.set(this, void 0);
|
23
24
|
_Cache_logger.set(this, void 0);
|
24
25
|
_Cache_options.set(this, void 0);
|
25
26
|
__classPrivateFieldSet(this, _Cache_options, {
|
@@ -30,24 +31,44 @@ class Cache {
|
|
30
31
|
}, "f");
|
31
32
|
__classPrivateFieldSet(this, _Cache_logger, logger, "f");
|
32
33
|
if ('startupNodes' in __classPrivateFieldGet(this, _Cache_options, "f").redis) {
|
33
|
-
__classPrivateFieldSet(this,
|
34
|
+
__classPrivateFieldSet(this, _Cache_reader, new ioredis_1.Cluster(__classPrivateFieldGet(this, _Cache_options, "f").redis.startupNodes, __classPrivateFieldGet(this, _Cache_options, "f").redis.options), "f");
|
35
|
+
__classPrivateFieldSet(this, _Cache_writer, __classPrivateFieldGet(this, _Cache_reader, "f"), "f");
|
34
36
|
}
|
35
37
|
else {
|
36
|
-
|
38
|
+
if (__classPrivateFieldGet(this, _Cache_options, "f").useReaderAndWriterWithSentinel) {
|
39
|
+
// Client for write (always on master)
|
40
|
+
__classPrivateFieldSet(this, _Cache_writer, new ioredis_1.Redis({
|
41
|
+
...__classPrivateFieldGet(this, _Cache_options, "f").redis,
|
42
|
+
role: 'master',
|
43
|
+
}), "f");
|
44
|
+
// Client for read (replica, only read-only commands)
|
45
|
+
__classPrivateFieldSet(this, _Cache_reader, new ioredis_1.Redis({
|
46
|
+
...__classPrivateFieldGet(this, _Cache_options, "f").redis,
|
47
|
+
role: 'slave',
|
48
|
+
readOnly: true,
|
49
|
+
}), "f");
|
50
|
+
}
|
51
|
+
else {
|
52
|
+
__classPrivateFieldSet(this, _Cache_reader, new ioredis_1.Redis(__classPrivateFieldGet(this, _Cache_options, "f").redis), "f");
|
53
|
+
__classPrivateFieldSet(this, _Cache_writer, __classPrivateFieldGet(this, _Cache_reader, "f"), "f");
|
54
|
+
}
|
37
55
|
}
|
38
56
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
39
|
-
type: EVENT.REDIS_CACHE_INITIALIZED,
|
57
|
+
'type': EVENT.REDIS_CACHE_INITIALIZED,
|
40
58
|
options: { ...__classPrivateFieldGet(this, _Cache_options, "f") },
|
41
59
|
});
|
42
60
|
}
|
43
61
|
getClient() {
|
44
|
-
return
|
62
|
+
return {
|
63
|
+
reader: __classPrivateFieldGet(this, _Cache_reader, "f"),
|
64
|
+
writer: __classPrivateFieldGet(this, _Cache_writer, "f"),
|
65
|
+
};
|
45
66
|
}
|
46
67
|
get({ key }) {
|
47
68
|
const normalizedKey = __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_normalizeKey).call(this, key);
|
48
69
|
return new Promise((resolve, reject) => {
|
49
70
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
50
|
-
type: EVENT.REDIS_CACHE_READ_START,
|
71
|
+
'type': EVENT.REDIS_CACHE_READ_START,
|
51
72
|
key,
|
52
73
|
normalizedKey,
|
53
74
|
});
|
@@ -56,7 +77,7 @@ class Cache {
|
|
56
77
|
const timer = setTimeout(() => {
|
57
78
|
isTimeout = true;
|
58
79
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
59
|
-
type: EVENT.REDIS_CACHE_READ_TIMEOUT,
|
80
|
+
'type': EVENT.REDIS_CACHE_READ_TIMEOUT,
|
60
81
|
key,
|
61
82
|
normalizedKey,
|
62
83
|
timers: {
|
@@ -68,14 +89,14 @@ class Cache {
|
|
68
89
|
id: EVENT.REDIS_CACHE_READ_TIMEOUT,
|
69
90
|
}));
|
70
91
|
}, __classPrivateFieldGet(this, _Cache_options, "f").readTimeout);
|
71
|
-
__classPrivateFieldGet(this,
|
92
|
+
__classPrivateFieldGet(this, _Cache_reader, "f").get(normalizedKey, (error, data) => {
|
72
93
|
if (isTimeout) {
|
73
94
|
return;
|
74
95
|
}
|
75
96
|
clearTimeout(timer);
|
76
97
|
if (error) {
|
77
98
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
78
|
-
type: EVENT.REDIS_CACHE_READ_ERROR,
|
99
|
+
'type': EVENT.REDIS_CACHE_READ_ERROR,
|
79
100
|
error,
|
80
101
|
key,
|
81
102
|
normalizedKey,
|
@@ -90,7 +111,7 @@ class Cache {
|
|
90
111
|
}
|
91
112
|
else if (!data) {
|
92
113
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
93
|
-
type: EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND,
|
114
|
+
'type': EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND,
|
94
115
|
key,
|
95
116
|
normalizedKey,
|
96
117
|
timers: {
|
@@ -109,7 +130,7 @@ class Cache {
|
|
109
130
|
}
|
110
131
|
catch (error) {
|
111
132
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
112
|
-
type: EVENT.REDIS_CACHE_JSON_PARSING_FAILED,
|
133
|
+
'type': EVENT.REDIS_CACHE_JSON_PARSING_FAILED,
|
113
134
|
data,
|
114
135
|
error,
|
115
136
|
key,
|
@@ -125,7 +146,7 @@ class Cache {
|
|
125
146
|
return;
|
126
147
|
}
|
127
148
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
128
|
-
type: EVENT.REDIS_CACHE_READ_DONE,
|
149
|
+
'type': EVENT.REDIS_CACHE_READ_DONE,
|
129
150
|
data,
|
130
151
|
key,
|
131
152
|
normalizedKey,
|
@@ -147,7 +168,7 @@ class Cache {
|
|
147
168
|
const normalizedKey = __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_normalizeKey).call(this, key);
|
148
169
|
return new Promise((resolve, reject) => {
|
149
170
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
150
|
-
type: EVENT.REDIS_CACHE_WRITE_START,
|
171
|
+
'type': EVENT.REDIS_CACHE_WRITE_START,
|
151
172
|
key,
|
152
173
|
normalizedKey,
|
153
174
|
});
|
@@ -157,7 +178,7 @@ class Cache {
|
|
157
178
|
}
|
158
179
|
catch (error) {
|
159
180
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
160
|
-
type: EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED,
|
181
|
+
'type': EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED,
|
161
182
|
data: value,
|
162
183
|
error,
|
163
184
|
key,
|
@@ -173,10 +194,10 @@ class Cache {
|
|
173
194
|
return;
|
174
195
|
}
|
175
196
|
// maxage - seconds
|
176
|
-
__classPrivateFieldGet(this,
|
197
|
+
__classPrivateFieldGet(this, _Cache_writer, "f").set(normalizedKey, json, 'EX', maxage, (error, done) => {
|
177
198
|
if (error) {
|
178
199
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
179
|
-
type: EVENT.REDIS_CACHE_WRITE_ERROR,
|
200
|
+
'type': EVENT.REDIS_CACHE_WRITE_ERROR,
|
180
201
|
error,
|
181
202
|
key,
|
182
203
|
normalizedKey,
|
@@ -191,7 +212,7 @@ class Cache {
|
|
191
212
|
}
|
192
213
|
else if (!done) {
|
193
214
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
194
|
-
type: EVENT.REDIS_CACHE_WRITE_FAILED,
|
215
|
+
'type': EVENT.REDIS_CACHE_WRITE_FAILED,
|
195
216
|
key,
|
196
217
|
normalizedKey,
|
197
218
|
timers: {
|
@@ -205,7 +226,7 @@ class Cache {
|
|
205
226
|
}
|
206
227
|
else {
|
207
228
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
208
|
-
type: EVENT.REDIS_CACHE_WRITE_DONE,
|
229
|
+
'type': EVENT.REDIS_CACHE_WRITE_DONE,
|
209
230
|
data: json,
|
210
231
|
key,
|
211
232
|
normalizedKey,
|
@@ -221,7 +242,7 @@ class Cache {
|
|
221
242
|
}
|
222
243
|
}
|
223
244
|
exports.Cache = Cache;
|
224
|
-
|
245
|
+
_Cache_writer = new WeakMap(), _Cache_reader = new WeakMap(), _Cache_logger = new WeakMap(), _Cache_options = new WeakMap(), _Cache_instances = new WeakSet(), _Cache_normalizeKey = function _Cache_normalizeKey(key) {
|
225
246
|
const value = `g${__classPrivateFieldGet(this, _Cache_options, "f").generation}:${key}`;
|
226
247
|
return (0, node_crypto_1.hash)('sha512', value);
|
227
248
|
}, _Cache_log = function _Cache_log(event) {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "descript-redis-cache",
|
3
|
-
"version": "4.
|
3
|
+
"version": "4.1.1",
|
4
4
|
"description": "plugin for descript to use redis as cache",
|
5
5
|
"main": "./build/index.js",
|
6
6
|
"types": "./build/index.d.ts",
|
@@ -31,20 +31,20 @@
|
|
31
31
|
"build"
|
32
32
|
],
|
33
33
|
"dependencies": {
|
34
|
-
"
|
34
|
+
"@stylistic/eslint-plugin": "^5.3.1",
|
35
|
+
"ioredis": "^5.7.0"
|
35
36
|
},
|
36
37
|
"peerDependencies": {
|
37
38
|
"descript": ">=4"
|
38
39
|
},
|
39
40
|
"devDependencies": {
|
40
41
|
"@eslint/js": "^9.14.0",
|
41
|
-
"@stylistic/eslint-plugin-ts": "^2.11.0",
|
42
42
|
"@types/eslint__js": "^8.42.3",
|
43
|
-
"@types/node": "^22.
|
44
|
-
"descript": "^4.0.
|
45
|
-
"eslint": "^9.
|
46
|
-
"typescript": "^5.
|
47
|
-
"typescript-eslint": "^8.
|
48
|
-
"vitest": "^2.
|
43
|
+
"@types/node": "^22.18.1",
|
44
|
+
"descript": "^4.0.16",
|
45
|
+
"eslint": "^9.35.0",
|
46
|
+
"typescript": "^5.9.2",
|
47
|
+
"typescript-eslint": "^8.42.0",
|
48
|
+
"vitest": "^3.2.4"
|
49
49
|
}
|
50
50
|
}
|