@sebspark/promise-cache 1.4.0 → 2.0.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/dist/index.d.mts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +28 -16
- package/dist/index.mjs +26 -14
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createClient, RedisClientOptions } from 'redis';
|
|
2
2
|
export { RedisClientOptions } from 'redis';
|
|
3
|
+
import { UUID } from 'node:crypto';
|
|
3
4
|
|
|
4
5
|
type GetType<T> = {
|
|
5
6
|
value: T;
|
|
@@ -18,6 +19,7 @@ type PersistorConstructorType = {
|
|
|
18
19
|
};
|
|
19
20
|
declare class Persistor {
|
|
20
21
|
client: ReturnType<typeof createClient> | null;
|
|
22
|
+
private clientId;
|
|
21
23
|
private onError;
|
|
22
24
|
private onSuccess;
|
|
23
25
|
private readonly redis?;
|
|
@@ -26,6 +28,7 @@ declare class Persistor {
|
|
|
26
28
|
startConnection(): Promise<unknown>;
|
|
27
29
|
size(): Promise<number>;
|
|
28
30
|
get<T>(key: string): Promise<GetType<T> | null>;
|
|
31
|
+
getClientId(): UUID;
|
|
29
32
|
private createOptions;
|
|
30
33
|
set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
|
|
31
34
|
delete(key: string): Promise<void>;
|
|
@@ -35,7 +38,6 @@ declare const createPersistor: ({ redis, onError, onSuccess, }: {
|
|
|
35
38
|
onError?: () => void;
|
|
36
39
|
onSuccess?: () => void;
|
|
37
40
|
}) => Persistor;
|
|
38
|
-
declare const clean: () => void;
|
|
39
41
|
|
|
40
42
|
type PromiseCacheOptions = {
|
|
41
43
|
ttlInSeconds?: number;
|
|
@@ -44,6 +46,7 @@ type PromiseCacheOptions = {
|
|
|
44
46
|
onError?: () => void;
|
|
45
47
|
onSuccess?: () => void;
|
|
46
48
|
};
|
|
49
|
+
declare const promises: {};
|
|
47
50
|
declare class PromiseCache<U> {
|
|
48
51
|
persistor: Persistor;
|
|
49
52
|
private readonly caseSensitive;
|
|
@@ -95,4 +98,4 @@ declare class LocalStorage {
|
|
|
95
98
|
}
|
|
96
99
|
declare const createLocalMemoryClient: () => LocalStorage;
|
|
97
100
|
|
|
98
|
-
export { LocalStorage, Persistor, PromiseCache, type PromiseCacheOptions,
|
|
101
|
+
export { LocalStorage, Persistor, PromiseCache, type PromiseCacheOptions, createLocalMemoryClient, createPersistor, promises };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createClient, RedisClientOptions } from 'redis';
|
|
2
2
|
export { RedisClientOptions } from 'redis';
|
|
3
|
+
import { UUID } from 'node:crypto';
|
|
3
4
|
|
|
4
5
|
type GetType<T> = {
|
|
5
6
|
value: T;
|
|
@@ -18,6 +19,7 @@ type PersistorConstructorType = {
|
|
|
18
19
|
};
|
|
19
20
|
declare class Persistor {
|
|
20
21
|
client: ReturnType<typeof createClient> | null;
|
|
22
|
+
private clientId;
|
|
21
23
|
private onError;
|
|
22
24
|
private onSuccess;
|
|
23
25
|
private readonly redis?;
|
|
@@ -26,6 +28,7 @@ declare class Persistor {
|
|
|
26
28
|
startConnection(): Promise<unknown>;
|
|
27
29
|
size(): Promise<number>;
|
|
28
30
|
get<T>(key: string): Promise<GetType<T> | null>;
|
|
31
|
+
getClientId(): UUID;
|
|
29
32
|
private createOptions;
|
|
30
33
|
set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
|
|
31
34
|
delete(key: string): Promise<void>;
|
|
@@ -35,7 +38,6 @@ declare const createPersistor: ({ redis, onError, onSuccess, }: {
|
|
|
35
38
|
onError?: () => void;
|
|
36
39
|
onSuccess?: () => void;
|
|
37
40
|
}) => Persistor;
|
|
38
|
-
declare const clean: () => void;
|
|
39
41
|
|
|
40
42
|
type PromiseCacheOptions = {
|
|
41
43
|
ttlInSeconds?: number;
|
|
@@ -44,6 +46,7 @@ type PromiseCacheOptions = {
|
|
|
44
46
|
onError?: () => void;
|
|
45
47
|
onSuccess?: () => void;
|
|
46
48
|
};
|
|
49
|
+
declare const promises: {};
|
|
47
50
|
declare class PromiseCache<U> {
|
|
48
51
|
persistor: Persistor;
|
|
49
52
|
private readonly caseSensitive;
|
|
@@ -95,4 +98,4 @@ declare class LocalStorage {
|
|
|
95
98
|
}
|
|
96
99
|
declare const createLocalMemoryClient: () => LocalStorage;
|
|
97
100
|
|
|
98
|
-
export { LocalStorage, Persistor, PromiseCache, type PromiseCacheOptions,
|
|
101
|
+
export { LocalStorage, Persistor, PromiseCache, type PromiseCacheOptions, createLocalMemoryClient, createPersistor, promises };
|
package/dist/index.js
CHANGED
|
@@ -23,13 +23,14 @@ __export(src_exports, {
|
|
|
23
23
|
LocalStorage: () => LocalStorage,
|
|
24
24
|
Persistor: () => Persistor,
|
|
25
25
|
PromiseCache: () => PromiseCache,
|
|
26
|
-
clean: () => clean,
|
|
27
26
|
createLocalMemoryClient: () => createLocalMemoryClient,
|
|
28
|
-
createPersistor: () => createPersistor
|
|
27
|
+
createPersistor: () => createPersistor,
|
|
28
|
+
promises: () => promises
|
|
29
29
|
});
|
|
30
30
|
module.exports = __toCommonJS(src_exports);
|
|
31
31
|
|
|
32
32
|
// src/persistor.ts
|
|
33
|
+
var import_node_crypto = require("crypto");
|
|
33
34
|
var import_retry = require("@sebspark/retry");
|
|
34
35
|
var import_redis = require("redis");
|
|
35
36
|
|
|
@@ -77,6 +78,7 @@ var CACHE_CLIENT = import_redis.createClient;
|
|
|
77
78
|
var isTestRunning = process.env.NODE_ENV === "test";
|
|
78
79
|
var Persistor = class {
|
|
79
80
|
client = null;
|
|
81
|
+
clientId = (0, import_node_crypto.randomUUID)();
|
|
80
82
|
onError;
|
|
81
83
|
onSuccess;
|
|
82
84
|
redis;
|
|
@@ -98,7 +100,8 @@ var Persistor = class {
|
|
|
98
100
|
},
|
|
99
101
|
maxRetries: 5,
|
|
100
102
|
retryCondition: () => {
|
|
101
|
-
|
|
103
|
+
var _a;
|
|
104
|
+
console.log(`Trying to connect: ${this.clientId}, ${(_a = this.redis) == null ? void 0 : _a.name}`);
|
|
102
105
|
return true;
|
|
103
106
|
}
|
|
104
107
|
};
|
|
@@ -116,11 +119,15 @@ var Persistor = class {
|
|
|
116
119
|
throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
|
|
117
120
|
});
|
|
118
121
|
this.client.on("connect", () => {
|
|
119
|
-
var _a2, _b2;
|
|
122
|
+
var _a2, _b2, _c, _d;
|
|
120
123
|
if (this.onSuccess) {
|
|
121
|
-
this.onSuccess(
|
|
124
|
+
this.onSuccess(
|
|
125
|
+
`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.name} | ${this.clientId} | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`
|
|
126
|
+
);
|
|
122
127
|
}
|
|
123
|
-
console.log(
|
|
128
|
+
console.log(
|
|
129
|
+
`\u{1F4E6} REDIS | Connection Ready | ${(_c = this.redis) == null ? void 0 : _c.name}\xA0| ${this.clientId} | ${(_d = this.redis) == null ? void 0 : _d.url}`
|
|
130
|
+
);
|
|
124
131
|
});
|
|
125
132
|
return await this.client.connect();
|
|
126
133
|
} catch (err) {
|
|
@@ -150,6 +157,9 @@ var Persistor = class {
|
|
|
150
157
|
throw new Error(`Error getting data from redis: ${error}`);
|
|
151
158
|
}
|
|
152
159
|
}
|
|
160
|
+
getClientId() {
|
|
161
|
+
return this.clientId;
|
|
162
|
+
}
|
|
153
163
|
createOptions(ttl) {
|
|
154
164
|
if (ttl !== null && ttl !== void 0) {
|
|
155
165
|
return { PX: Math.round(ttl) };
|
|
@@ -179,33 +189,35 @@ var Persistor = class {
|
|
|
179
189
|
}
|
|
180
190
|
}
|
|
181
191
|
};
|
|
182
|
-
var
|
|
192
|
+
var persistors = {};
|
|
183
193
|
var createPersistor = ({
|
|
184
194
|
redis,
|
|
185
195
|
onError,
|
|
186
196
|
onSuccess
|
|
187
197
|
}) => {
|
|
188
198
|
if (redis) {
|
|
189
|
-
|
|
190
|
-
if (
|
|
191
|
-
|
|
199
|
+
let connectionName = redis.url;
|
|
200
|
+
if (redis.name) {
|
|
201
|
+
connectionName = redis.name;
|
|
202
|
+
}
|
|
203
|
+
const key = connectionName;
|
|
204
|
+
if (!persistors[key]) {
|
|
205
|
+
persistors[key] = new Persistor({
|
|
192
206
|
redis,
|
|
193
207
|
onError,
|
|
194
208
|
onSuccess
|
|
195
209
|
});
|
|
196
210
|
}
|
|
197
|
-
return
|
|
211
|
+
return persistors[key];
|
|
198
212
|
}
|
|
199
213
|
return new Persistor({
|
|
200
214
|
onSuccess,
|
|
201
215
|
onError
|
|
202
216
|
});
|
|
203
217
|
};
|
|
204
|
-
var clean = () => {
|
|
205
|
-
_persistors = {};
|
|
206
|
-
};
|
|
207
218
|
|
|
208
219
|
// src/promiseCache.ts
|
|
220
|
+
var promises = {};
|
|
209
221
|
var PromiseCache = class {
|
|
210
222
|
persistor;
|
|
211
223
|
caseSensitive;
|
|
@@ -293,7 +305,7 @@ var PromiseCache = class {
|
|
|
293
305
|
LocalStorage,
|
|
294
306
|
Persistor,
|
|
295
307
|
PromiseCache,
|
|
296
|
-
clean,
|
|
297
308
|
createLocalMemoryClient,
|
|
298
|
-
createPersistor
|
|
309
|
+
createPersistor,
|
|
310
|
+
promises
|
|
299
311
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/persistor.ts
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
2
3
|
import { retry } from "@sebspark/retry";
|
|
3
4
|
import { createClient } from "redis";
|
|
4
5
|
|
|
@@ -46,6 +47,7 @@ var CACHE_CLIENT = createClient;
|
|
|
46
47
|
var isTestRunning = process.env.NODE_ENV === "test";
|
|
47
48
|
var Persistor = class {
|
|
48
49
|
client = null;
|
|
50
|
+
clientId = randomUUID();
|
|
49
51
|
onError;
|
|
50
52
|
onSuccess;
|
|
51
53
|
redis;
|
|
@@ -67,7 +69,8 @@ var Persistor = class {
|
|
|
67
69
|
},
|
|
68
70
|
maxRetries: 5,
|
|
69
71
|
retryCondition: () => {
|
|
70
|
-
|
|
72
|
+
var _a;
|
|
73
|
+
console.log(`Trying to connect: ${this.clientId}, ${(_a = this.redis) == null ? void 0 : _a.name}`);
|
|
71
74
|
return true;
|
|
72
75
|
}
|
|
73
76
|
};
|
|
@@ -85,11 +88,15 @@ var Persistor = class {
|
|
|
85
88
|
throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
|
|
86
89
|
});
|
|
87
90
|
this.client.on("connect", () => {
|
|
88
|
-
var _a2, _b2;
|
|
91
|
+
var _a2, _b2, _c, _d;
|
|
89
92
|
if (this.onSuccess) {
|
|
90
|
-
this.onSuccess(
|
|
93
|
+
this.onSuccess(
|
|
94
|
+
`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.name} | ${this.clientId} | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`
|
|
95
|
+
);
|
|
91
96
|
}
|
|
92
|
-
console.log(
|
|
97
|
+
console.log(
|
|
98
|
+
`\u{1F4E6} REDIS | Connection Ready | ${(_c = this.redis) == null ? void 0 : _c.name}\xA0| ${this.clientId} | ${(_d = this.redis) == null ? void 0 : _d.url}`
|
|
99
|
+
);
|
|
93
100
|
});
|
|
94
101
|
return await this.client.connect();
|
|
95
102
|
} catch (err) {
|
|
@@ -119,6 +126,9 @@ var Persistor = class {
|
|
|
119
126
|
throw new Error(`Error getting data from redis: ${error}`);
|
|
120
127
|
}
|
|
121
128
|
}
|
|
129
|
+
getClientId() {
|
|
130
|
+
return this.clientId;
|
|
131
|
+
}
|
|
122
132
|
createOptions(ttl) {
|
|
123
133
|
if (ttl !== null && ttl !== void 0) {
|
|
124
134
|
return { PX: Math.round(ttl) };
|
|
@@ -148,33 +158,35 @@ var Persistor = class {
|
|
|
148
158
|
}
|
|
149
159
|
}
|
|
150
160
|
};
|
|
151
|
-
var
|
|
161
|
+
var persistors = {};
|
|
152
162
|
var createPersistor = ({
|
|
153
163
|
redis,
|
|
154
164
|
onError,
|
|
155
165
|
onSuccess
|
|
156
166
|
}) => {
|
|
157
167
|
if (redis) {
|
|
158
|
-
|
|
159
|
-
if (
|
|
160
|
-
|
|
168
|
+
let connectionName = redis.url;
|
|
169
|
+
if (redis.name) {
|
|
170
|
+
connectionName = redis.name;
|
|
171
|
+
}
|
|
172
|
+
const key = connectionName;
|
|
173
|
+
if (!persistors[key]) {
|
|
174
|
+
persistors[key] = new Persistor({
|
|
161
175
|
redis,
|
|
162
176
|
onError,
|
|
163
177
|
onSuccess
|
|
164
178
|
});
|
|
165
179
|
}
|
|
166
|
-
return
|
|
180
|
+
return persistors[key];
|
|
167
181
|
}
|
|
168
182
|
return new Persistor({
|
|
169
183
|
onSuccess,
|
|
170
184
|
onError
|
|
171
185
|
});
|
|
172
186
|
};
|
|
173
|
-
var clean = () => {
|
|
174
|
-
_persistors = {};
|
|
175
|
-
};
|
|
176
187
|
|
|
177
188
|
// src/promiseCache.ts
|
|
189
|
+
var promises = {};
|
|
178
190
|
var PromiseCache = class {
|
|
179
191
|
persistor;
|
|
180
192
|
caseSensitive;
|
|
@@ -261,7 +273,7 @@ export {
|
|
|
261
273
|
LocalStorage,
|
|
262
274
|
Persistor,
|
|
263
275
|
PromiseCache,
|
|
264
|
-
clean,
|
|
265
276
|
createLocalMemoryClient,
|
|
266
|
-
createPersistor
|
|
277
|
+
createPersistor,
|
|
278
|
+
promises
|
|
267
279
|
};
|