@cacheable/node-cache 1.4.2 → 1.5.1
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 +18 -1
- package/dist/index.cjs +30 -25
- package/dist/index.d.cts +15 -11
- package/dist/index.d.ts +15 -11
- package/dist/index.js +30 -16
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -36,6 +36,22 @@ npm install @cacheable/node-cache --save
|
|
|
36
36
|
|
|
37
37
|
# Basic Usage
|
|
38
38
|
|
|
39
|
+
```javascript
|
|
40
|
+
import NodeCache from '@cacheable/node-cache';
|
|
41
|
+
|
|
42
|
+
const cache = new NodeCache();
|
|
43
|
+
cache.set('foo', 'bar');
|
|
44
|
+
cache.get('foo'); // 'bar'
|
|
45
|
+
|
|
46
|
+
cache.set('foo', 'bar', 10); // 10 seconds
|
|
47
|
+
|
|
48
|
+
cache.del('foo'); // true
|
|
49
|
+
|
|
50
|
+
cache.set('bar', 'baz', '35m'); // 35 minutes using shorthand
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
# NodeCache Not Default Export
|
|
54
|
+
|
|
39
55
|
```javascript
|
|
40
56
|
import {NodeCache} from '@cacheable/node-cache';
|
|
41
57
|
|
|
@@ -84,6 +100,7 @@ export type NodeCacheStoreOptions = {
|
|
|
84
100
|
primary?: Keyv; // The primary storage adapter
|
|
85
101
|
secondary?: Keyv; // The secondary storage adapter
|
|
86
102
|
maxKeys?: number; // Default is 0 (unlimited). If this is set it will throw and error if you try to set more keys than the max.
|
|
103
|
+
stats?: boolean; // Default is true, if this is set to false it will not track stats
|
|
87
104
|
};
|
|
88
105
|
```
|
|
89
106
|
|
|
@@ -121,7 +138,7 @@ Create a new cache instance. You can pass in options to set the configuration:
|
|
|
121
138
|
|
|
122
139
|
```javascript
|
|
123
140
|
export type NodeCacheOptions = {
|
|
124
|
-
stdTTL?: number; // The standard ttl as number in seconds for every generated cache element. 0 = unlimited
|
|
141
|
+
stdTTL?: number; // The standard ttl as number in seconds for every generated cache element. 0 = unlimited. If string, it will be parsed as shorthand and default to milliseconds if it is a number as a string.
|
|
125
142
|
checkperiod?: number; // Default is 600, 0 means no periodic check
|
|
126
143
|
useClones?: boolean; // Default is true
|
|
127
144
|
deleteOnExpire?: boolean; // Default is true, if this is set to true it will delete the key when it expires.
|
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,48 +15,44 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
|
|
30
20
|
// src/index.ts
|
|
31
21
|
var src_exports = {};
|
|
32
22
|
__export(src_exports, {
|
|
23
|
+
NodeCache: () => NodeCache,
|
|
33
24
|
NodeCacheErrors: () => NodeCacheErrors,
|
|
34
25
|
NodeCacheStore: () => NodeCacheStore,
|
|
35
|
-
default: () =>
|
|
26
|
+
default: () => src_default
|
|
36
27
|
});
|
|
37
28
|
module.exports = __toCommonJS(src_exports);
|
|
38
|
-
var import_eventemitter3 = __toESM(require("eventemitter3"), 1);
|
|
39
29
|
var import_cacheable2 = require("cacheable");
|
|
30
|
+
var import_hookified2 = require("hookified");
|
|
40
31
|
|
|
41
32
|
// src/store.ts
|
|
42
33
|
var import_cacheable = require("cacheable");
|
|
43
34
|
var import_keyv = require("keyv");
|
|
44
|
-
var
|
|
35
|
+
var import_hookified = require("hookified");
|
|
36
|
+
var NodeCacheStore = class extends import_hookified.Hookified {
|
|
45
37
|
_maxKeys = 0;
|
|
46
38
|
_cache = new import_cacheable.Cacheable({ primary: new import_keyv.Keyv({ store: new import_cacheable.CacheableMemory() }) });
|
|
47
39
|
constructor(options) {
|
|
40
|
+
super();
|
|
48
41
|
if (options) {
|
|
49
42
|
const cacheOptions = {
|
|
50
43
|
ttl: options.ttl,
|
|
51
44
|
primary: options.primary,
|
|
52
|
-
secondary: options.secondary
|
|
45
|
+
secondary: options.secondary,
|
|
46
|
+
stats: options.stats ?? true
|
|
53
47
|
};
|
|
54
48
|
this._cache = new import_cacheable.Cacheable(cacheOptions);
|
|
55
49
|
if (options.maxKeys) {
|
|
56
50
|
this._maxKeys = options.maxKeys;
|
|
57
|
-
if (this._maxKeys > 0) {
|
|
58
|
-
this._cache.stats.enabled = true;
|
|
59
|
-
}
|
|
60
51
|
}
|
|
61
52
|
}
|
|
53
|
+
this._cache.on("error", (error) => {
|
|
54
|
+
this.emit("error", error);
|
|
55
|
+
});
|
|
62
56
|
}
|
|
63
57
|
/**
|
|
64
58
|
* Cacheable instance.
|
|
@@ -240,10 +234,10 @@ var NodeCacheErrors = /* @__PURE__ */ ((NodeCacheErrors2) => {
|
|
|
240
234
|
NodeCacheErrors2["ECACHEFULL"] = "Cache max keys amount exceeded";
|
|
241
235
|
NodeCacheErrors2["EKEYTYPE"] = "The key argument has to be of type `string` or `number`. Found: `__key`";
|
|
242
236
|
NodeCacheErrors2["EKEYSTYPE"] = "The keys argument has to be an array.";
|
|
243
|
-
NodeCacheErrors2["ETTLTYPE"] = "The ttl argument has to be a number.";
|
|
237
|
+
NodeCacheErrors2["ETTLTYPE"] = "The ttl argument has to be a number or a string for shorthand ttl.";
|
|
244
238
|
return NodeCacheErrors2;
|
|
245
239
|
})(NodeCacheErrors || {});
|
|
246
|
-
var NodeCache = class extends
|
|
240
|
+
var NodeCache = class extends import_hookified2.Hookified {
|
|
247
241
|
options = {
|
|
248
242
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
249
243
|
stdTTL: 0,
|
|
@@ -267,21 +261,27 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
267
261
|
* Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
268
262
|
* @param {string | number} key - it will convert the key to a string
|
|
269
263
|
* @param {any} value
|
|
270
|
-
* @param {number} [ttl] - this is in seconds and undefined will use the default ttl
|
|
264
|
+
* @param {number | string} [ttl] - this is in seconds and undefined will use the default ttl
|
|
271
265
|
* @returns {boolean}
|
|
272
266
|
*/
|
|
273
267
|
set(key, value, ttl) {
|
|
274
268
|
if (typeof key !== "string" && typeof key !== "number") {
|
|
275
269
|
throw this.createError("The key argument has to be of type `string` or `number`. Found: `__key`" /* EKEYTYPE */, key);
|
|
276
270
|
}
|
|
277
|
-
if (ttl && typeof ttl !== "number") {
|
|
278
|
-
throw this.createError("The ttl argument has to be a number." /* ETTLTYPE */, this.formatKey(key));
|
|
271
|
+
if (ttl && typeof ttl !== "number" && typeof ttl !== "string") {
|
|
272
|
+
throw this.createError("The ttl argument has to be a number or a string for shorthand ttl." /* ETTLTYPE */, this.formatKey(key));
|
|
279
273
|
}
|
|
280
274
|
const keyValue = this.formatKey(key);
|
|
281
|
-
|
|
275
|
+
let ttlValue = 0;
|
|
276
|
+
if (this.options.stdTTL) {
|
|
277
|
+
ttlValue = this.getExpirationTimestamp(this.options.stdTTL);
|
|
278
|
+
}
|
|
279
|
+
if (ttl) {
|
|
280
|
+
ttlValue = this.getExpirationTimestamp(ttl);
|
|
281
|
+
}
|
|
282
282
|
let expirationTimestamp = 0;
|
|
283
283
|
if (ttlValue && ttlValue > 0) {
|
|
284
|
-
expirationTimestamp =
|
|
284
|
+
expirationTimestamp = ttlValue;
|
|
285
285
|
}
|
|
286
286
|
if (this.options.maxKeys) {
|
|
287
287
|
const maxKeys = this.options.maxKeys;
|
|
@@ -412,7 +412,7 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
412
412
|
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
413
413
|
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
414
414
|
* @param {string | number} key if the key is a number it will convert it to a string
|
|
415
|
-
* @param {number} [ttl] the ttl in seconds
|
|
415
|
+
* @param {number | string} [ttl] the ttl in seconds if number, or a shorthand string like '1h' for 1 hour
|
|
416
416
|
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
417
417
|
*/
|
|
418
418
|
ttl(key, ttl) {
|
|
@@ -509,6 +509,9 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
509
509
|
return key.toString();
|
|
510
510
|
}
|
|
511
511
|
getExpirationTimestamp(ttlInSeconds) {
|
|
512
|
+
if (typeof ttlInSeconds === "string") {
|
|
513
|
+
return (0, import_cacheable2.shorthandToTime)(ttlInSeconds);
|
|
514
|
+
}
|
|
512
515
|
const currentTimestamp = Date.now();
|
|
513
516
|
const ttlInMilliseconds = ttlInSeconds * 1e3;
|
|
514
517
|
const expirationTimestamp = currentTimestamp + ttlInMilliseconds;
|
|
@@ -545,8 +548,10 @@ var NodeCache = class extends import_eventemitter3.default {
|
|
|
545
548
|
return new Error(error);
|
|
546
549
|
}
|
|
547
550
|
};
|
|
551
|
+
var src_default = NodeCache;
|
|
548
552
|
// Annotate the CommonJS export names for ESM import in node:
|
|
549
553
|
0 && (module.exports = {
|
|
554
|
+
NodeCache,
|
|
550
555
|
NodeCacheErrors,
|
|
551
556
|
NodeCacheStore
|
|
552
557
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Hookified } from 'hookified';
|
|
2
2
|
import { Cacheable } from 'cacheable';
|
|
3
3
|
import { Keyv } from 'keyv';
|
|
4
4
|
|
|
@@ -20,8 +20,12 @@ type NodeCacheStoreOptions = {
|
|
|
20
20
|
* [storage-tiering-and-caching](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching)
|
|
21
21
|
*/
|
|
22
22
|
secondary?: Keyv;
|
|
23
|
+
/**
|
|
24
|
+
* Enable stats tracking. This is a breaking change from the original NodeCache.
|
|
25
|
+
*/
|
|
26
|
+
stats?: boolean;
|
|
23
27
|
};
|
|
24
|
-
declare class NodeCacheStore {
|
|
28
|
+
declare class NodeCacheStore extends Hookified {
|
|
25
29
|
private _maxKeys;
|
|
26
30
|
private readonly _cache;
|
|
27
31
|
constructor(options?: NodeCacheStoreOptions);
|
|
@@ -142,9 +146,9 @@ declare class NodeCacheStore {
|
|
|
142
146
|
|
|
143
147
|
type NodeCacheOptions = {
|
|
144
148
|
/**
|
|
145
|
-
* The standard ttl as number in seconds for every generated cache element. 0 = unlimited
|
|
149
|
+
* The standard ttl as number in seconds for every generated cache element. 0 = unlimited, If string, it will be in shorthand format like '1h' for 1 hour
|
|
146
150
|
*/
|
|
147
|
-
stdTTL?: number;
|
|
151
|
+
stdTTL?: number | string;
|
|
148
152
|
/**
|
|
149
153
|
* The interval to check for expired items in seconds. Default is 600 = 5 minutes
|
|
150
154
|
*/
|
|
@@ -181,7 +185,7 @@ declare enum NodeCacheErrors {
|
|
|
181
185
|
ECACHEFULL = "Cache max keys amount exceeded",
|
|
182
186
|
EKEYTYPE = "The key argument has to be of type `string` or `number`. Found: `__key`",
|
|
183
187
|
EKEYSTYPE = "The keys argument has to be an array.",
|
|
184
|
-
ETTLTYPE = "The ttl argument has to be a number."
|
|
188
|
+
ETTLTYPE = "The ttl argument has to be a number or a string for shorthand ttl."
|
|
185
189
|
}
|
|
186
190
|
type NodeCacheStats = {
|
|
187
191
|
/**
|
|
@@ -205,7 +209,7 @@ type NodeCacheStats = {
|
|
|
205
209
|
*/
|
|
206
210
|
vsize: number;
|
|
207
211
|
};
|
|
208
|
-
declare class NodeCache extends
|
|
212
|
+
declare class NodeCache extends Hookified {
|
|
209
213
|
readonly options: NodeCacheOptions;
|
|
210
214
|
readonly store: Map<string, any>;
|
|
211
215
|
private _stats;
|
|
@@ -216,10 +220,10 @@ declare class NodeCache extends eventemitter {
|
|
|
216
220
|
* Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
217
221
|
* @param {string | number} key - it will convert the key to a string
|
|
218
222
|
* @param {any} value
|
|
219
|
-
* @param {number} [ttl] - this is in seconds and undefined will use the default ttl
|
|
223
|
+
* @param {number | string} [ttl] - this is in seconds and undefined will use the default ttl
|
|
220
224
|
* @returns {boolean}
|
|
221
225
|
*/
|
|
222
|
-
set(key: string | number, value: any, ttl?: number): boolean;
|
|
226
|
+
set(key: string | number, value: any, ttl?: number | string): boolean;
|
|
223
227
|
/**
|
|
224
228
|
* Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
|
|
225
229
|
* @param {NodeCacheItem[]} data an array of key value pairs with optional ttl
|
|
@@ -262,10 +266,10 @@ declare class NodeCache extends eventemitter {
|
|
|
262
266
|
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
263
267
|
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
264
268
|
* @param {string | number} key if the key is a number it will convert it to a string
|
|
265
|
-
* @param {number} [ttl] the ttl in seconds
|
|
269
|
+
* @param {number | string} [ttl] the ttl in seconds if number, or a shorthand string like '1h' for 1 hour
|
|
266
270
|
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
267
271
|
*/
|
|
268
|
-
ttl(key: string | number, ttl?: number): boolean;
|
|
272
|
+
ttl(key: string | number, ttl?: number | string): boolean;
|
|
269
273
|
/**
|
|
270
274
|
* Receive the ttl of a key.
|
|
271
275
|
* @param {string | number} key if the key is a number it will convert it to a string
|
|
@@ -317,4 +321,4 @@ declare class NodeCache extends eventemitter {
|
|
|
317
321
|
private createError;
|
|
318
322
|
}
|
|
319
323
|
|
|
320
|
-
export { NodeCacheErrors, type NodeCacheItem, type NodeCacheOptions, type NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, NodeCache as default };
|
|
324
|
+
export { NodeCache, NodeCacheErrors, type NodeCacheItem, type NodeCacheOptions, type NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, NodeCache as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Hookified } from 'hookified';
|
|
2
2
|
import { Cacheable } from 'cacheable';
|
|
3
3
|
import { Keyv } from 'keyv';
|
|
4
4
|
|
|
@@ -20,8 +20,12 @@ type NodeCacheStoreOptions = {
|
|
|
20
20
|
* [storage-tiering-and-caching](https://github.com/jaredwray/cacheable/tree/main/packages/cacheable#storage-tiering-and-caching)
|
|
21
21
|
*/
|
|
22
22
|
secondary?: Keyv;
|
|
23
|
+
/**
|
|
24
|
+
* Enable stats tracking. This is a breaking change from the original NodeCache.
|
|
25
|
+
*/
|
|
26
|
+
stats?: boolean;
|
|
23
27
|
};
|
|
24
|
-
declare class NodeCacheStore {
|
|
28
|
+
declare class NodeCacheStore extends Hookified {
|
|
25
29
|
private _maxKeys;
|
|
26
30
|
private readonly _cache;
|
|
27
31
|
constructor(options?: NodeCacheStoreOptions);
|
|
@@ -142,9 +146,9 @@ declare class NodeCacheStore {
|
|
|
142
146
|
|
|
143
147
|
type NodeCacheOptions = {
|
|
144
148
|
/**
|
|
145
|
-
* The standard ttl as number in seconds for every generated cache element. 0 = unlimited
|
|
149
|
+
* The standard ttl as number in seconds for every generated cache element. 0 = unlimited, If string, it will be in shorthand format like '1h' for 1 hour
|
|
146
150
|
*/
|
|
147
|
-
stdTTL?: number;
|
|
151
|
+
stdTTL?: number | string;
|
|
148
152
|
/**
|
|
149
153
|
* The interval to check for expired items in seconds. Default is 600 = 5 minutes
|
|
150
154
|
*/
|
|
@@ -181,7 +185,7 @@ declare enum NodeCacheErrors {
|
|
|
181
185
|
ECACHEFULL = "Cache max keys amount exceeded",
|
|
182
186
|
EKEYTYPE = "The key argument has to be of type `string` or `number`. Found: `__key`",
|
|
183
187
|
EKEYSTYPE = "The keys argument has to be an array.",
|
|
184
|
-
ETTLTYPE = "The ttl argument has to be a number."
|
|
188
|
+
ETTLTYPE = "The ttl argument has to be a number or a string for shorthand ttl."
|
|
185
189
|
}
|
|
186
190
|
type NodeCacheStats = {
|
|
187
191
|
/**
|
|
@@ -205,7 +209,7 @@ type NodeCacheStats = {
|
|
|
205
209
|
*/
|
|
206
210
|
vsize: number;
|
|
207
211
|
};
|
|
208
|
-
declare class NodeCache extends
|
|
212
|
+
declare class NodeCache extends Hookified {
|
|
209
213
|
readonly options: NodeCacheOptions;
|
|
210
214
|
readonly store: Map<string, any>;
|
|
211
215
|
private _stats;
|
|
@@ -216,10 +220,10 @@ declare class NodeCache extends eventemitter {
|
|
|
216
220
|
* Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
217
221
|
* @param {string | number} key - it will convert the key to a string
|
|
218
222
|
* @param {any} value
|
|
219
|
-
* @param {number} [ttl] - this is in seconds and undefined will use the default ttl
|
|
223
|
+
* @param {number | string} [ttl] - this is in seconds and undefined will use the default ttl
|
|
220
224
|
* @returns {boolean}
|
|
221
225
|
*/
|
|
222
|
-
set(key: string | number, value: any, ttl?: number): boolean;
|
|
226
|
+
set(key: string | number, value: any, ttl?: number | string): boolean;
|
|
223
227
|
/**
|
|
224
228
|
* Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
|
|
225
229
|
* @param {NodeCacheItem[]} data an array of key value pairs with optional ttl
|
|
@@ -262,10 +266,10 @@ declare class NodeCache extends eventemitter {
|
|
|
262
266
|
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
263
267
|
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
264
268
|
* @param {string | number} key if the key is a number it will convert it to a string
|
|
265
|
-
* @param {number} [ttl] the ttl in seconds
|
|
269
|
+
* @param {number | string} [ttl] the ttl in seconds if number, or a shorthand string like '1h' for 1 hour
|
|
266
270
|
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
267
271
|
*/
|
|
268
|
-
ttl(key: string | number, ttl?: number): boolean;
|
|
272
|
+
ttl(key: string | number, ttl?: number | string): boolean;
|
|
269
273
|
/**
|
|
270
274
|
* Receive the ttl of a key.
|
|
271
275
|
* @param {string | number} key if the key is a number it will convert it to a string
|
|
@@ -317,4 +321,4 @@ declare class NodeCache extends eventemitter {
|
|
|
317
321
|
private createError;
|
|
318
322
|
}
|
|
319
323
|
|
|
320
|
-
export { NodeCacheErrors, type NodeCacheItem, type NodeCacheOptions, type NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, NodeCache as default };
|
|
324
|
+
export { NodeCache, NodeCacheErrors, type NodeCacheItem, type NodeCacheOptions, type NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, NodeCache as default };
|
package/dist/index.js
CHANGED
|
@@ -1,28 +1,31 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
2
|
+
import { CacheableMemory as CacheableMemory2, CacheableStats, shorthandToTime } from "cacheable";
|
|
3
|
+
import { Hookified as Hookified2 } from "hookified";
|
|
4
4
|
|
|
5
5
|
// src/store.ts
|
|
6
6
|
import { Cacheable, CacheableMemory } from "cacheable";
|
|
7
7
|
import { Keyv } from "keyv";
|
|
8
|
-
|
|
8
|
+
import { Hookified } from "hookified";
|
|
9
|
+
var NodeCacheStore = class extends Hookified {
|
|
9
10
|
_maxKeys = 0;
|
|
10
11
|
_cache = new Cacheable({ primary: new Keyv({ store: new CacheableMemory() }) });
|
|
11
12
|
constructor(options) {
|
|
13
|
+
super();
|
|
12
14
|
if (options) {
|
|
13
15
|
const cacheOptions = {
|
|
14
16
|
ttl: options.ttl,
|
|
15
17
|
primary: options.primary,
|
|
16
|
-
secondary: options.secondary
|
|
18
|
+
secondary: options.secondary,
|
|
19
|
+
stats: options.stats ?? true
|
|
17
20
|
};
|
|
18
21
|
this._cache = new Cacheable(cacheOptions);
|
|
19
22
|
if (options.maxKeys) {
|
|
20
23
|
this._maxKeys = options.maxKeys;
|
|
21
|
-
if (this._maxKeys > 0) {
|
|
22
|
-
this._cache.stats.enabled = true;
|
|
23
|
-
}
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
+
this._cache.on("error", (error) => {
|
|
27
|
+
this.emit("error", error);
|
|
28
|
+
});
|
|
26
29
|
}
|
|
27
30
|
/**
|
|
28
31
|
* Cacheable instance.
|
|
@@ -204,10 +207,10 @@ var NodeCacheErrors = /* @__PURE__ */ ((NodeCacheErrors2) => {
|
|
|
204
207
|
NodeCacheErrors2["ECACHEFULL"] = "Cache max keys amount exceeded";
|
|
205
208
|
NodeCacheErrors2["EKEYTYPE"] = "The key argument has to be of type `string` or `number`. Found: `__key`";
|
|
206
209
|
NodeCacheErrors2["EKEYSTYPE"] = "The keys argument has to be an array.";
|
|
207
|
-
NodeCacheErrors2["ETTLTYPE"] = "The ttl argument has to be a number.";
|
|
210
|
+
NodeCacheErrors2["ETTLTYPE"] = "The ttl argument has to be a number or a string for shorthand ttl.";
|
|
208
211
|
return NodeCacheErrors2;
|
|
209
212
|
})(NodeCacheErrors || {});
|
|
210
|
-
var NodeCache = class extends
|
|
213
|
+
var NodeCache = class extends Hookified2 {
|
|
211
214
|
options = {
|
|
212
215
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
213
216
|
stdTTL: 0,
|
|
@@ -231,21 +234,27 @@ var NodeCache = class extends eventemitter {
|
|
|
231
234
|
* Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
232
235
|
* @param {string | number} key - it will convert the key to a string
|
|
233
236
|
* @param {any} value
|
|
234
|
-
* @param {number} [ttl] - this is in seconds and undefined will use the default ttl
|
|
237
|
+
* @param {number | string} [ttl] - this is in seconds and undefined will use the default ttl
|
|
235
238
|
* @returns {boolean}
|
|
236
239
|
*/
|
|
237
240
|
set(key, value, ttl) {
|
|
238
241
|
if (typeof key !== "string" && typeof key !== "number") {
|
|
239
242
|
throw this.createError("The key argument has to be of type `string` or `number`. Found: `__key`" /* EKEYTYPE */, key);
|
|
240
243
|
}
|
|
241
|
-
if (ttl && typeof ttl !== "number") {
|
|
242
|
-
throw this.createError("The ttl argument has to be a number." /* ETTLTYPE */, this.formatKey(key));
|
|
244
|
+
if (ttl && typeof ttl !== "number" && typeof ttl !== "string") {
|
|
245
|
+
throw this.createError("The ttl argument has to be a number or a string for shorthand ttl." /* ETTLTYPE */, this.formatKey(key));
|
|
243
246
|
}
|
|
244
247
|
const keyValue = this.formatKey(key);
|
|
245
|
-
|
|
248
|
+
let ttlValue = 0;
|
|
249
|
+
if (this.options.stdTTL) {
|
|
250
|
+
ttlValue = this.getExpirationTimestamp(this.options.stdTTL);
|
|
251
|
+
}
|
|
252
|
+
if (ttl) {
|
|
253
|
+
ttlValue = this.getExpirationTimestamp(ttl);
|
|
254
|
+
}
|
|
246
255
|
let expirationTimestamp = 0;
|
|
247
256
|
if (ttlValue && ttlValue > 0) {
|
|
248
|
-
expirationTimestamp =
|
|
257
|
+
expirationTimestamp = ttlValue;
|
|
249
258
|
}
|
|
250
259
|
if (this.options.maxKeys) {
|
|
251
260
|
const maxKeys = this.options.maxKeys;
|
|
@@ -376,7 +385,7 @@ var NodeCache = class extends eventemitter {
|
|
|
376
385
|
* Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
377
386
|
* Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
378
387
|
* @param {string | number} key if the key is a number it will convert it to a string
|
|
379
|
-
* @param {number} [ttl] the ttl in seconds
|
|
388
|
+
* @param {number | string} [ttl] the ttl in seconds if number, or a shorthand string like '1h' for 1 hour
|
|
380
389
|
* @returns {boolean} true if the key has been found and changed. Otherwise returns false.
|
|
381
390
|
*/
|
|
382
391
|
ttl(key, ttl) {
|
|
@@ -473,6 +482,9 @@ var NodeCache = class extends eventemitter {
|
|
|
473
482
|
return key.toString();
|
|
474
483
|
}
|
|
475
484
|
getExpirationTimestamp(ttlInSeconds) {
|
|
485
|
+
if (typeof ttlInSeconds === "string") {
|
|
486
|
+
return shorthandToTime(ttlInSeconds);
|
|
487
|
+
}
|
|
476
488
|
const currentTimestamp = Date.now();
|
|
477
489
|
const ttlInMilliseconds = ttlInSeconds * 1e3;
|
|
478
490
|
const expirationTimestamp = currentTimestamp + ttlInMilliseconds;
|
|
@@ -509,8 +521,10 @@ var NodeCache = class extends eventemitter {
|
|
|
509
521
|
return new Error(error);
|
|
510
522
|
}
|
|
511
523
|
};
|
|
524
|
+
var src_default = NodeCache;
|
|
512
525
|
export {
|
|
526
|
+
NodeCache,
|
|
513
527
|
NodeCacheErrors,
|
|
514
528
|
NodeCacheStore,
|
|
515
|
-
|
|
529
|
+
src_default as default
|
|
516
530
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cacheable/node-cache",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"description": "Simple and Maintained fast NodeJS internal caching",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"cacheable": "^1.8.1",
|
|
44
|
-
"
|
|
44
|
+
"hookified": "^1.5.1",
|
|
45
45
|
"keyv": "^5.2.1"
|
|
46
46
|
},
|
|
47
47
|
"files": [
|