descript-redis-cache 4.0.4 → 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
@@ -10,14 +10,11 @@ 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 __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
13
  var _Cache_instances, _Cache_client, _Cache_logger, _Cache_options, _Cache_normalizeKey, _Cache_log;
17
14
  Object.defineProperty(exports, "__esModule", { value: true });
18
15
  exports.EVENT = exports.Cache = void 0;
19
16
  const node_crypto_1 = require("node:crypto");
20
- const descript_1 = __importDefault(require("descript"));
17
+ const descript_1 = require("descript");
21
18
  const ioredis_1 = require("ioredis");
22
19
  class Cache {
23
20
  constructor(options, logger) {
@@ -54,23 +51,20 @@ class Cache {
54
51
  key,
55
52
  normalizedKey,
56
53
  });
57
- const networkTimerStart = process.hrtime();
58
- const totalTimerStart = process.hrtime();
54
+ const start = Date.now();
59
55
  let isTimeout = false;
60
56
  const timer = setTimeout(() => {
61
57
  isTimeout = true;
62
- const networkTimer = process.hrtime(networkTimerStart);
63
- const totalTimer = process.hrtime(totalTimerStart);
64
58
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
65
59
  type: EVENT.REDIS_CACHE_READ_TIMEOUT,
66
60
  key,
67
61
  normalizedKey,
68
62
  timers: {
69
- network: networkTimer,
70
- total: totalTimer,
63
+ start,
64
+ end: Date.now(),
71
65
  },
72
66
  });
73
- reject(descript_1.default.error({
67
+ reject((0, descript_1.error)({
74
68
  id: EVENT.REDIS_CACHE_READ_TIMEOUT,
75
69
  }));
76
70
  }, __classPrivateFieldGet(this, _Cache_options, "f").readTimeout);
@@ -78,36 +72,33 @@ class Cache {
78
72
  if (isTimeout) {
79
73
  return;
80
74
  }
81
- const networkTimer = process.hrtime(networkTimerStart);
82
75
  clearTimeout(timer);
83
76
  if (error) {
84
- const totalTimer = process.hrtime(totalTimerStart);
85
77
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
86
78
  type: EVENT.REDIS_CACHE_READ_ERROR,
87
79
  error,
88
80
  key,
89
81
  normalizedKey,
90
82
  timers: {
91
- network: networkTimer,
92
- total: totalTimer,
83
+ start,
84
+ end: Date.now(),
93
85
  },
94
86
  });
95
- reject(descript_1.default.error({
87
+ reject((0, descript_1.error)({
96
88
  id: EVENT.REDIS_CACHE_READ_ERROR,
97
89
  }));
98
90
  }
99
91
  else if (!data) {
100
- const totalTimer = process.hrtime(totalTimerStart);
101
92
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
102
93
  type: EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND,
103
94
  key,
104
95
  normalizedKey,
105
96
  timers: {
106
- network: networkTimer,
107
- total: totalTimer,
97
+ start,
98
+ end: Date.now(),
108
99
  },
109
100
  });
110
- reject(descript_1.default.error({
101
+ reject((0, descript_1.error)({
111
102
  id: EVENT.REDIS_CACHE_READ_KEY_NOT_FOUND,
112
103
  }));
113
104
  }
@@ -117,7 +108,6 @@ class Cache {
117
108
  parsedValue = JSON.parse(data);
118
109
  }
119
110
  catch (error) {
120
- const totalTimer = process.hrtime(totalTimerStart);
121
111
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
122
112
  type: EVENT.REDIS_CACHE_JSON_PARSING_FAILED,
123
113
  data,
@@ -125,24 +115,23 @@ class Cache {
125
115
  key,
126
116
  normalizedKey,
127
117
  timers: {
128
- network: networkTimer,
129
- total: totalTimer,
118
+ start,
119
+ end: Date.now(),
130
120
  },
131
121
  });
132
- reject(descript_1.default.error({
122
+ reject((0, descript_1.error)({
133
123
  id: EVENT.REDIS_CACHE_JSON_PARSING_FAILED,
134
124
  }));
135
125
  return;
136
126
  }
137
- const totalTimer = process.hrtime(totalTimerStart);
138
127
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
139
128
  type: EVENT.REDIS_CACHE_READ_DONE,
140
129
  data,
141
130
  key,
142
131
  normalizedKey,
143
132
  timers: {
144
- network: networkTimer,
145
- total: totalTimer,
133
+ start,
134
+ end: Date.now(),
146
135
  },
147
136
  });
148
137
  resolve(parsedValue);
@@ -154,7 +143,7 @@ class Cache {
154
143
  if (typeof value === 'undefined') {
155
144
  return Promise.resolve();
156
145
  }
157
- const totalTimerStart = process.hrtime();
146
+ const start = Date.now();
158
147
  const normalizedKey = __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_normalizeKey).call(this, key);
159
148
  return new Promise((resolve, reject) => {
160
149
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
@@ -167,7 +156,6 @@ class Cache {
167
156
  json = JSON.stringify(value);
168
157
  }
169
158
  catch (error) {
170
- const totalTimer = process.hrtime(totalTimerStart);
171
159
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
172
160
  type: EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED,
173
161
  data: value,
@@ -175,19 +163,17 @@ class Cache {
175
163
  key,
176
164
  normalizedKey,
177
165
  timers: {
178
- total: totalTimer,
166
+ start,
167
+ end: Date.now(),
179
168
  },
180
169
  });
181
- reject(descript_1.default.error({
170
+ reject((0, descript_1.error)({
182
171
  id: EVENT.REDIS_CACHE_JSON_STRINGIFY_FAILED,
183
172
  }));
184
173
  return;
185
174
  }
186
- const networkTimerStart = process.hrtime();
187
175
  // maxage - seconds
188
176
  __classPrivateFieldGet(this, _Cache_client, "f").set(normalizedKey, json, 'EX', maxage, (error, done) => {
189
- const networkTimer = process.hrtime(networkTimerStart);
190
- const totalTimer = process.hrtime(totalTimerStart);
191
177
  if (error) {
192
178
  __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_log).call(this, {
193
179
  type: EVENT.REDIS_CACHE_WRITE_ERROR,
@@ -195,11 +181,11 @@ class Cache {
195
181
  key,
196
182
  normalizedKey,
197
183
  timers: {
198
- network: networkTimer,
199
- total: totalTimer,
184
+ start,
185
+ end: Date.now(),
200
186
  },
201
187
  });
202
- reject(descript_1.default.error({
188
+ reject((0, descript_1.error)({
203
189
  id: EVENT.REDIS_CACHE_WRITE_ERROR,
204
190
  }));
205
191
  }
@@ -209,11 +195,11 @@ class Cache {
209
195
  key,
210
196
  normalizedKey,
211
197
  timers: {
212
- network: networkTimer,
213
- total: totalTimer,
198
+ start,
199
+ end: Date.now(),
214
200
  },
215
201
  });
216
- reject(descript_1.default.error({
202
+ reject((0, descript_1.error)({
217
203
  id: EVENT.REDIS_CACHE_WRITE_FAILED,
218
204
  }));
219
205
  }
@@ -224,8 +210,8 @@ class Cache {
224
210
  key,
225
211
  normalizedKey,
226
212
  timers: {
227
- network: networkTimer,
228
- total: totalTimer,
213
+ start,
214
+ end: Date.now(),
229
215
  },
230
216
  });
231
217
  resolve();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "descript-redis-cache",
3
- "version": "4.0.4",
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",