descript-redis-cache 4.0.2 → 4.0.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/README.md +13 -8
- package/build/index.d.ts +9 -5
- package/build/index.js +40 -21
- package/package.json +5 -5
package/README.md
CHANGED
@@ -5,15 +5,23 @@ Plugin to use Redis as a cache in Descript
|
|
5
5
|
|
6
6
|
```js
|
7
7
|
import de from 'descript';
|
8
|
-
import
|
8
|
+
import { Cache } from 'descript-redis-cache';
|
9
9
|
|
10
|
-
const redisCache = new
|
10
|
+
const redisCache = new Cache(options);
|
11
|
+
|
12
|
+
// subscribe to events if necessary
|
13
|
+
// @see https://github.com/luin/ioredis#connection-events
|
14
|
+
redisCache.getClient()
|
15
|
+
.on('reconnecting', () => {/* ... */})
|
16
|
+
.on('error', () => {/* ... */})
|
17
|
+
.on('close', () => {/* ... */})
|
18
|
+
.on('end', () => {/* ... */});
|
11
19
|
|
12
20
|
const myBlock = de.http({
|
13
21
|
block: { /* ... */ },
|
14
22
|
options: {
|
15
|
-
key: ({ params }) => '
|
16
|
-
cache:
|
23
|
+
key: ({ params }) => '_some_cache_key_by_params_',
|
24
|
+
cache: redisCache,
|
17
25
|
}
|
18
26
|
});
|
19
27
|
```
|
@@ -30,10 +38,7 @@ export interface Options {
|
|
30
38
|
generation?: number;
|
31
39
|
// read timeout in milliseconds (default: 100)
|
32
40
|
readTimeout?: number;
|
33
|
-
redis:
|
34
|
-
{ startupNodes: ClusterNode[], options?: ClusterOptions } |
|
35
|
-
{ options: RedisOptions }
|
36
|
-
);
|
41
|
+
redis: RedisOptions | { startupNodes: ClusterNode[], options?: ClusterOptions };
|
37
42
|
}
|
38
43
|
```
|
39
44
|
|
package/build/index.d.ts
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
import type { CacheInterface } from 'descript';
|
2
2
|
import type { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis';
|
3
|
+
import { Cluster, Redis } from 'ioredis';
|
3
4
|
export interface Options {
|
4
5
|
defaultKeyTTL?: number;
|
5
6
|
generation?: number;
|
6
7
|
readTimeout?: number;
|
7
|
-
redis:
|
8
|
+
redis: RedisOptions | {
|
8
9
|
startupNodes: ClusterNode[];
|
9
10
|
options?: ClusterOptions;
|
10
|
-
}
|
11
|
-
options: RedisOptions;
|
12
|
-
});
|
11
|
+
};
|
13
12
|
}
|
14
13
|
interface Logger {
|
15
14
|
log(event: LoggerEvent): void;
|
@@ -17,6 +16,9 @@ interface Logger {
|
|
17
16
|
export type LoggerEvent = ({
|
18
17
|
type: EVENT.REDIS_CACHE_INITIALIZED;
|
19
18
|
options: Options;
|
19
|
+
} | {
|
20
|
+
type: EVENT.REDIS_CACHE_ERROR;
|
21
|
+
error: Error;
|
20
22
|
} | {
|
21
23
|
type: EVENT.REDIS_CACHE_READ_START;
|
22
24
|
key: string;
|
@@ -108,6 +110,7 @@ export type LoggerEvent = ({
|
|
108
110
|
export declare class Cache<Result> implements CacheInterface<Result> {
|
109
111
|
#private;
|
110
112
|
constructor(options: Options, logger?: Logger);
|
113
|
+
getClient(): Cluster | Redis;
|
111
114
|
get({ key }: {
|
112
115
|
key: string;
|
113
116
|
}): Promise<Result | undefined>;
|
@@ -117,8 +120,9 @@ export declare class Cache<Result> implements CacheInterface<Result> {
|
|
117
120
|
maxage?: number;
|
118
121
|
}): Promise<void>;
|
119
122
|
}
|
120
|
-
export declare
|
123
|
+
export declare enum EVENT {
|
121
124
|
REDIS_CACHE_INITIALIZED = "REDIS_CACHE_INITIALIZED",
|
125
|
+
REDIS_CACHE_ERROR = "REDIS_CACHE_ERROR",
|
122
126
|
REDIS_CACHE_JSON_PARSING_FAILED = "REDIS_CACHE_JSON_PARSING_FAILED",
|
123
127
|
REDIS_CACHE_JSON_STRINGIFY_FAILED = "REDIS_CACHE_JSON_STRINGIFY_FAILED",
|
124
128
|
REDIS_CACHE_READ_DONE = "REDIS_CACHE_READ_DONE",
|
package/build/index.js
CHANGED
@@ -15,7 +15,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
15
|
};
|
16
16
|
var _Cache_instances, _Cache_client, _Cache_logger, _Cache_options, _Cache_normalizeKey, _Cache_log;
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
18
|
-
exports.Cache = void 0;
|
18
|
+
exports.EVENT = exports.Cache = void 0;
|
19
19
|
const node_crypto_1 = require("node:crypto");
|
20
20
|
const descript_1 = __importDefault(require("descript"));
|
21
21
|
const ioredis_1 = require("ioredis");
|
@@ -36,18 +36,21 @@ class Cache {
|
|
36
36
|
__classPrivateFieldSet(this, _Cache_client, new ioredis_1.Cluster(__classPrivateFieldGet(this, _Cache_options, "f").redis.startupNodes, __classPrivateFieldGet(this, _Cache_options, "f").redis.options), "f");
|
37
37
|
}
|
38
38
|
else {
|
39
|
-
__classPrivateFieldSet(this, _Cache_client, new ioredis_1.Redis(__classPrivateFieldGet(this, _Cache_options, "f").redis
|
39
|
+
__classPrivateFieldSet(this, _Cache_client, new ioredis_1.Redis(__classPrivateFieldGet(this, _Cache_options, "f").redis), "f");
|
40
40
|
}
|
41
41
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
42
|
-
type:
|
42
|
+
type: EVENT.REDIS_CACHE_INITIALIZED,
|
43
43
|
options: { ...__classPrivateFieldGet(this, _Cache_options, "f") },
|
44
44
|
});
|
45
45
|
}
|
46
|
+
getClient() {
|
47
|
+
return __classPrivateFieldGet(this, _Cache_client, "f");
|
48
|
+
}
|
46
49
|
get({ key }) {
|
47
50
|
const normalizedKey = __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_normalizeKey).call(this, key);
|
48
51
|
return new Promise((resolve, reject) => {
|
49
52
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
50
|
-
type:
|
53
|
+
type: EVENT.REDIS_CACHE_READ_START,
|
51
54
|
key,
|
52
55
|
normalizedKey,
|
53
56
|
});
|
@@ -59,7 +62,7 @@ class Cache {
|
|
59
62
|
const networkTimer = process.hrtime(networkTimerStart);
|
60
63
|
const totalTimer = process.hrtime(totalTimerStart);
|
61
64
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
62
|
-
type:
|
65
|
+
type: EVENT.REDIS_CACHE_READ_TIMEOUT,
|
63
66
|
key,
|
64
67
|
normalizedKey,
|
65
68
|
timers: {
|
@@ -68,7 +71,7 @@ class Cache {
|
|
68
71
|
},
|
69
72
|
});
|
70
73
|
reject(descript_1.default.error({
|
71
|
-
id:
|
74
|
+
id: EVENT.REDIS_CACHE_READ_TIMEOUT,
|
72
75
|
}));
|
73
76
|
}, __classPrivateFieldGet(this, _Cache_options, "f").readTimeout);
|
74
77
|
__classPrivateFieldGet(this, _Cache_client, "f").get(normalizedKey, (error, data) => {
|
@@ -80,7 +83,7 @@ class Cache {
|
|
80
83
|
if (error) {
|
81
84
|
const totalTimer = process.hrtime(totalTimerStart);
|
82
85
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
83
|
-
type:
|
86
|
+
type: EVENT.REDIS_CACHE_READ_ERROR,
|
84
87
|
error,
|
85
88
|
key,
|
86
89
|
normalizedKey,
|
@@ -90,13 +93,13 @@ class Cache {
|
|
90
93
|
},
|
91
94
|
});
|
92
95
|
reject(descript_1.default.error({
|
93
|
-
id:
|
96
|
+
id: EVENT.REDIS_CACHE_READ_ERROR,
|
94
97
|
}));
|
95
98
|
}
|
96
99
|
else if (!data) {
|
97
100
|
const totalTimer = process.hrtime(totalTimerStart);
|
98
101
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
99
|
-
type:
|
102
|
+
type: EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND,
|
100
103
|
key,
|
101
104
|
normalizedKey,
|
102
105
|
timers: {
|
@@ -105,7 +108,7 @@ class Cache {
|
|
105
108
|
},
|
106
109
|
});
|
107
110
|
reject(descript_1.default.error({
|
108
|
-
id:
|
111
|
+
id: EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND,
|
109
112
|
}));
|
110
113
|
}
|
111
114
|
else {
|
@@ -116,7 +119,7 @@ class Cache {
|
|
116
119
|
catch (error) {
|
117
120
|
const totalTimer = process.hrtime(totalTimerStart);
|
118
121
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
119
|
-
type:
|
122
|
+
type: EVENT.REDIS_CACHE_JSON_PARSING_FAILED,
|
120
123
|
data,
|
121
124
|
error,
|
122
125
|
key,
|
@@ -127,13 +130,13 @@ class Cache {
|
|
127
130
|
},
|
128
131
|
});
|
129
132
|
reject(descript_1.default.error({
|
130
|
-
id:
|
133
|
+
id: EVENT.REDIS_CACHE_JSON_PARSING_FAILED,
|
131
134
|
}));
|
132
135
|
return;
|
133
136
|
}
|
134
137
|
const totalTimer = process.hrtime(totalTimerStart);
|
135
138
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
136
|
-
type:
|
139
|
+
type: EVENT.REDIS_CACHE_READ_DONE,
|
137
140
|
data,
|
138
141
|
key,
|
139
142
|
normalizedKey,
|
@@ -155,7 +158,7 @@ class Cache {
|
|
155
158
|
const normalizedKey = __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_normalizeKey).call(this, key);
|
156
159
|
return new Promise((resolve, reject) => {
|
157
160
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
158
|
-
type:
|
161
|
+
type: EVENT.REDIS_CACHE_WRITE_START,
|
159
162
|
key,
|
160
163
|
normalizedKey,
|
161
164
|
});
|
@@ -166,7 +169,7 @@ class Cache {
|
|
166
169
|
catch (error) {
|
167
170
|
const totalTimer = process.hrtime(totalTimerStart);
|
168
171
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
169
|
-
type:
|
172
|
+
type: EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED,
|
170
173
|
data: value,
|
171
174
|
error,
|
172
175
|
key,
|
@@ -176,7 +179,7 @@ class Cache {
|
|
176
179
|
},
|
177
180
|
});
|
178
181
|
reject(descript_1.default.error({
|
179
|
-
id:
|
182
|
+
id: EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED,
|
180
183
|
}));
|
181
184
|
return;
|
182
185
|
}
|
@@ -187,7 +190,7 @@ class Cache {
|
|
187
190
|
const totalTimer = process.hrtime(totalTimerStart);
|
188
191
|
if (error) {
|
189
192
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
190
|
-
type:
|
193
|
+
type: EVENT.REDIS_CACHE_WRITE_ERROR,
|
191
194
|
error,
|
192
195
|
key,
|
193
196
|
normalizedKey,
|
@@ -197,12 +200,12 @@ class Cache {
|
|
197
200
|
},
|
198
201
|
});
|
199
202
|
reject(descript_1.default.error({
|
200
|
-
id:
|
203
|
+
id: EVENT.REDIS_CACHE_WRITE_ERROR,
|
201
204
|
}));
|
202
205
|
}
|
203
206
|
else if (!done) {
|
204
207
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
205
|
-
type:
|
208
|
+
type: EVENT.REDIS_CACHE_WRITE_FAILED,
|
206
209
|
key,
|
207
210
|
normalizedKey,
|
208
211
|
timers: {
|
@@ -211,12 +214,12 @@ class Cache {
|
|
211
214
|
},
|
212
215
|
});
|
213
216
|
reject(descript_1.default.error({
|
214
|
-
id:
|
217
|
+
id: EVENT.REDIS_CACHE_WRITE_FAILED,
|
215
218
|
}));
|
216
219
|
}
|
217
220
|
else {
|
218
221
|
__classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
|
219
|
-
type:
|
222
|
+
type: EVENT.REDIS_CACHE_WRITE_DONE,
|
220
223
|
data: json,
|
221
224
|
key,
|
222
225
|
normalizedKey,
|
@@ -240,3 +243,19 @@ _Cache_client = new WeakMap(), _Cache_logger = new WeakMap(), _Cache_options = n
|
|
240
243
|
__classPrivateFieldGet(this, _Cache_logger, "f").log(event);
|
241
244
|
}
|
242
245
|
};
|
246
|
+
var EVENT;
|
247
|
+
(function (EVENT) {
|
248
|
+
EVENT["REDIS_CACHE_INITIALIZED"] = "REDIS_CACHE_INITIALIZED";
|
249
|
+
EVENT["REDIS_CACHE_ERROR"] = "REDIS_CACHE_ERROR";
|
250
|
+
EVENT["REDIS_CACHE_JSON_PARSING_FAILED"] = "REDIS_CACHE_JSON_PARSING_FAILED";
|
251
|
+
EVENT["REDIS_CACHE_JSON_STRINGIFY_FAILED"] = "REDIS_CACHE_JSON_STRINGIFY_FAILED";
|
252
|
+
EVENT["REDIS_CACHE_READ_DONE"] = "REDIS_CACHE_READ_DONE";
|
253
|
+
EVENT["REDIS_CACHE_READ_ERROR"] = "REDIS_CACHE_READ_ERROR";
|
254
|
+
EVENT["REDIS_CACHE_READ_KEY_NOT_FOUND"] = "REDIS_CACHE_READ_KEY_NOT_FOUND";
|
255
|
+
EVENT["REDIS_CACHE_READ_START"] = "REDIS_CACHE_READ_START";
|
256
|
+
EVENT["REDIS_CACHE_READ_TIMEOUT"] = "REDIS_CACHE_READ_TIMEOUT";
|
257
|
+
EVENT["REDIS_CACHE_WRITE_DONE"] = "REDIS_CACHE_WRITE_DONE";
|
258
|
+
EVENT["REDIS_CACHE_WRITE_ERROR"] = "REDIS_CACHE_WRITE_ERROR";
|
259
|
+
EVENT["REDIS_CACHE_WRITE_FAILED"] = "REDIS_CACHE_WRITE_FAILED";
|
260
|
+
EVENT["REDIS_CACHE_WRITE_START"] = "REDIS_CACHE_WRITE_START";
|
261
|
+
})(EVENT || (exports.EVENT = EVENT = {}));
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "descript-redis-cache",
|
3
|
-
"version": "4.0.
|
3
|
+
"version": "4.0.4",
|
4
4
|
"description": "plugin for descript to use redis as cache",
|
5
5
|
"main": "./build/index.js",
|
6
6
|
"types": "./build/index.d.ts",
|
@@ -40,11 +40,11 @@
|
|
40
40
|
"@eslint/js": "^9.14.0",
|
41
41
|
"@stylistic/eslint-plugin-ts": "^2.11.0",
|
42
42
|
"@types/eslint__js": "^8.42.3",
|
43
|
-
"@types/node": "^22.
|
43
|
+
"@types/node": "^22.10.0",
|
44
44
|
"descript": "^4.0.5",
|
45
45
|
"eslint": "^9.15.0",
|
46
|
-
"typescript": "^5.
|
47
|
-
"typescript-eslint": "^8.
|
48
|
-
"vitest": "^2.1.
|
46
|
+
"typescript": "^5.7.2",
|
47
|
+
"typescript-eslint": "^8.16.0",
|
48
|
+
"vitest": "^2.1.6"
|
49
49
|
}
|
50
50
|
}
|