@sebspark/promise-cache 0.2.9 → 1.1.0
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 +18 -5
- package/dist/index.d.ts +18 -5
- package/dist/index.js +21 -17
- package/dist/index.mjs +18 -16
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -13,8 +13,14 @@ type SetParams<T> = {
|
|
|
13
13
|
declare class Persistor {
|
|
14
14
|
client: ReturnType<typeof createClient> | null;
|
|
15
15
|
status: 'connected' | 'disconnected';
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
redis: {
|
|
17
|
+
url: string;
|
|
18
|
+
password?: string;
|
|
19
|
+
} | undefined;
|
|
20
|
+
constructor(redis?: {
|
|
21
|
+
url: string;
|
|
22
|
+
password?: string;
|
|
23
|
+
});
|
|
18
24
|
connect(onError?: (message: string) => void, onConnect?: (message: string) => void): Promise<void>;
|
|
19
25
|
size(): Promise<number>;
|
|
20
26
|
get<T>(key: string): Promise<GetType<T> | null>;
|
|
@@ -22,6 +28,10 @@ declare class Persistor {
|
|
|
22
28
|
set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
|
|
23
29
|
delete(key: string): Promise<void>;
|
|
24
30
|
}
|
|
31
|
+
declare const createPersistor: (redis?: {
|
|
32
|
+
url: string;
|
|
33
|
+
password?: string;
|
|
34
|
+
}) => Persistor;
|
|
25
35
|
|
|
26
36
|
declare class PromiseCache<U> {
|
|
27
37
|
persistor: Persistor;
|
|
@@ -32,10 +42,13 @@ declare class PromiseCache<U> {
|
|
|
32
42
|
* @param ttlInSeconds Default cache TTL.
|
|
33
43
|
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
34
44
|
*/
|
|
35
|
-
constructor({ ttlInSeconds, caseSensitive,
|
|
45
|
+
constructor({ ttlInSeconds, caseSensitive, redis, }: {
|
|
36
46
|
ttlInSeconds?: number;
|
|
37
47
|
caseSensitive?: boolean;
|
|
38
|
-
|
|
48
|
+
redis?: {
|
|
49
|
+
url: string;
|
|
50
|
+
password?: string;
|
|
51
|
+
};
|
|
39
52
|
});
|
|
40
53
|
/**
|
|
41
54
|
* Cache size.
|
|
@@ -78,4 +91,4 @@ declare class LocalStorage {
|
|
|
78
91
|
}
|
|
79
92
|
declare const createLocalMemoryClient: () => LocalStorage;
|
|
80
93
|
|
|
81
|
-
export { LocalStorage, PromiseCache, createLocalMemoryClient };
|
|
94
|
+
export { LocalStorage, Persistor, PromiseCache, createLocalMemoryClient, createPersistor };
|
package/dist/index.d.ts
CHANGED
|
@@ -13,8 +13,14 @@ type SetParams<T> = {
|
|
|
13
13
|
declare class Persistor {
|
|
14
14
|
client: ReturnType<typeof createClient> | null;
|
|
15
15
|
status: 'connected' | 'disconnected';
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
redis: {
|
|
17
|
+
url: string;
|
|
18
|
+
password?: string;
|
|
19
|
+
} | undefined;
|
|
20
|
+
constructor(redis?: {
|
|
21
|
+
url: string;
|
|
22
|
+
password?: string;
|
|
23
|
+
});
|
|
18
24
|
connect(onError?: (message: string) => void, onConnect?: (message: string) => void): Promise<void>;
|
|
19
25
|
size(): Promise<number>;
|
|
20
26
|
get<T>(key: string): Promise<GetType<T> | null>;
|
|
@@ -22,6 +28,10 @@ declare class Persistor {
|
|
|
22
28
|
set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
|
|
23
29
|
delete(key: string): Promise<void>;
|
|
24
30
|
}
|
|
31
|
+
declare const createPersistor: (redis?: {
|
|
32
|
+
url: string;
|
|
33
|
+
password?: string;
|
|
34
|
+
}) => Persistor;
|
|
25
35
|
|
|
26
36
|
declare class PromiseCache<U> {
|
|
27
37
|
persistor: Persistor;
|
|
@@ -32,10 +42,13 @@ declare class PromiseCache<U> {
|
|
|
32
42
|
* @param ttlInSeconds Default cache TTL.
|
|
33
43
|
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
34
44
|
*/
|
|
35
|
-
constructor({ ttlInSeconds, caseSensitive,
|
|
45
|
+
constructor({ ttlInSeconds, caseSensitive, redis, }: {
|
|
36
46
|
ttlInSeconds?: number;
|
|
37
47
|
caseSensitive?: boolean;
|
|
38
|
-
|
|
48
|
+
redis?: {
|
|
49
|
+
url: string;
|
|
50
|
+
password?: string;
|
|
51
|
+
};
|
|
39
52
|
});
|
|
40
53
|
/**
|
|
41
54
|
* Cache size.
|
|
@@ -78,4 +91,4 @@ declare class LocalStorage {
|
|
|
78
91
|
}
|
|
79
92
|
declare const createLocalMemoryClient: () => LocalStorage;
|
|
80
93
|
|
|
81
|
-
export { LocalStorage, PromiseCache, createLocalMemoryClient };
|
|
94
|
+
export { LocalStorage, Persistor, PromiseCache, createLocalMemoryClient, createPersistor };
|
package/dist/index.js
CHANGED
|
@@ -21,8 +21,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
LocalStorage: () => LocalStorage,
|
|
24
|
+
Persistor: () => Persistor,
|
|
24
25
|
PromiseCache: () => PromiseCache,
|
|
25
|
-
createLocalMemoryClient: () => createLocalMemoryClient
|
|
26
|
+
createLocalMemoryClient: () => createLocalMemoryClient,
|
|
27
|
+
createPersistor: () => createPersistor
|
|
26
28
|
});
|
|
27
29
|
module.exports = __toCommonJS(src_exports);
|
|
28
30
|
|
|
@@ -70,27 +72,26 @@ var createLocalMemoryClient = () => {
|
|
|
70
72
|
|
|
71
73
|
// src/persistor.ts
|
|
72
74
|
var CACHE_CLIENT = import_redis.createClient;
|
|
73
|
-
if (process.env.NODE_ENV === "test") {
|
|
74
|
-
CACHE_CLIENT = createLocalMemoryClient;
|
|
75
|
-
}
|
|
76
75
|
var Persistor = class {
|
|
77
76
|
client = null;
|
|
78
77
|
status = "disconnected";
|
|
79
|
-
|
|
80
|
-
constructor(
|
|
81
|
-
if (
|
|
82
|
-
this.
|
|
78
|
+
redis;
|
|
79
|
+
constructor(redis) {
|
|
80
|
+
if (redis) {
|
|
81
|
+
this.redis = redis;
|
|
83
82
|
} else {
|
|
84
83
|
CACHE_CLIENT = createLocalMemoryClient;
|
|
85
84
|
}
|
|
86
85
|
this.connect();
|
|
87
86
|
}
|
|
88
87
|
async connect(onError, onConnect) {
|
|
88
|
+
var _a;
|
|
89
89
|
try {
|
|
90
|
-
this.client = CACHE_CLIENT(
|
|
90
|
+
this.client = CACHE_CLIENT(this.redis);
|
|
91
91
|
this.client.on("error", (err) => {
|
|
92
|
+
var _a2;
|
|
92
93
|
if (onError) {
|
|
93
|
-
onError(`\u274C REDIS | Client Error | ${this.
|
|
94
|
+
onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
|
|
94
95
|
}
|
|
95
96
|
this.status = "disconnected";
|
|
96
97
|
});
|
|
@@ -101,8 +102,9 @@ var Persistor = class {
|
|
|
101
102
|
return;
|
|
102
103
|
}
|
|
103
104
|
this.client.on("connect", () => {
|
|
105
|
+
var _a2;
|
|
104
106
|
if (onConnect) {
|
|
105
|
-
onConnect(`\u{1F4E6} REDIS | Connection Ready | ${this.
|
|
107
|
+
onConnect(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
|
|
106
108
|
}
|
|
107
109
|
this.status = "connected";
|
|
108
110
|
resolve(true);
|
|
@@ -110,7 +112,7 @@ var Persistor = class {
|
|
|
110
112
|
});
|
|
111
113
|
} catch (err) {
|
|
112
114
|
if (onError) {
|
|
113
|
-
onError(`\u274C REDIS | Connection Error | ${this.
|
|
115
|
+
onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
|
|
114
116
|
}
|
|
115
117
|
this.status = "disconnected";
|
|
116
118
|
}
|
|
@@ -164,8 +166,8 @@ var Persistor = class {
|
|
|
164
166
|
}
|
|
165
167
|
}
|
|
166
168
|
};
|
|
167
|
-
var createPersistor = (
|
|
168
|
-
const persistor = new Persistor(
|
|
169
|
+
var createPersistor = (redis) => {
|
|
170
|
+
const persistor = new Persistor(redis);
|
|
169
171
|
return persistor;
|
|
170
172
|
};
|
|
171
173
|
|
|
@@ -183,9 +185,9 @@ var PromiseCache = class {
|
|
|
183
185
|
constructor({
|
|
184
186
|
ttlInSeconds,
|
|
185
187
|
caseSensitive = false,
|
|
186
|
-
|
|
188
|
+
redis
|
|
187
189
|
}) {
|
|
188
|
-
this.persistor = createPersistor(
|
|
190
|
+
this.persistor = createPersistor(redis);
|
|
189
191
|
this.caseSensitive = caseSensitive;
|
|
190
192
|
if (ttlInSeconds) {
|
|
191
193
|
this.ttl = ttlInSeconds * 1e3;
|
|
@@ -253,6 +255,8 @@ var PromiseCache = class {
|
|
|
253
255
|
// Annotate the CommonJS export names for ESM import in node:
|
|
254
256
|
0 && (module.exports = {
|
|
255
257
|
LocalStorage,
|
|
258
|
+
Persistor,
|
|
256
259
|
PromiseCache,
|
|
257
|
-
createLocalMemoryClient
|
|
260
|
+
createLocalMemoryClient,
|
|
261
|
+
createPersistor
|
|
258
262
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -42,27 +42,26 @@ var createLocalMemoryClient = () => {
|
|
|
42
42
|
|
|
43
43
|
// src/persistor.ts
|
|
44
44
|
var CACHE_CLIENT = createClient;
|
|
45
|
-
if (process.env.NODE_ENV === "test") {
|
|
46
|
-
CACHE_CLIENT = createLocalMemoryClient;
|
|
47
|
-
}
|
|
48
45
|
var Persistor = class {
|
|
49
46
|
client = null;
|
|
50
47
|
status = "disconnected";
|
|
51
|
-
|
|
52
|
-
constructor(
|
|
53
|
-
if (
|
|
54
|
-
this.
|
|
48
|
+
redis;
|
|
49
|
+
constructor(redis) {
|
|
50
|
+
if (redis) {
|
|
51
|
+
this.redis = redis;
|
|
55
52
|
} else {
|
|
56
53
|
CACHE_CLIENT = createLocalMemoryClient;
|
|
57
54
|
}
|
|
58
55
|
this.connect();
|
|
59
56
|
}
|
|
60
57
|
async connect(onError, onConnect) {
|
|
58
|
+
var _a;
|
|
61
59
|
try {
|
|
62
|
-
this.client = CACHE_CLIENT(
|
|
60
|
+
this.client = CACHE_CLIENT(this.redis);
|
|
63
61
|
this.client.on("error", (err) => {
|
|
62
|
+
var _a2;
|
|
64
63
|
if (onError) {
|
|
65
|
-
onError(`\u274C REDIS | Client Error | ${this.
|
|
64
|
+
onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
|
|
66
65
|
}
|
|
67
66
|
this.status = "disconnected";
|
|
68
67
|
});
|
|
@@ -73,8 +72,9 @@ var Persistor = class {
|
|
|
73
72
|
return;
|
|
74
73
|
}
|
|
75
74
|
this.client.on("connect", () => {
|
|
75
|
+
var _a2;
|
|
76
76
|
if (onConnect) {
|
|
77
|
-
onConnect(`\u{1F4E6} REDIS | Connection Ready | ${this.
|
|
77
|
+
onConnect(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
|
|
78
78
|
}
|
|
79
79
|
this.status = "connected";
|
|
80
80
|
resolve(true);
|
|
@@ -82,7 +82,7 @@ var Persistor = class {
|
|
|
82
82
|
});
|
|
83
83
|
} catch (err) {
|
|
84
84
|
if (onError) {
|
|
85
|
-
onError(`\u274C REDIS | Connection Error | ${this.
|
|
85
|
+
onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
|
|
86
86
|
}
|
|
87
87
|
this.status = "disconnected";
|
|
88
88
|
}
|
|
@@ -136,8 +136,8 @@ var Persistor = class {
|
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
138
|
};
|
|
139
|
-
var createPersistor = (
|
|
140
|
-
const persistor = new Persistor(
|
|
139
|
+
var createPersistor = (redis) => {
|
|
140
|
+
const persistor = new Persistor(redis);
|
|
141
141
|
return persistor;
|
|
142
142
|
};
|
|
143
143
|
|
|
@@ -155,9 +155,9 @@ var PromiseCache = class {
|
|
|
155
155
|
constructor({
|
|
156
156
|
ttlInSeconds,
|
|
157
157
|
caseSensitive = false,
|
|
158
|
-
|
|
158
|
+
redis
|
|
159
159
|
}) {
|
|
160
|
-
this.persistor = createPersistor(
|
|
160
|
+
this.persistor = createPersistor(redis);
|
|
161
161
|
this.caseSensitive = caseSensitive;
|
|
162
162
|
if (ttlInSeconds) {
|
|
163
163
|
this.ttl = ttlInSeconds * 1e3;
|
|
@@ -224,6 +224,8 @@ var PromiseCache = class {
|
|
|
224
224
|
};
|
|
225
225
|
export {
|
|
226
226
|
LocalStorage,
|
|
227
|
+
Persistor,
|
|
227
228
|
PromiseCache,
|
|
228
|
-
createLocalMemoryClient
|
|
229
|
+
createLocalMemoryClient,
|
|
230
|
+
createPersistor
|
|
229
231
|
};
|