descript-redis-cache 4.0.5 → 4.0.6

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
@@ -2,9 +2,13 @@ import type { CacheInterface } 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;
@@ -13,6 +17,10 @@ export interface Options {
13
17
  interface Logger {
14
18
  log(event: LoggerEvent): void;
15
19
  }
20
+ interface Timers {
21
+ start: number;
22
+ end: number;
23
+ }
16
24
  export type LoggerEvent = ({
17
25
  type: EVENT.REDIS_CACHE_INITIALIZED;
18
26
  options: Options;
@@ -27,46 +35,31 @@ export type LoggerEvent = ({
27
35
  type: EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND;
28
36
  key: string;
29
37
  normalizedKey: string;
30
- timers: {
31
- network: [number, number];
32
- total: [number, number];
33
- };
38
+ timers: Timers;
34
39
  } | {
35
40
  type: EVENT.REDIS_CACHE_READ_TIMEOUT;
36
41
  key: string;
37
42
  normalizedKey: string;
38
- timers: {
39
- network: [number, number];
40
- total: [number, number];
41
- };
43
+ timers: Timers;
42
44
  } | {
43
45
  type: EVENT.REDIS_CACHE_READ_ERROR;
44
46
  error: Error;
45
47
  key: string;
46
48
  normalizedKey: string;
47
- timers: {
48
- network: [number, number];
49
- total: [number, number];
50
- };
49
+ timers: Timers;
51
50
  } | {
52
51
  type: EVENT.REDIS_CACHE_JSON_PARSING_FAILED;
53
52
  data: unknown;
54
53
  error: unknown;
55
54
  key: string;
56
55
  normalizedKey: string;
57
- timers: {
58
- network: [number, number];
59
- total: [number, number];
60
- };
56
+ timers: Timers;
61
57
  } | {
62
58
  type: EVENT.REDIS_CACHE_READ_DONE;
63
59
  data: unknown;
64
60
  key: string;
65
61
  normalizedKey: string;
66
- timers: {
67
- network: [number, number];
68
- total: [number, number];
69
- };
62
+ timers: Timers;
70
63
  } | {
71
64
  type: EVENT.REDIS_CACHE_WRITE_START;
72
65
  key: string;
@@ -77,35 +70,24 @@ export type LoggerEvent = ({
77
70
  error: unknown;
78
71
  key: string;
79
72
  normalizedKey: string;
80
- timers: {
81
- total: [number, number];
82
- };
73
+ timers: Timers;
83
74
  } | {
84
75
  type: EVENT.REDIS_CACHE_WRITE_ERROR;
85
76
  error: unknown;
86
77
  key: string;
87
78
  normalizedKey: string;
88
- timers: {
89
- network: [number, number];
90
- total: [number, number];
91
- };
79
+ timers: Timers;
92
80
  } | {
93
81
  type: EVENT.REDIS_CACHE_WRITE_FAILED;
94
82
  key: string;
95
83
  normalizedKey: string;
96
- timers: {
97
- network: [number, number];
98
- total: [number, number];
99
- };
84
+ timers: Timers;
100
85
  } | {
101
86
  type: EVENT.REDIS_CACHE_WRITE_DONE;
102
87
  data: string;
103
88
  key: string;
104
89
  normalizedKey: string;
105
- timers: {
106
- network: [number, number];
107
- total: [number, number];
108
- };
90
+ timers: Timers;
109
91
  });
110
92
  export declare class Cache<Result> implements CacheInterface<Result> {
111
93
  #private;
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();
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.6",
4
4
  "description": "plugin for descript to use redis as cache",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",