descript-redis-cache 4.0.5 → 4.0.7

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 CHANGED
@@ -1,24 +1,27 @@
1
- import type { CacheInterface } from 'descript';
1
+ import type { CacheInterface, LoggerInterface } from 'descript';
2
2
  import type { ClusterNode, ClusterOptions, RedisOptions } from 'ioredis';
3
3
  import { Cluster, Redis } from 'ioredis';
4
4
  export interface Options {
5
+ /** key TTL in seconds (default: 60 * 60 * 24) */
5
6
  defaultKeyTTL?: number;
7
+ /** increment generation to invalidate all key across breaking changes releases (default: 1) */
6
8
  generation?: number;
9
+ /** read timeout in milliseconds (default: 100) */
7
10
  readTimeout?: number;
11
+ /** redis config */
8
12
  redis: RedisOptions | {
9
13
  startupNodes: ClusterNode[];
10
14
  options?: ClusterOptions;
11
15
  };
12
16
  }
13
- interface Logger {
14
- log(event: LoggerEvent): void;
17
+ type Logger = LoggerInterface<LoggerEvent>;
18
+ interface Timers {
19
+ start: number;
20
+ end: number;
15
21
  }
16
22
  export type LoggerEvent = ({
17
23
  type: EVENT.REDIS_CACHE_INITIALIZED;
18
24
  options: Options;
19
- } | {
20
- type: EVENT.REDIS_CACHE_ERROR;
21
- error: Error;
22
25
  } | {
23
26
  type: EVENT.REDIS_CACHE_READ_START;
24
27
  key: string;
@@ -27,46 +30,31 @@ export type LoggerEvent = ({
27
30
  type: EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND;
28
31
  key: string;
29
32
  normalizedKey: string;
30
- timers: {
31
- network: [number, number];
32
- total: [number, number];
33
- };
33
+ timers: Timers;
34
34
  } | {
35
35
  type: EVENT.REDIS_CACHE_READ_TIMEOUT;
36
36
  key: string;
37
37
  normalizedKey: string;
38
- timers: {
39
- network: [number, number];
40
- total: [number, number];
41
- };
38
+ timers: Timers;
42
39
  } | {
43
40
  type: EVENT.REDIS_CACHE_READ_ERROR;
44
41
  error: Error;
45
42
  key: string;
46
43
  normalizedKey: string;
47
- timers: {
48
- network: [number, number];
49
- total: [number, number];
50
- };
44
+ timers: Timers;
51
45
  } | {
52
46
  type: EVENT.REDIS_CACHE_JSON_PARSING_FAILED;
53
47
  data: unknown;
54
48
  error: unknown;
55
49
  key: string;
56
50
  normalizedKey: string;
57
- timers: {
58
- network: [number, number];
59
- total: [number, number];
60
- };
51
+ timers: Timers;
61
52
  } | {
62
53
  type: EVENT.REDIS_CACHE_READ_DONE;
63
54
  data: unknown;
64
55
  key: string;
65
56
  normalizedKey: string;
66
- timers: {
67
- network: [number, number];
68
- total: [number, number];
69
- };
57
+ timers: Timers;
70
58
  } | {
71
59
  type: EVENT.REDIS_CACHE_WRITE_START;
72
60
  key: string;
@@ -77,35 +65,24 @@ export type LoggerEvent = ({
77
65
  error: unknown;
78
66
  key: string;
79
67
  normalizedKey: string;
80
- timers: {
81
- total: [number, number];
82
- };
68
+ timers: Timers;
83
69
  } | {
84
70
  type: EVENT.REDIS_CACHE_WRITE_ERROR;
85
- error: unknown;
71
+ error: Error;
86
72
  key: string;
87
73
  normalizedKey: string;
88
- timers: {
89
- network: [number, number];
90
- total: [number, number];
91
- };
74
+ timers: Timers;
92
75
  } | {
93
76
  type: EVENT.REDIS_CACHE_WRITE_FAILED;
94
77
  key: string;
95
78
  normalizedKey: string;
96
- timers: {
97
- network: [number, number];
98
- total: [number, number];
99
- };
79
+ timers: Timers;
100
80
  } | {
101
81
  type: EVENT.REDIS_CACHE_WRITE_DONE;
102
82
  data: string;
103
83
  key: string;
104
84
  normalizedKey: string;
105
- timers: {
106
- network: [number, number];
107
- total: [number, number];
108
- };
85
+ timers: Timers;
109
86
  });
110
87
  export declare class Cache<Result> implements CacheInterface<Result> {
111
88
  #private;
@@ -122,7 +99,6 @@ export declare class Cache<Result> implements CacheInterface<Result> {
122
99
  }
123
100
  export declare enum EVENT {
124
101
  REDIS_CACHE_INITIALIZED = "REDIS_CACHE_INITIALIZED",
125
- REDIS_CACHE_ERROR = "REDIS_CACHE_ERROR",
126
102
  REDIS_CACHE_JSON_PARSING_FAILED = "REDIS_CACHE_JSON_PARSING_FAILED",
127
103
  REDIS_CACHE_JSON_STRINGIFY_FAILED = "REDIS_CACHE_JSON_STRINGIFY_FAILED",
128
104
  REDIS_CACHE_READ_DONE = "REDIS_CACHE_READ_DONE",
package/build/index.js CHANGED
@@ -51,20 +51,17 @@ class Cache {
51
51
  key,
52
52
  normalizedKey,
53
53
  });
54
- const networkTimerStart = process.hrtime();
55
- const totalTimerStart = process.hrtime();
54
+ const start = Date.now();
56
55
  let isTimeout = false;
57
56
  const timer = setTimeout(() => {
58
57
  isTimeout = true;
59
- const networkTimer = process.hrtime(networkTimerStart);
60
- const totalTimer = process.hrtime(totalTimerStart);
61
58
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
62
59
  type: EVENT.REDIS_CACHE_READ_TIMEOUT,
63
60
  key,
64
61
  normalizedKey,
65
62
  timers: {
66
- network: networkTimer,
67
- total: totalTimer,
63
+ start,
64
+ end: Date.now(),
68
65
  },
69
66
  });
70
67
  reject((0, descript_1.error)({
@@ -75,18 +72,16 @@ class Cache {
75
72
  if (isTimeout) {
76
73
  return;
77
74
  }
78
- const networkTimer = process.hrtime(networkTimerStart);
79
75
  clearTimeout(timer);
80
76
  if (error) {
81
- const totalTimer = process.hrtime(totalTimerStart);
82
77
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
83
78
  type: EVENT.REDIS_CACHE_READ_ERROR,
84
79
  error,
85
80
  key,
86
81
  normalizedKey,
87
82
  timers: {
88
- network: networkTimer,
89
- total: totalTimer,
83
+ start,
84
+ end: Date.now(),
90
85
  },
91
86
  });
92
87
  reject((0, descript_1.error)({
@@ -94,14 +89,13 @@ class Cache {
94
89
  }));
95
90
  }
96
91
  else if (!data) {
97
- const totalTimer = process.hrtime(totalTimerStart);
98
92
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
99
93
  type: EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND,
100
94
  key,
101
95
  normalizedKey,
102
96
  timers: {
103
- network: networkTimer,
104
- total: totalTimer,
97
+ start,
98
+ end: Date.now(),
105
99
  },
106
100
  });
107
101
  reject((0, descript_1.error)({
@@ -114,7 +108,6 @@ class Cache {
114
108
  parsedValue = JSON.parse(data);
115
109
  }
116
110
  catch (error) {
117
- const totalTimer = process.hrtime(totalTimerStart);
118
111
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
119
112
  type: EVENT.REDIS_CACHE_JSON_PARSING_FAILED,
120
113
  data,
@@ -122,8 +115,8 @@ class Cache {
122
115
  key,
123
116
  normalizedKey,
124
117
  timers: {
125
- network: networkTimer,
126
- total: totalTimer,
118
+ start,
119
+ end: Date.now(),
127
120
  },
128
121
  });
129
122
  reject((0, descript_1.error)({
@@ -131,15 +124,14 @@ class Cache {
131
124
  }));
132
125
  return;
133
126
  }
134
- const totalTimer = process.hrtime(totalTimerStart);
135
127
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
136
128
  type: EVENT.REDIS_CACHE_READ_DONE,
137
129
  data,
138
130
  key,
139
131
  normalizedKey,
140
132
  timers: {
141
- network: networkTimer,
142
- total: totalTimer,
133
+ start,
134
+ end: Date.now(),
143
135
  },
144
136
  });
145
137
  resolve(parsedValue);
@@ -151,7 +143,7 @@ class Cache {
151
143
  if (typeof value === 'undefined') {
152
144
  return Promise.resolve();
153
145
  }
154
- const totalTimerStart = process.hrtime();
146
+ const start = Date.now();
155
147
  const normalizedKey = __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_normalizeKey).call(this, key);
156
148
  return new Promise((resolve, reject) => {
157
149
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
@@ -164,7 +156,6 @@ class Cache {
164
156
  json = JSON.stringify(value);
165
157
  }
166
158
  catch (error) {
167
- const totalTimer = process.hrtime(totalTimerStart);
168
159
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
169
160
  type: EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED,
170
161
  data: value,
@@ -172,7 +163,8 @@ class Cache {
172
163
  key,
173
164
  normalizedKey,
174
165
  timers: {
175
- total: totalTimer,
166
+ start,
167
+ end: Date.now(),
176
168
  },
177
169
  });
178
170
  reject((0, descript_1.error)({
@@ -180,11 +172,8 @@ class Cache {
180
172
  }));
181
173
  return;
182
174
  }
183
- const networkTimerStart = process.hrtime();
184
175
  // maxage - seconds
185
176
  __classPrivateFieldGet(this, _Cache_client, "f").set(normalizedKey, json, 'EX', maxage, (error, done) => {
186
- const networkTimer = process.hrtime(networkTimerStart);
187
- const totalTimer = process.hrtime(totalTimerStart);
188
177
  if (error) {
189
178
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
190
179
  type: EVENT.REDIS_CACHE_WRITE_ERROR,
@@ -192,8 +181,8 @@ class Cache {
192
181
  key,
193
182
  normalizedKey,
194
183
  timers: {
195
- network: networkTimer,
196
- total: totalTimer,
184
+ start,
185
+ end: Date.now(),
197
186
  },
198
187
  });
199
188
  reject((0, descript_1.error)({
@@ -206,8 +195,8 @@ class Cache {
206
195
  key,
207
196
  normalizedKey,
208
197
  timers: {
209
- network: networkTimer,
210
- total: totalTimer,
198
+ start,
199
+ end: Date.now(),
211
200
  },
212
201
  });
213
202
  reject((0, descript_1.error)({
@@ -221,8 +210,8 @@ class Cache {
221
210
  key,
222
211
  normalizedKey,
223
212
  timers: {
224
- network: networkTimer,
225
- total: totalTimer,
213
+ start,
214
+ end: Date.now(),
226
215
  },
227
216
  });
228
217
  resolve();
@@ -243,7 +232,6 @@ _Cache_client = new WeakMap(), _Cache_logger = new WeakMap(), _Cache_options = n
243
232
  var EVENT;
244
233
  (function (EVENT) {
245
234
  EVENT["REDIS_CACHE_INITIALIZED"] = "REDIS_CACHE_INITIALIZED";
246
- EVENT["REDIS_CACHE_ERROR"] = "REDIS_CACHE_ERROR";
247
235
  EVENT["REDIS_CACHE_JSON_PARSING_FAILED"] = "REDIS_CACHE_JSON_PARSING_FAILED";
248
236
  EVENT["REDIS_CACHE_JSON_STRINGIFY_FAILED"] = "REDIS_CACHE_JSON_STRINGIFY_FAILED";
249
237
  EVENT["REDIS_CACHE_READ_DONE"] = "REDIS_CACHE_READ_DONE";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "descript-redis-cache",
3
- "version": "4.0.5",
3
+ "version": "4.0.7",
4
4
  "description": "plugin for descript to use redis as cache",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -41,7 +41,7 @@
41
41
  "@stylistic/eslint-plugin-ts": "^2.11.0",
42
42
  "@types/eslint__js": "^8.42.3",
43
43
  "@types/node": "^22.10.0",
44
- "descript": "^4.0.5",
44
+ "descript": "^4.0.6",
45
45
  "eslint": "^9.15.0",
46
46
  "typescript": "^5.7.2",
47
47
  "typescript-eslint": "^8.16.0",