@sebspark/promise-cache 1.2.1 → 1.2.3
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 +15 -5
- package/dist/index.d.ts +15 -5
- package/dist/index.js +31 -17
- package/dist/index.mjs +31 -17
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -10,19 +10,27 @@ type SetParams<T> = {
|
|
|
10
10
|
timestamp: number;
|
|
11
11
|
ttl?: number;
|
|
12
12
|
};
|
|
13
|
+
type PersistorConstructorType = {
|
|
14
|
+
redis?: RedisClientOptions;
|
|
15
|
+
onError?: () => void;
|
|
16
|
+
onSuccess?: () => void;
|
|
17
|
+
};
|
|
13
18
|
declare class Persistor {
|
|
14
19
|
client: ReturnType<typeof createClient> | null;
|
|
15
|
-
private status;
|
|
16
20
|
private readonly redis?;
|
|
17
|
-
constructor(
|
|
18
|
-
connect(onError?: (message: string) => void,
|
|
21
|
+
constructor(options: PersistorConstructorType);
|
|
22
|
+
connect(onError?: (message: string) => void, onSuccess?: (message: string) => void): Promise<void>;
|
|
19
23
|
size(): Promise<number>;
|
|
20
24
|
get<T>(key: string): Promise<GetType<T> | null>;
|
|
21
25
|
private createOptions;
|
|
22
26
|
set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
|
|
23
27
|
delete(key: string): Promise<void>;
|
|
24
28
|
}
|
|
25
|
-
declare const createPersistor: (redis
|
|
29
|
+
declare const createPersistor: ({ redis, onError, onSuccess, }: {
|
|
30
|
+
redis?: RedisClientOptions;
|
|
31
|
+
onError?: () => void;
|
|
32
|
+
onSuccess?: () => void;
|
|
33
|
+
}) => Persistor;
|
|
26
34
|
declare const clean: () => void;
|
|
27
35
|
|
|
28
36
|
declare class PromiseCache<U> {
|
|
@@ -34,10 +42,12 @@ declare class PromiseCache<U> {
|
|
|
34
42
|
* @param ttlInSeconds Default cache TTL.
|
|
35
43
|
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
36
44
|
*/
|
|
37
|
-
constructor({ ttlInSeconds, caseSensitive, redis, }: {
|
|
45
|
+
constructor({ ttlInSeconds, caseSensitive, redis, onSuccess, onError, }: {
|
|
38
46
|
ttlInSeconds?: number;
|
|
39
47
|
caseSensitive?: boolean;
|
|
40
48
|
redis?: RedisClientOptions;
|
|
49
|
+
onError?: () => void;
|
|
50
|
+
onSuccess?: () => void;
|
|
41
51
|
});
|
|
42
52
|
/**
|
|
43
53
|
* Cache size.
|
package/dist/index.d.ts
CHANGED
|
@@ -10,19 +10,27 @@ type SetParams<T> = {
|
|
|
10
10
|
timestamp: number;
|
|
11
11
|
ttl?: number;
|
|
12
12
|
};
|
|
13
|
+
type PersistorConstructorType = {
|
|
14
|
+
redis?: RedisClientOptions;
|
|
15
|
+
onError?: () => void;
|
|
16
|
+
onSuccess?: () => void;
|
|
17
|
+
};
|
|
13
18
|
declare class Persistor {
|
|
14
19
|
client: ReturnType<typeof createClient> | null;
|
|
15
|
-
private status;
|
|
16
20
|
private readonly redis?;
|
|
17
|
-
constructor(
|
|
18
|
-
connect(onError?: (message: string) => void,
|
|
21
|
+
constructor(options: PersistorConstructorType);
|
|
22
|
+
connect(onError?: (message: string) => void, onSuccess?: (message: string) => void): Promise<void>;
|
|
19
23
|
size(): Promise<number>;
|
|
20
24
|
get<T>(key: string): Promise<GetType<T> | null>;
|
|
21
25
|
private createOptions;
|
|
22
26
|
set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
|
|
23
27
|
delete(key: string): Promise<void>;
|
|
24
28
|
}
|
|
25
|
-
declare const createPersistor: (redis
|
|
29
|
+
declare const createPersistor: ({ redis, onError, onSuccess, }: {
|
|
30
|
+
redis?: RedisClientOptions;
|
|
31
|
+
onError?: () => void;
|
|
32
|
+
onSuccess?: () => void;
|
|
33
|
+
}) => Persistor;
|
|
26
34
|
declare const clean: () => void;
|
|
27
35
|
|
|
28
36
|
declare class PromiseCache<U> {
|
|
@@ -34,10 +42,12 @@ declare class PromiseCache<U> {
|
|
|
34
42
|
* @param ttlInSeconds Default cache TTL.
|
|
35
43
|
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
36
44
|
*/
|
|
37
|
-
constructor({ ttlInSeconds, caseSensitive, redis, }: {
|
|
45
|
+
constructor({ ttlInSeconds, caseSensitive, redis, onSuccess, onError, }: {
|
|
38
46
|
ttlInSeconds?: number;
|
|
39
47
|
caseSensitive?: boolean;
|
|
40
48
|
redis?: RedisClientOptions;
|
|
49
|
+
onError?: () => void;
|
|
50
|
+
onSuccess?: () => void;
|
|
41
51
|
});
|
|
42
52
|
/**
|
|
43
53
|
* Cache size.
|
package/dist/index.js
CHANGED
|
@@ -57,7 +57,7 @@ var LocalStorage = class {
|
|
|
57
57
|
}
|
|
58
58
|
// This is just for testing
|
|
59
59
|
on(event, callback) {
|
|
60
|
-
if (event === "connect") {
|
|
60
|
+
if (event === "connect" && callback) {
|
|
61
61
|
callback("connect");
|
|
62
62
|
}
|
|
63
63
|
return this;
|
|
@@ -75,26 +75,27 @@ var createLocalMemoryClient = () => {
|
|
|
75
75
|
var CACHE_CLIENT = import_redis.createClient;
|
|
76
76
|
var Persistor = class {
|
|
77
77
|
client = null;
|
|
78
|
-
status = "disconnected";
|
|
79
78
|
redis;
|
|
80
|
-
constructor(
|
|
79
|
+
constructor(options) {
|
|
80
|
+
const { redis, onError, onSuccess } = options;
|
|
81
81
|
if (redis) {
|
|
82
82
|
this.redis = redis;
|
|
83
83
|
} else {
|
|
84
84
|
CACHE_CLIENT = createLocalMemoryClient;
|
|
85
85
|
}
|
|
86
|
-
this.connect();
|
|
86
|
+
this.connect(onError, onSuccess);
|
|
87
87
|
}
|
|
88
|
-
async connect(onError,
|
|
89
|
-
var _a;
|
|
88
|
+
async connect(onError, onSuccess) {
|
|
89
|
+
var _a, _b;
|
|
90
90
|
try {
|
|
91
91
|
this.client = CACHE_CLIENT(this.redis);
|
|
92
92
|
this.client.on("error", (err) => {
|
|
93
|
-
var _a2;
|
|
93
|
+
var _a2, _b2;
|
|
94
94
|
if (onError) {
|
|
95
95
|
onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
|
|
96
|
+
} else {
|
|
97
|
+
console.error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
|
|
96
98
|
}
|
|
97
|
-
this.status = "disconnected";
|
|
98
99
|
});
|
|
99
100
|
this.client.connect();
|
|
100
101
|
await new Promise((resolve, reject) => {
|
|
@@ -104,18 +105,18 @@ var Persistor = class {
|
|
|
104
105
|
}
|
|
105
106
|
this.client.on("connect", () => {
|
|
106
107
|
var _a2;
|
|
107
|
-
if (
|
|
108
|
-
|
|
108
|
+
if (onSuccess) {
|
|
109
|
+
onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
|
|
109
110
|
}
|
|
110
|
-
this.status = "connected";
|
|
111
111
|
resolve(true);
|
|
112
112
|
});
|
|
113
113
|
});
|
|
114
114
|
} catch (err) {
|
|
115
115
|
if (onError) {
|
|
116
116
|
onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
|
|
117
|
+
} else {
|
|
118
|
+
console.error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
|
|
117
119
|
}
|
|
118
|
-
this.status = "disconnected";
|
|
119
120
|
}
|
|
120
121
|
}
|
|
121
122
|
async size() {
|
|
@@ -168,15 +169,26 @@ var Persistor = class {
|
|
|
168
169
|
}
|
|
169
170
|
};
|
|
170
171
|
var _persistors = {};
|
|
171
|
-
var createPersistor = (
|
|
172
|
+
var createPersistor = ({
|
|
173
|
+
redis,
|
|
174
|
+
onError,
|
|
175
|
+
onSuccess
|
|
176
|
+
}) => {
|
|
172
177
|
if (redis) {
|
|
173
178
|
const key = JSON.stringify(redis);
|
|
174
179
|
if (!_persistors[key]) {
|
|
175
|
-
|
|
180
|
+
_persistors[key] = new Persistor({
|
|
181
|
+
redis,
|
|
182
|
+
onError,
|
|
183
|
+
onSuccess
|
|
184
|
+
});
|
|
176
185
|
}
|
|
177
186
|
return _persistors[key];
|
|
178
187
|
}
|
|
179
|
-
return new Persistor(
|
|
188
|
+
return new Persistor({
|
|
189
|
+
onSuccess,
|
|
190
|
+
onError
|
|
191
|
+
});
|
|
180
192
|
};
|
|
181
193
|
var clean = () => {
|
|
182
194
|
_persistors = {};
|
|
@@ -196,9 +208,11 @@ var PromiseCache = class {
|
|
|
196
208
|
constructor({
|
|
197
209
|
ttlInSeconds,
|
|
198
210
|
caseSensitive = false,
|
|
199
|
-
redis
|
|
211
|
+
redis,
|
|
212
|
+
onSuccess,
|
|
213
|
+
onError
|
|
200
214
|
}) {
|
|
201
|
-
this.persistor = createPersistor(redis);
|
|
215
|
+
this.persistor = createPersistor({ redis, onError, onSuccess });
|
|
202
216
|
this.caseSensitive = caseSensitive;
|
|
203
217
|
if (ttlInSeconds) {
|
|
204
218
|
this.ttl = ttlInSeconds * 1e3;
|
package/dist/index.mjs
CHANGED
|
@@ -26,7 +26,7 @@ var LocalStorage = class {
|
|
|
26
26
|
}
|
|
27
27
|
// This is just for testing
|
|
28
28
|
on(event, callback) {
|
|
29
|
-
if (event === "connect") {
|
|
29
|
+
if (event === "connect" && callback) {
|
|
30
30
|
callback("connect");
|
|
31
31
|
}
|
|
32
32
|
return this;
|
|
@@ -44,26 +44,27 @@ var createLocalMemoryClient = () => {
|
|
|
44
44
|
var CACHE_CLIENT = createClient;
|
|
45
45
|
var Persistor = class {
|
|
46
46
|
client = null;
|
|
47
|
-
status = "disconnected";
|
|
48
47
|
redis;
|
|
49
|
-
constructor(
|
|
48
|
+
constructor(options) {
|
|
49
|
+
const { redis, onError, onSuccess } = options;
|
|
50
50
|
if (redis) {
|
|
51
51
|
this.redis = redis;
|
|
52
52
|
} else {
|
|
53
53
|
CACHE_CLIENT = createLocalMemoryClient;
|
|
54
54
|
}
|
|
55
|
-
this.connect();
|
|
55
|
+
this.connect(onError, onSuccess);
|
|
56
56
|
}
|
|
57
|
-
async connect(onError,
|
|
58
|
-
var _a;
|
|
57
|
+
async connect(onError, onSuccess) {
|
|
58
|
+
var _a, _b;
|
|
59
59
|
try {
|
|
60
60
|
this.client = CACHE_CLIENT(this.redis);
|
|
61
61
|
this.client.on("error", (err) => {
|
|
62
|
-
var _a2;
|
|
62
|
+
var _a2, _b2;
|
|
63
63
|
if (onError) {
|
|
64
64
|
onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
|
|
65
|
+
} else {
|
|
66
|
+
console.error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
|
|
65
67
|
}
|
|
66
|
-
this.status = "disconnected";
|
|
67
68
|
});
|
|
68
69
|
this.client.connect();
|
|
69
70
|
await new Promise((resolve, reject) => {
|
|
@@ -73,18 +74,18 @@ var Persistor = class {
|
|
|
73
74
|
}
|
|
74
75
|
this.client.on("connect", () => {
|
|
75
76
|
var _a2;
|
|
76
|
-
if (
|
|
77
|
-
|
|
77
|
+
if (onSuccess) {
|
|
78
|
+
onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
|
|
78
79
|
}
|
|
79
|
-
this.status = "connected";
|
|
80
80
|
resolve(true);
|
|
81
81
|
});
|
|
82
82
|
});
|
|
83
83
|
} catch (err) {
|
|
84
84
|
if (onError) {
|
|
85
85
|
onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
|
|
86
|
+
} else {
|
|
87
|
+
console.error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
|
|
86
88
|
}
|
|
87
|
-
this.status = "disconnected";
|
|
88
89
|
}
|
|
89
90
|
}
|
|
90
91
|
async size() {
|
|
@@ -137,15 +138,26 @@ var Persistor = class {
|
|
|
137
138
|
}
|
|
138
139
|
};
|
|
139
140
|
var _persistors = {};
|
|
140
|
-
var createPersistor = (
|
|
141
|
+
var createPersistor = ({
|
|
142
|
+
redis,
|
|
143
|
+
onError,
|
|
144
|
+
onSuccess
|
|
145
|
+
}) => {
|
|
141
146
|
if (redis) {
|
|
142
147
|
const key = JSON.stringify(redis);
|
|
143
148
|
if (!_persistors[key]) {
|
|
144
|
-
|
|
149
|
+
_persistors[key] = new Persistor({
|
|
150
|
+
redis,
|
|
151
|
+
onError,
|
|
152
|
+
onSuccess
|
|
153
|
+
});
|
|
145
154
|
}
|
|
146
155
|
return _persistors[key];
|
|
147
156
|
}
|
|
148
|
-
return new Persistor(
|
|
157
|
+
return new Persistor({
|
|
158
|
+
onSuccess,
|
|
159
|
+
onError
|
|
160
|
+
});
|
|
149
161
|
};
|
|
150
162
|
var clean = () => {
|
|
151
163
|
_persistors = {};
|
|
@@ -165,9 +177,11 @@ var PromiseCache = class {
|
|
|
165
177
|
constructor({
|
|
166
178
|
ttlInSeconds,
|
|
167
179
|
caseSensitive = false,
|
|
168
|
-
redis
|
|
180
|
+
redis,
|
|
181
|
+
onSuccess,
|
|
182
|
+
onError
|
|
169
183
|
}) {
|
|
170
|
-
this.persistor = createPersistor(redis);
|
|
184
|
+
this.persistor = createPersistor({ redis, onError, onSuccess });
|
|
171
185
|
this.caseSensitive = caseSensitive;
|
|
172
186
|
if (ttlInSeconds) {
|
|
173
187
|
this.ttl = ttlInSeconds * 1e3;
|