cacheable 1.8.4 → 1.8.5
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/README.md +14 -1
- package/dist/index.cjs +35 -13
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +35 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -347,11 +347,24 @@ const cache = new CacheableMemory();
|
|
|
347
347
|
const wrappedFunction = cache.wrap(syncFunction, { ttl: '1h', key: 'syncFunction' });
|
|
348
348
|
console.log(wrappedFunction(2)); // 4
|
|
349
349
|
console.log(wrappedFunction(2)); // 4 from cache
|
|
350
|
-
console.log(cache.get('syncFunction')); // 4
|
|
351
350
|
```
|
|
352
351
|
|
|
353
352
|
In this example we are wrapping a `sync` function in a cache with a `ttl` of `1 hour`. This will cache the result of the function for `1 hour` and then expire the value. You can also set the `key` property in the `wrap()` options to set a custom key for the cache.
|
|
354
353
|
|
|
354
|
+
When an error occurs in the function it will not cache the value and will return the error. This is useful if you want to cache the results of a function but not cache the error. If you want it to cache the error you can set the `cacheError` property to `true` in the `wrap()` options. This is disabled by default.
|
|
355
|
+
|
|
356
|
+
```javascript
|
|
357
|
+
import { CacheableMemory } from 'cacheable';
|
|
358
|
+
const syncFunction = (value: number) => {
|
|
359
|
+
throw new Error('error');
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
const cache = new CacheableMemory();
|
|
363
|
+
const wrappedFunction = cache.wrap(syncFunction, { ttl: '1h', key: 'syncFunction', cacheError: true });
|
|
364
|
+
console.log(wrappedFunction()); // error
|
|
365
|
+
console.log(wrappedFunction()); // error from cache
|
|
366
|
+
```
|
|
367
|
+
|
|
355
368
|
# Keyv Storage Adapter - KeyvCacheableMemory
|
|
356
369
|
|
|
357
370
|
`cacheable` comes with a built-in storage adapter for Keyv called `KeyvCacheableMemory`. This takes `CacheableMemory` and creates a storage adapter for Keyv. This is useful if you want to use `CacheableMemory` as a storage adapter for Keyv. Here is an example of how to use `KeyvCacheableMemory`:
|
package/dist/index.cjs
CHANGED
|
@@ -45,7 +45,7 @@ __export(src_exports, {
|
|
|
45
45
|
});
|
|
46
46
|
module.exports = __toCommonJS(src_exports);
|
|
47
47
|
var import_keyv = require("keyv");
|
|
48
|
-
var
|
|
48
|
+
var import_hookified2 = require("hookified");
|
|
49
49
|
|
|
50
50
|
// src/shorthand-time.ts
|
|
51
51
|
var shorthandToMilliseconds = (shorthand) => {
|
|
@@ -114,6 +114,9 @@ var shorthandToTime = (shorthand, fromDate) => {
|
|
|
114
114
|
return fromDate.getTime() + milliseconds;
|
|
115
115
|
};
|
|
116
116
|
|
|
117
|
+
// src/memory.ts
|
|
118
|
+
var import_hookified = require("hookified");
|
|
119
|
+
|
|
117
120
|
// src/hash.ts
|
|
118
121
|
var crypto = __toESM(require("crypto"), 1);
|
|
119
122
|
function hash(object, algorithm = "sha256") {
|
|
@@ -188,8 +191,15 @@ function wrapSync(function_, options) {
|
|
|
188
191
|
const cacheKey = createWrapKey(function_, arguments_, keyPrefix);
|
|
189
192
|
let value = cache.get(cacheKey);
|
|
190
193
|
if (value === void 0) {
|
|
191
|
-
|
|
192
|
-
|
|
194
|
+
try {
|
|
195
|
+
value = function_(...arguments_);
|
|
196
|
+
cache.set(cacheKey, value, ttl);
|
|
197
|
+
} catch (error) {
|
|
198
|
+
cache.emit("error", error);
|
|
199
|
+
if (options.cacheErrors) {
|
|
200
|
+
cache.set(cacheKey, error, ttl);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
193
203
|
}
|
|
194
204
|
return value;
|
|
195
205
|
};
|
|
@@ -198,17 +208,21 @@ function wrap(function_, options) {
|
|
|
198
208
|
const { ttl, keyPrefix, cache } = options;
|
|
199
209
|
return async function(...arguments_) {
|
|
200
210
|
let value;
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
211
|
+
const cacheKey = createWrapKey(function_, arguments_, keyPrefix);
|
|
212
|
+
value = await cache.get(cacheKey);
|
|
213
|
+
if (value === void 0) {
|
|
214
|
+
value = await coalesceAsync(cacheKey, async () => {
|
|
215
|
+
try {
|
|
206
216
|
const result = await function_(...arguments_);
|
|
207
217
|
await cache.set(cacheKey, result, ttl);
|
|
208
218
|
return result;
|
|
209
|
-
})
|
|
210
|
-
|
|
211
|
-
|
|
219
|
+
} catch (error) {
|
|
220
|
+
cache.emit("error", error);
|
|
221
|
+
if (options.cacheErrors) {
|
|
222
|
+
await cache.set(cacheKey, error, ttl);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
});
|
|
212
226
|
}
|
|
213
227
|
return value;
|
|
214
228
|
};
|
|
@@ -294,7 +308,7 @@ var DoublyLinkedList = class {
|
|
|
294
308
|
};
|
|
295
309
|
|
|
296
310
|
// src/memory.ts
|
|
297
|
-
var CacheableMemory = class {
|
|
311
|
+
var CacheableMemory = class extends import_hookified.Hookified {
|
|
298
312
|
_lru = new DoublyLinkedList();
|
|
299
313
|
_hashCache = /* @__PURE__ */ new Map();
|
|
300
314
|
_hash0 = /* @__PURE__ */ new Map();
|
|
@@ -322,6 +336,7 @@ var CacheableMemory = class {
|
|
|
322
336
|
* @param {CacheableMemoryOptions} [options] - The options for the CacheableMemory
|
|
323
337
|
*/
|
|
324
338
|
constructor(options) {
|
|
339
|
+
super();
|
|
325
340
|
if (options?.ttl) {
|
|
326
341
|
this.setTtl(options.ttl);
|
|
327
342
|
}
|
|
@@ -867,6 +882,7 @@ var KeyvCacheableMemory = class {
|
|
|
867
882
|
return this.getStore(this._namespace).has(key);
|
|
868
883
|
}
|
|
869
884
|
on(event, listener) {
|
|
885
|
+
this.getStore(this._namespace).on(event, listener);
|
|
870
886
|
return this;
|
|
871
887
|
}
|
|
872
888
|
getStore(namespace) {
|
|
@@ -1114,7 +1130,7 @@ var CacheableEvents = /* @__PURE__ */ ((CacheableEvents2) => {
|
|
|
1114
1130
|
CacheableEvents2["ERROR"] = "error";
|
|
1115
1131
|
return CacheableEvents2;
|
|
1116
1132
|
})(CacheableEvents || {});
|
|
1117
|
-
var Cacheable = class extends
|
|
1133
|
+
var Cacheable = class extends import_hookified2.Hookified {
|
|
1118
1134
|
_primary = new import_keyv.Keyv({ store: new KeyvCacheableMemory() });
|
|
1119
1135
|
_secondary;
|
|
1120
1136
|
_nonBlocking = false;
|
|
@@ -1275,6 +1291,9 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
1275
1291
|
*/
|
|
1276
1292
|
setPrimary(primary) {
|
|
1277
1293
|
this._primary = primary instanceof import_keyv.Keyv ? primary : new import_keyv.Keyv(primary);
|
|
1294
|
+
this._primary.on("error", (error) => {
|
|
1295
|
+
this.emit("error" /* ERROR */, error);
|
|
1296
|
+
});
|
|
1278
1297
|
}
|
|
1279
1298
|
/**
|
|
1280
1299
|
* Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
|
|
@@ -1283,6 +1302,9 @@ var Cacheable = class extends import_hookified.Hookified {
|
|
|
1283
1302
|
*/
|
|
1284
1303
|
setSecondary(secondary) {
|
|
1285
1304
|
this._secondary = secondary instanceof import_keyv.Keyv ? secondary : new import_keyv.Keyv(secondary);
|
|
1305
|
+
this._secondary.on("error", (error) => {
|
|
1306
|
+
this.emit("error" /* ERROR */, error);
|
|
1307
|
+
});
|
|
1286
1308
|
}
|
|
1287
1309
|
getNameSpace() {
|
|
1288
1310
|
if (typeof this._namespace === "function") {
|
package/dist/index.d.cts
CHANGED
|
@@ -112,6 +112,7 @@ type CacheableStoreItem = {
|
|
|
112
112
|
type WrapFunctionOptions = {
|
|
113
113
|
ttl?: number | string;
|
|
114
114
|
keyPrefix?: string;
|
|
115
|
+
cacheErrors?: boolean;
|
|
115
116
|
};
|
|
116
117
|
type WrapOptions = WrapFunctionOptions & {
|
|
117
118
|
cache: Cacheable;
|
|
@@ -138,7 +139,7 @@ type CacheableMemoryOptions = {
|
|
|
138
139
|
lruSize?: number;
|
|
139
140
|
checkInterval?: number;
|
|
140
141
|
};
|
|
141
|
-
declare class CacheableMemory {
|
|
142
|
+
declare class CacheableMemory extends Hookified {
|
|
142
143
|
private _lru;
|
|
143
144
|
private readonly _hashCache;
|
|
144
145
|
private readonly _hash0;
|
package/dist/index.d.ts
CHANGED
|
@@ -112,6 +112,7 @@ type CacheableStoreItem = {
|
|
|
112
112
|
type WrapFunctionOptions = {
|
|
113
113
|
ttl?: number | string;
|
|
114
114
|
keyPrefix?: string;
|
|
115
|
+
cacheErrors?: boolean;
|
|
115
116
|
};
|
|
116
117
|
type WrapOptions = WrapFunctionOptions & {
|
|
117
118
|
cache: Cacheable;
|
|
@@ -138,7 +139,7 @@ type CacheableMemoryOptions = {
|
|
|
138
139
|
lruSize?: number;
|
|
139
140
|
checkInterval?: number;
|
|
140
141
|
};
|
|
141
|
-
declare class CacheableMemory {
|
|
142
|
+
declare class CacheableMemory extends Hookified {
|
|
142
143
|
private _lru;
|
|
143
144
|
private readonly _hashCache;
|
|
144
145
|
private readonly _hash0;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { Keyv } from "keyv";
|
|
3
|
-
import { Hookified } from "hookified";
|
|
3
|
+
import { Hookified as Hookified2 } from "hookified";
|
|
4
4
|
|
|
5
5
|
// src/shorthand-time.ts
|
|
6
6
|
var shorthandToMilliseconds = (shorthand) => {
|
|
@@ -69,6 +69,9 @@ var shorthandToTime = (shorthand, fromDate) => {
|
|
|
69
69
|
return fromDate.getTime() + milliseconds;
|
|
70
70
|
};
|
|
71
71
|
|
|
72
|
+
// src/memory.ts
|
|
73
|
+
import { Hookified } from "hookified";
|
|
74
|
+
|
|
72
75
|
// src/hash.ts
|
|
73
76
|
import * as crypto from "node:crypto";
|
|
74
77
|
function hash(object, algorithm = "sha256") {
|
|
@@ -143,8 +146,15 @@ function wrapSync(function_, options) {
|
|
|
143
146
|
const cacheKey = createWrapKey(function_, arguments_, keyPrefix);
|
|
144
147
|
let value = cache.get(cacheKey);
|
|
145
148
|
if (value === void 0) {
|
|
146
|
-
|
|
147
|
-
|
|
149
|
+
try {
|
|
150
|
+
value = function_(...arguments_);
|
|
151
|
+
cache.set(cacheKey, value, ttl);
|
|
152
|
+
} catch (error) {
|
|
153
|
+
cache.emit("error", error);
|
|
154
|
+
if (options.cacheErrors) {
|
|
155
|
+
cache.set(cacheKey, error, ttl);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
148
158
|
}
|
|
149
159
|
return value;
|
|
150
160
|
};
|
|
@@ -153,17 +163,21 @@ function wrap(function_, options) {
|
|
|
153
163
|
const { ttl, keyPrefix, cache } = options;
|
|
154
164
|
return async function(...arguments_) {
|
|
155
165
|
let value;
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
166
|
+
const cacheKey = createWrapKey(function_, arguments_, keyPrefix);
|
|
167
|
+
value = await cache.get(cacheKey);
|
|
168
|
+
if (value === void 0) {
|
|
169
|
+
value = await coalesceAsync(cacheKey, async () => {
|
|
170
|
+
try {
|
|
161
171
|
const result = await function_(...arguments_);
|
|
162
172
|
await cache.set(cacheKey, result, ttl);
|
|
163
173
|
return result;
|
|
164
|
-
})
|
|
165
|
-
|
|
166
|
-
|
|
174
|
+
} catch (error) {
|
|
175
|
+
cache.emit("error", error);
|
|
176
|
+
if (options.cacheErrors) {
|
|
177
|
+
await cache.set(cacheKey, error, ttl);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
});
|
|
167
181
|
}
|
|
168
182
|
return value;
|
|
169
183
|
};
|
|
@@ -249,7 +263,7 @@ var DoublyLinkedList = class {
|
|
|
249
263
|
};
|
|
250
264
|
|
|
251
265
|
// src/memory.ts
|
|
252
|
-
var CacheableMemory = class {
|
|
266
|
+
var CacheableMemory = class extends Hookified {
|
|
253
267
|
_lru = new DoublyLinkedList();
|
|
254
268
|
_hashCache = /* @__PURE__ */ new Map();
|
|
255
269
|
_hash0 = /* @__PURE__ */ new Map();
|
|
@@ -277,6 +291,7 @@ var CacheableMemory = class {
|
|
|
277
291
|
* @param {CacheableMemoryOptions} [options] - The options for the CacheableMemory
|
|
278
292
|
*/
|
|
279
293
|
constructor(options) {
|
|
294
|
+
super();
|
|
280
295
|
if (options?.ttl) {
|
|
281
296
|
this.setTtl(options.ttl);
|
|
282
297
|
}
|
|
@@ -822,6 +837,7 @@ var KeyvCacheableMemory = class {
|
|
|
822
837
|
return this.getStore(this._namespace).has(key);
|
|
823
838
|
}
|
|
824
839
|
on(event, listener) {
|
|
840
|
+
this.getStore(this._namespace).on(event, listener);
|
|
825
841
|
return this;
|
|
826
842
|
}
|
|
827
843
|
getStore(namespace) {
|
|
@@ -1072,7 +1088,7 @@ var CacheableEvents = /* @__PURE__ */ ((CacheableEvents2) => {
|
|
|
1072
1088
|
CacheableEvents2["ERROR"] = "error";
|
|
1073
1089
|
return CacheableEvents2;
|
|
1074
1090
|
})(CacheableEvents || {});
|
|
1075
|
-
var Cacheable = class extends
|
|
1091
|
+
var Cacheable = class extends Hookified2 {
|
|
1076
1092
|
_primary = new Keyv({ store: new KeyvCacheableMemory() });
|
|
1077
1093
|
_secondary;
|
|
1078
1094
|
_nonBlocking = false;
|
|
@@ -1233,6 +1249,9 @@ var Cacheable = class extends Hookified {
|
|
|
1233
1249
|
*/
|
|
1234
1250
|
setPrimary(primary) {
|
|
1235
1251
|
this._primary = primary instanceof Keyv ? primary : new Keyv(primary);
|
|
1252
|
+
this._primary.on("error", (error) => {
|
|
1253
|
+
this.emit("error" /* ERROR */, error);
|
|
1254
|
+
});
|
|
1236
1255
|
}
|
|
1237
1256
|
/**
|
|
1238
1257
|
* Sets the secondary store for the cacheable instance. If it is set to undefined then the secondary store is disabled.
|
|
@@ -1241,6 +1260,9 @@ var Cacheable = class extends Hookified {
|
|
|
1241
1260
|
*/
|
|
1242
1261
|
setSecondary(secondary) {
|
|
1243
1262
|
this._secondary = secondary instanceof Keyv ? secondary : new Keyv(secondary);
|
|
1263
|
+
this._secondary.on("error", (error) => {
|
|
1264
|
+
this.emit("error" /* ERROR */, error);
|
|
1265
|
+
});
|
|
1244
1266
|
}
|
|
1245
1267
|
getNameSpace() {
|
|
1246
1268
|
if (typeof this._namespace === "function") {
|