@sebspark/promise-cache 0.2.2 → 0.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 +19 -1
- package/dist/index.d.ts +19 -1
- package/dist/index.js +46 -3
- package/dist/index.mjs +44 -2
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -41,6 +41,11 @@ declare class PromiseCache<U> {
|
|
|
41
41
|
* @param ttlInSeconds Time to live in seconds.
|
|
42
42
|
*/
|
|
43
43
|
override<U>(key: string, value: U, ttlInSeconds?: number): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Get a value from the cache.
|
|
46
|
+
* @param key Cache key.
|
|
47
|
+
*/
|
|
48
|
+
find<U>(key: string): Promise<U | null>;
|
|
44
49
|
/**
|
|
45
50
|
* A simple promise cache wrapper.
|
|
46
51
|
* @param key Cache key.
|
|
@@ -51,4 +56,17 @@ declare class PromiseCache<U> {
|
|
|
51
56
|
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number): Promise<U>;
|
|
52
57
|
}
|
|
53
58
|
|
|
54
|
-
|
|
59
|
+
declare class MockedRedis {
|
|
60
|
+
client: Map<any, any>;
|
|
61
|
+
get(key: string): any;
|
|
62
|
+
set(key: string, value: string, options?: {
|
|
63
|
+
EX: number;
|
|
64
|
+
}): void;
|
|
65
|
+
clear(): void;
|
|
66
|
+
DBSIZE(): Promise<number>;
|
|
67
|
+
on(event: string, callback: () => void): this;
|
|
68
|
+
connect(): Promise<this>;
|
|
69
|
+
}
|
|
70
|
+
declare const createClient: () => MockedRedis;
|
|
71
|
+
|
|
72
|
+
export { PromiseCache, createClient };
|
package/dist/index.d.ts
CHANGED
|
@@ -41,6 +41,11 @@ declare class PromiseCache<U> {
|
|
|
41
41
|
* @param ttlInSeconds Time to live in seconds.
|
|
42
42
|
*/
|
|
43
43
|
override<U>(key: string, value: U, ttlInSeconds?: number): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Get a value from the cache.
|
|
46
|
+
* @param key Cache key.
|
|
47
|
+
*/
|
|
48
|
+
find<U>(key: string): Promise<U | null>;
|
|
44
49
|
/**
|
|
45
50
|
* A simple promise cache wrapper.
|
|
46
51
|
* @param key Cache key.
|
|
@@ -51,4 +56,17 @@ declare class PromiseCache<U> {
|
|
|
51
56
|
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number): Promise<U>;
|
|
52
57
|
}
|
|
53
58
|
|
|
54
|
-
|
|
59
|
+
declare class MockedRedis {
|
|
60
|
+
client: Map<any, any>;
|
|
61
|
+
get(key: string): any;
|
|
62
|
+
set(key: string, value: string, options?: {
|
|
63
|
+
EX: number;
|
|
64
|
+
}): void;
|
|
65
|
+
clear(): void;
|
|
66
|
+
DBSIZE(): Promise<number>;
|
|
67
|
+
on(event: string, callback: () => void): this;
|
|
68
|
+
connect(): Promise<this>;
|
|
69
|
+
}
|
|
70
|
+
declare const createClient: () => MockedRedis;
|
|
71
|
+
|
|
72
|
+
export { PromiseCache, createClient };
|
package/dist/index.js
CHANGED
|
@@ -20,7 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
-
PromiseCache: () => PromiseCache
|
|
23
|
+
PromiseCache: () => PromiseCache,
|
|
24
|
+
createClient: () => createClient2
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(src_exports);
|
|
26
27
|
|
|
@@ -37,7 +38,7 @@ var Persistor = class {
|
|
|
37
38
|
}
|
|
38
39
|
async connect() {
|
|
39
40
|
try {
|
|
40
|
-
this.client =
|
|
41
|
+
this.client = (0, import_redis.createClient)({ url: REDIS_URL });
|
|
41
42
|
this.client.on("error", (err) => {
|
|
42
43
|
console.error(`\u274C REDIS | Client Error | ${REDIS_URL}`, err);
|
|
43
44
|
this.status = "disconnected";
|
|
@@ -146,6 +147,14 @@ var PromiseCache = class {
|
|
|
146
147
|
ttl: effectiveTTL
|
|
147
148
|
});
|
|
148
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* Get a value from the cache.
|
|
152
|
+
* @param key Cache key.
|
|
153
|
+
*/
|
|
154
|
+
async find(key) {
|
|
155
|
+
const result = await this.persistor.get(key);
|
|
156
|
+
return (result == null ? void 0 : result.value) ?? null;
|
|
157
|
+
}
|
|
149
158
|
/**
|
|
150
159
|
* A simple promise cache wrapper.
|
|
151
160
|
* @param key Cache key.
|
|
@@ -175,7 +184,41 @@ var PromiseCache = class {
|
|
|
175
184
|
return response;
|
|
176
185
|
}
|
|
177
186
|
};
|
|
187
|
+
|
|
188
|
+
// src/test/mockedRedis.ts
|
|
189
|
+
var MockedRedis = class {
|
|
190
|
+
client = /* @__PURE__ */ new Map();
|
|
191
|
+
get(key) {
|
|
192
|
+
return this.client.get(key);
|
|
193
|
+
}
|
|
194
|
+
set(key, value, options) {
|
|
195
|
+
this.client.set(key, value);
|
|
196
|
+
if (options == null ? void 0 : options.EX) {
|
|
197
|
+
setTimeout(() => {
|
|
198
|
+
this.client.delete(key);
|
|
199
|
+
}, options.EX / 1e3);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
clear() {
|
|
203
|
+
this.client.clear();
|
|
204
|
+
}
|
|
205
|
+
async DBSIZE() {
|
|
206
|
+
return Promise.resolve(this.client.size);
|
|
207
|
+
}
|
|
208
|
+
// Fake connection to fake server
|
|
209
|
+
on(event, callback) {
|
|
210
|
+
if (event === "connect") {
|
|
211
|
+
callback();
|
|
212
|
+
}
|
|
213
|
+
return this;
|
|
214
|
+
}
|
|
215
|
+
connect() {
|
|
216
|
+
return Promise.resolve(this);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
var createClient2 = () => new MockedRedis();
|
|
178
220
|
// Annotate the CommonJS export names for ESM import in node:
|
|
179
221
|
0 && (module.exports = {
|
|
180
|
-
PromiseCache
|
|
222
|
+
PromiseCache,
|
|
223
|
+
createClient
|
|
181
224
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -11,7 +11,7 @@ var Persistor = class {
|
|
|
11
11
|
}
|
|
12
12
|
async connect() {
|
|
13
13
|
try {
|
|
14
|
-
this.client =
|
|
14
|
+
this.client = createClient({ url: REDIS_URL });
|
|
15
15
|
this.client.on("error", (err) => {
|
|
16
16
|
console.error(`\u274C REDIS | Client Error | ${REDIS_URL}`, err);
|
|
17
17
|
this.status = "disconnected";
|
|
@@ -120,6 +120,14 @@ var PromiseCache = class {
|
|
|
120
120
|
ttl: effectiveTTL
|
|
121
121
|
});
|
|
122
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Get a value from the cache.
|
|
125
|
+
* @param key Cache key.
|
|
126
|
+
*/
|
|
127
|
+
async find(key) {
|
|
128
|
+
const result = await this.persistor.get(key);
|
|
129
|
+
return (result == null ? void 0 : result.value) ?? null;
|
|
130
|
+
}
|
|
123
131
|
/**
|
|
124
132
|
* A simple promise cache wrapper.
|
|
125
133
|
* @param key Cache key.
|
|
@@ -149,6 +157,40 @@ var PromiseCache = class {
|
|
|
149
157
|
return response;
|
|
150
158
|
}
|
|
151
159
|
};
|
|
160
|
+
|
|
161
|
+
// src/test/mockedRedis.ts
|
|
162
|
+
var MockedRedis = class {
|
|
163
|
+
client = /* @__PURE__ */ new Map();
|
|
164
|
+
get(key) {
|
|
165
|
+
return this.client.get(key);
|
|
166
|
+
}
|
|
167
|
+
set(key, value, options) {
|
|
168
|
+
this.client.set(key, value);
|
|
169
|
+
if (options == null ? void 0 : options.EX) {
|
|
170
|
+
setTimeout(() => {
|
|
171
|
+
this.client.delete(key);
|
|
172
|
+
}, options.EX / 1e3);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
clear() {
|
|
176
|
+
this.client.clear();
|
|
177
|
+
}
|
|
178
|
+
async DBSIZE() {
|
|
179
|
+
return Promise.resolve(this.client.size);
|
|
180
|
+
}
|
|
181
|
+
// Fake connection to fake server
|
|
182
|
+
on(event, callback) {
|
|
183
|
+
if (event === "connect") {
|
|
184
|
+
callback();
|
|
185
|
+
}
|
|
186
|
+
return this;
|
|
187
|
+
}
|
|
188
|
+
connect() {
|
|
189
|
+
return Promise.resolve(this);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
var createClient2 = () => new MockedRedis();
|
|
152
193
|
export {
|
|
153
|
-
PromiseCache
|
|
194
|
+
PromiseCache,
|
|
195
|
+
createClient2 as createClient
|
|
154
196
|
};
|