@sebspark/promise-cache 0.2.1 → 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 +48 -2
- package/dist/index.d.ts +48 -2
- package/dist/index.js +92 -15
- package/dist/index.mjs +90 -14
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
|
+
type GetType<T> = {
|
|
2
|
+
value: T;
|
|
3
|
+
ttl: number;
|
|
4
|
+
timestamp: number;
|
|
5
|
+
};
|
|
6
|
+
type SetParams<T> = {
|
|
7
|
+
value: T;
|
|
8
|
+
timestamp: number;
|
|
9
|
+
ttl: number;
|
|
10
|
+
};
|
|
11
|
+
declare class Persistor {
|
|
12
|
+
private client;
|
|
13
|
+
status: 'connected' | 'disconnected';
|
|
14
|
+
constructor();
|
|
15
|
+
connect(): Promise<void>;
|
|
16
|
+
size(): Promise<number>;
|
|
17
|
+
get<T>(key: string): Promise<GetType<T> | null>;
|
|
18
|
+
set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
|
|
19
|
+
delete(key: string): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
1
22
|
declare class PromiseCache<U> {
|
|
2
|
-
|
|
23
|
+
persistor: Persistor;
|
|
3
24
|
private readonly caseSensitive;
|
|
4
25
|
private readonly ttl;
|
|
5
26
|
/**
|
|
@@ -13,6 +34,18 @@ declare class PromiseCache<U> {
|
|
|
13
34
|
* @returns The number of entries in the cache.
|
|
14
35
|
*/
|
|
15
36
|
size(): Promise<number>;
|
|
37
|
+
/**
|
|
38
|
+
* Set a value in the cache.
|
|
39
|
+
* @param key Cache key.
|
|
40
|
+
* @param value Cache value.
|
|
41
|
+
* @param ttlInSeconds Time to live in seconds.
|
|
42
|
+
*/
|
|
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>;
|
|
16
49
|
/**
|
|
17
50
|
* A simple promise cache wrapper.
|
|
18
51
|
* @param key Cache key.
|
|
@@ -23,4 +56,17 @@ declare class PromiseCache<U> {
|
|
|
23
56
|
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number): Promise<U>;
|
|
24
57
|
}
|
|
25
58
|
|
|
26
|
-
|
|
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
|
@@ -1,5 +1,26 @@
|
|
|
1
|
+
type GetType<T> = {
|
|
2
|
+
value: T;
|
|
3
|
+
ttl: number;
|
|
4
|
+
timestamp: number;
|
|
5
|
+
};
|
|
6
|
+
type SetParams<T> = {
|
|
7
|
+
value: T;
|
|
8
|
+
timestamp: number;
|
|
9
|
+
ttl: number;
|
|
10
|
+
};
|
|
11
|
+
declare class Persistor {
|
|
12
|
+
private client;
|
|
13
|
+
status: 'connected' | 'disconnected';
|
|
14
|
+
constructor();
|
|
15
|
+
connect(): Promise<void>;
|
|
16
|
+
size(): Promise<number>;
|
|
17
|
+
get<T>(key: string): Promise<GetType<T> | null>;
|
|
18
|
+
set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
|
|
19
|
+
delete(key: string): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
1
22
|
declare class PromiseCache<U> {
|
|
2
|
-
|
|
23
|
+
persistor: Persistor;
|
|
3
24
|
private readonly caseSensitive;
|
|
4
25
|
private readonly ttl;
|
|
5
26
|
/**
|
|
@@ -13,6 +34,18 @@ declare class PromiseCache<U> {
|
|
|
13
34
|
* @returns The number of entries in the cache.
|
|
14
35
|
*/
|
|
15
36
|
size(): Promise<number>;
|
|
37
|
+
/**
|
|
38
|
+
* Set a value in the cache.
|
|
39
|
+
* @param key Cache key.
|
|
40
|
+
* @param value Cache value.
|
|
41
|
+
* @param ttlInSeconds Time to live in seconds.
|
|
42
|
+
*/
|
|
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>;
|
|
16
49
|
/**
|
|
17
50
|
* A simple promise cache wrapper.
|
|
18
51
|
* @param key Cache key.
|
|
@@ -23,4 +56,17 @@ declare class PromiseCache<U> {
|
|
|
23
56
|
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number): Promise<U>;
|
|
24
57
|
}
|
|
25
58
|
|
|
26
|
-
|
|
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,25 +20,45 @@ 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
|
|
|
27
28
|
// src/persistor.ts
|
|
28
29
|
var import_redis = require("redis");
|
|
29
|
-
var REDIS_HOST = process.env.REDIS_HOST || "
|
|
30
|
+
var REDIS_HOST = process.env.REDIS_HOST || "127.0.0.1";
|
|
30
31
|
var REDIS_PORT = process.env.REDIS_PORT || 6379;
|
|
31
32
|
var REDIS_URL = `redis://${REDIS_HOST}:${REDIS_PORT}`;
|
|
32
33
|
var Persistor = class {
|
|
33
34
|
client;
|
|
35
|
+
status = "disconnected";
|
|
34
36
|
constructor() {
|
|
35
|
-
this.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
() =>
|
|
41
|
-
|
|
37
|
+
this.connect();
|
|
38
|
+
}
|
|
39
|
+
async connect() {
|
|
40
|
+
try {
|
|
41
|
+
this.client = (0, import_redis.createClient)({ url: REDIS_URL });
|
|
42
|
+
this.client.on("error", (err) => {
|
|
43
|
+
console.error(`\u274C REDIS | Client Error | ${REDIS_URL}`, err);
|
|
44
|
+
this.status = "disconnected";
|
|
45
|
+
});
|
|
46
|
+
this.client.connect();
|
|
47
|
+
await new Promise((resolve, reject) => {
|
|
48
|
+
if (!this.client) {
|
|
49
|
+
reject("Client not initialized");
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
this.client.on("connect", () => {
|
|
53
|
+
console.info(`\u{1F4E6} REDIS | Connection Ready | ${REDIS_URL}`);
|
|
54
|
+
this.status = "connected";
|
|
55
|
+
resolve(true);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
} catch (err) {
|
|
59
|
+
console.error(`\u274C REDIS | Connection Error | ${REDIS_URL}`, err);
|
|
60
|
+
this.status = "disconnected";
|
|
61
|
+
}
|
|
42
62
|
}
|
|
43
63
|
async size() {
|
|
44
64
|
if (!this.client) {
|
|
@@ -91,7 +111,7 @@ var persistor = new Persistor();
|
|
|
91
111
|
|
|
92
112
|
// src/promiseCache.ts
|
|
93
113
|
var PromiseCache = class {
|
|
94
|
-
|
|
114
|
+
persistor;
|
|
95
115
|
caseSensitive;
|
|
96
116
|
ttl;
|
|
97
117
|
// Time to live in milliseconds.
|
|
@@ -101,7 +121,7 @@ var PromiseCache = class {
|
|
|
101
121
|
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
102
122
|
*/
|
|
103
123
|
constructor(ttlInSeconds, caseSensitive = false) {
|
|
104
|
-
this.
|
|
124
|
+
this.persistor = persistor;
|
|
105
125
|
this.caseSensitive = caseSensitive;
|
|
106
126
|
this.ttl = ttlInSeconds * 1e3;
|
|
107
127
|
}
|
|
@@ -110,7 +130,30 @@ var PromiseCache = class {
|
|
|
110
130
|
* @returns The number of entries in the cache.
|
|
111
131
|
*/
|
|
112
132
|
async size() {
|
|
113
|
-
return await this.
|
|
133
|
+
return await this.persistor.size();
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Set a value in the cache.
|
|
137
|
+
* @param key Cache key.
|
|
138
|
+
* @param value Cache value.
|
|
139
|
+
* @param ttlInSeconds Time to live in seconds.
|
|
140
|
+
*/
|
|
141
|
+
async override(key, value, ttlInSeconds) {
|
|
142
|
+
const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
|
|
143
|
+
const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
|
|
144
|
+
await this.persistor.set(effectiveKey, {
|
|
145
|
+
value,
|
|
146
|
+
timestamp: Date.now(),
|
|
147
|
+
ttl: effectiveTTL
|
|
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;
|
|
114
157
|
}
|
|
115
158
|
/**
|
|
116
159
|
* A simple promise cache wrapper.
|
|
@@ -123,7 +166,7 @@ var PromiseCache = class {
|
|
|
123
166
|
const now = Date.now();
|
|
124
167
|
const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
|
|
125
168
|
const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
|
|
126
|
-
const cached = await this.
|
|
169
|
+
const cached = await this.persistor.get(effectiveKey);
|
|
127
170
|
if (cached) {
|
|
128
171
|
if (cached.ttl !== effectiveTTL) {
|
|
129
172
|
console.error(
|
|
@@ -133,7 +176,7 @@ var PromiseCache = class {
|
|
|
133
176
|
return cached.value;
|
|
134
177
|
}
|
|
135
178
|
const response = await delegate();
|
|
136
|
-
this.
|
|
179
|
+
this.persistor.set(effectiveKey, {
|
|
137
180
|
value: response,
|
|
138
181
|
timestamp: now,
|
|
139
182
|
ttl: effectiveTTL
|
|
@@ -141,7 +184,41 @@ var PromiseCache = class {
|
|
|
141
184
|
return response;
|
|
142
185
|
}
|
|
143
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();
|
|
144
220
|
// Annotate the CommonJS export names for ESM import in node:
|
|
145
221
|
0 && (module.exports = {
|
|
146
|
-
PromiseCache
|
|
222
|
+
PromiseCache,
|
|
223
|
+
createClient
|
|
147
224
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,18 +1,37 @@
|
|
|
1
1
|
// src/persistor.ts
|
|
2
2
|
import { createClient } from "redis";
|
|
3
|
-
var REDIS_HOST = process.env.REDIS_HOST || "
|
|
3
|
+
var REDIS_HOST = process.env.REDIS_HOST || "127.0.0.1";
|
|
4
4
|
var REDIS_PORT = process.env.REDIS_PORT || 6379;
|
|
5
5
|
var REDIS_URL = `redis://${REDIS_HOST}:${REDIS_PORT}`;
|
|
6
6
|
var Persistor = class {
|
|
7
7
|
client;
|
|
8
|
+
status = "disconnected";
|
|
8
9
|
constructor() {
|
|
9
|
-
this.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
() =>
|
|
15
|
-
|
|
10
|
+
this.connect();
|
|
11
|
+
}
|
|
12
|
+
async connect() {
|
|
13
|
+
try {
|
|
14
|
+
this.client = createClient({ url: REDIS_URL });
|
|
15
|
+
this.client.on("error", (err) => {
|
|
16
|
+
console.error(`\u274C REDIS | Client Error | ${REDIS_URL}`, err);
|
|
17
|
+
this.status = "disconnected";
|
|
18
|
+
});
|
|
19
|
+
this.client.connect();
|
|
20
|
+
await new Promise((resolve, reject) => {
|
|
21
|
+
if (!this.client) {
|
|
22
|
+
reject("Client not initialized");
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
this.client.on("connect", () => {
|
|
26
|
+
console.info(`\u{1F4E6} REDIS | Connection Ready | ${REDIS_URL}`);
|
|
27
|
+
this.status = "connected";
|
|
28
|
+
resolve(true);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
} catch (err) {
|
|
32
|
+
console.error(`\u274C REDIS | Connection Error | ${REDIS_URL}`, err);
|
|
33
|
+
this.status = "disconnected";
|
|
34
|
+
}
|
|
16
35
|
}
|
|
17
36
|
async size() {
|
|
18
37
|
if (!this.client) {
|
|
@@ -65,7 +84,7 @@ var persistor = new Persistor();
|
|
|
65
84
|
|
|
66
85
|
// src/promiseCache.ts
|
|
67
86
|
var PromiseCache = class {
|
|
68
|
-
|
|
87
|
+
persistor;
|
|
69
88
|
caseSensitive;
|
|
70
89
|
ttl;
|
|
71
90
|
// Time to live in milliseconds.
|
|
@@ -75,7 +94,7 @@ var PromiseCache = class {
|
|
|
75
94
|
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
76
95
|
*/
|
|
77
96
|
constructor(ttlInSeconds, caseSensitive = false) {
|
|
78
|
-
this.
|
|
97
|
+
this.persistor = persistor;
|
|
79
98
|
this.caseSensitive = caseSensitive;
|
|
80
99
|
this.ttl = ttlInSeconds * 1e3;
|
|
81
100
|
}
|
|
@@ -84,7 +103,30 @@ var PromiseCache = class {
|
|
|
84
103
|
* @returns The number of entries in the cache.
|
|
85
104
|
*/
|
|
86
105
|
async size() {
|
|
87
|
-
return await this.
|
|
106
|
+
return await this.persistor.size();
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Set a value in the cache.
|
|
110
|
+
* @param key Cache key.
|
|
111
|
+
* @param value Cache value.
|
|
112
|
+
* @param ttlInSeconds Time to live in seconds.
|
|
113
|
+
*/
|
|
114
|
+
async override(key, value, ttlInSeconds) {
|
|
115
|
+
const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
|
|
116
|
+
const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
|
|
117
|
+
await this.persistor.set(effectiveKey, {
|
|
118
|
+
value,
|
|
119
|
+
timestamp: Date.now(),
|
|
120
|
+
ttl: effectiveTTL
|
|
121
|
+
});
|
|
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;
|
|
88
130
|
}
|
|
89
131
|
/**
|
|
90
132
|
* A simple promise cache wrapper.
|
|
@@ -97,7 +139,7 @@ var PromiseCache = class {
|
|
|
97
139
|
const now = Date.now();
|
|
98
140
|
const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
|
|
99
141
|
const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
|
|
100
|
-
const cached = await this.
|
|
142
|
+
const cached = await this.persistor.get(effectiveKey);
|
|
101
143
|
if (cached) {
|
|
102
144
|
if (cached.ttl !== effectiveTTL) {
|
|
103
145
|
console.error(
|
|
@@ -107,7 +149,7 @@ var PromiseCache = class {
|
|
|
107
149
|
return cached.value;
|
|
108
150
|
}
|
|
109
151
|
const response = await delegate();
|
|
110
|
-
this.
|
|
152
|
+
this.persistor.set(effectiveKey, {
|
|
111
153
|
value: response,
|
|
112
154
|
timestamp: now,
|
|
113
155
|
ttl: effectiveTTL
|
|
@@ -115,6 +157,40 @@ var PromiseCache = class {
|
|
|
115
157
|
return response;
|
|
116
158
|
}
|
|
117
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();
|
|
118
193
|
export {
|
|
119
|
-
PromiseCache
|
|
194
|
+
PromiseCache,
|
|
195
|
+
createClient2 as createClient
|
|
120
196
|
};
|