@cacheable/node-cache 0.5.0 → 1.0.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/LICENSE +1 -1
- package/README.md +43 -2
- package/dist/index.cjs +398 -0
- package/dist/index.d.cts +89 -0
- package/dist/index.d.ts +39 -12
- package/dist/index.js +335 -317
- package/package.json +19 -9
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
> Simple and Maintained fast Node.js caching
|
|
6
6
|
|
|
7
|
-
[](https://codecov.io/gh/jaredwray/cacheable)
|
|
8
8
|
[](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)
|
|
9
9
|
[](https://www.npmjs.com/package/cacheable)
|
|
10
10
|
[](https://www.npmjs.com/package/cacheable)
|
|
@@ -22,6 +22,7 @@ Note: `NodeCache` is ready and available for use. `NodeCacheStore` is in progres
|
|
|
22
22
|
* [Getting Started](#getting-started)
|
|
23
23
|
* [Basic Usage](#basic-usage)
|
|
24
24
|
* [Advanced Usage](#advanced-usage)
|
|
25
|
+
* [NodeCacheStore](#nodecachestore)
|
|
25
26
|
* [API](#api)
|
|
26
27
|
* [How to Contribute](#how-to-contribute)
|
|
27
28
|
* [License and Copyright](#license-and-copyright)
|
|
@@ -60,6 +61,46 @@ await cache.get('foo'); // 'bar'
|
|
|
60
61
|
cache.getStats(); // {hits: 1, misses: 1, keys: 1, ksize: 2, vsize: 3}
|
|
61
62
|
```
|
|
62
63
|
|
|
64
|
+
## NodeCacheStore
|
|
65
|
+
|
|
66
|
+
The `NodeCacheStore` is a class that extends the `NodeCache` and adds the ability to use storage adapters. This is based on the `cacheable` engine and allows you to do layer 1 and layer 2 caching. The storage adapters are based on the [Keyv](https://keyv.org) package. This allows you to use any of the storage adapters that are available.
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
import {NodeCacheStore} from '@cacheable/node-cache';
|
|
70
|
+
|
|
71
|
+
const cache = new NodeCacheStore();
|
|
72
|
+
cache.set('foo', 'bar');
|
|
73
|
+
cache.get('foo'); // 'bar'
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### NodeCacheStoreOptions
|
|
77
|
+
|
|
78
|
+
When initializing the cache you can pass in the options below:
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
export type NodeCacheStoreOptions = {
|
|
82
|
+
ttl?: number; // The standard ttl as number in seconds for every generated cache element. 0 = unlimited
|
|
83
|
+
primary?: Keyv; // The primary storage adapter
|
|
84
|
+
secondary?: Keyv; // The secondary storage adapter
|
|
85
|
+
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.
|
|
86
|
+
};
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Node Cache Store API
|
|
90
|
+
|
|
91
|
+
* `set(key: string | number, value: any, ttl?: number): Promise<boolean>` - Set a key value pair with an optional ttl (in milliseconds). Will return true on success. If the ttl is not set it will default to 0 (no ttl)
|
|
92
|
+
* `mset(data: Array<NodeCacheItem>): Promise<boolean>` - Set multiple key value pairs at once
|
|
93
|
+
* `get(key: string | number): Promise<any>` - Get a value from the cache by key
|
|
94
|
+
* `mget(keys: Array<string | number>): Promise<Record<string, unknown>>` - Get multiple values from the cache by keys
|
|
95
|
+
* `del(key: string | number): Promise<boolean>` - Delete a key
|
|
96
|
+
* `mdel(keys: Array<string | number>): Promise<boolean>` - Delete multiple keys
|
|
97
|
+
* `clear(): Promise<void>` - Clear the cache
|
|
98
|
+
* `stats`: `NodeCacheStats` - Get the stats of the cache
|
|
99
|
+
* `ttl`: `number` - The standard ttl as number in seconds for every generated cache element. 0 = unlimited
|
|
100
|
+
* `primary`: `Keyv` - The primary storage adapter
|
|
101
|
+
* `secondary`: `Keyv` - The secondary storage adapter
|
|
102
|
+
* `maxKeys`: `number` - If this is set it will throw and error if you try to set more keys than the max
|
|
103
|
+
|
|
63
104
|
## API
|
|
64
105
|
|
|
65
106
|
### `constructor(options?: NodeCacheOptions)`
|
|
@@ -259,4 +300,4 @@ cache.on('set', (key, value) => {
|
|
|
259
300
|
You can contribute by forking the repo and submitting a pull request. Please make sure to add tests and update the documentation. To learn more about how to contribute go to our main README [https://github.com/jaredwray/cacheable](https://github.com/jaredwray/cacheable). This will talk about how to `Open a Pull Request`, `Ask a Question`, or `Post an Issue`.
|
|
260
301
|
|
|
261
302
|
## License and Copyright
|
|
262
|
-
[MIT © Jared Wray](
|
|
303
|
+
[MIT © Jared Wray](./LICENSE)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
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
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
NodeCacheErrors: () => NodeCacheErrors,
|
|
34
|
+
NodeCacheStore: () => NodeCacheStore,
|
|
35
|
+
default: () => NodeCache
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(src_exports);
|
|
38
|
+
var import_eventemitter3 = __toESM(require("eventemitter3"), 1);
|
|
39
|
+
var import_cacheable2 = require("cacheable");
|
|
40
|
+
|
|
41
|
+
// src/store.ts
|
|
42
|
+
var import_cacheable = require("cacheable");
|
|
43
|
+
var import_keyv = require("keyv");
|
|
44
|
+
var NodeCacheStore = class {
|
|
45
|
+
_maxKeys = 0;
|
|
46
|
+
_cache = new import_cacheable.Cacheable({ primary: new import_keyv.Keyv({ store: new import_cacheable.CacheableMemory() }) });
|
|
47
|
+
constructor(options) {
|
|
48
|
+
if (options) {
|
|
49
|
+
const cacheOptions = {
|
|
50
|
+
ttl: options.ttl,
|
|
51
|
+
primary: options.primary,
|
|
52
|
+
secondary: options.secondary
|
|
53
|
+
};
|
|
54
|
+
this._cache = new import_cacheable.Cacheable(cacheOptions);
|
|
55
|
+
if (options.maxKeys) {
|
|
56
|
+
this._maxKeys = options.maxKeys;
|
|
57
|
+
if (this._maxKeys > 0) {
|
|
58
|
+
this._cache.stats.enabled = true;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
get cache() {
|
|
64
|
+
return this._cache;
|
|
65
|
+
}
|
|
66
|
+
get ttl() {
|
|
67
|
+
return this._cache.ttl;
|
|
68
|
+
}
|
|
69
|
+
set ttl(ttl) {
|
|
70
|
+
this._cache.ttl = ttl;
|
|
71
|
+
}
|
|
72
|
+
get primary() {
|
|
73
|
+
return this._cache.primary;
|
|
74
|
+
}
|
|
75
|
+
set primary(primary) {
|
|
76
|
+
this._cache.primary = primary;
|
|
77
|
+
}
|
|
78
|
+
get secondary() {
|
|
79
|
+
return this._cache.secondary;
|
|
80
|
+
}
|
|
81
|
+
set secondary(secondary) {
|
|
82
|
+
this._cache.secondary = secondary;
|
|
83
|
+
}
|
|
84
|
+
get maxKeys() {
|
|
85
|
+
return this._maxKeys;
|
|
86
|
+
}
|
|
87
|
+
set maxKeys(maxKeys) {
|
|
88
|
+
this._maxKeys = maxKeys;
|
|
89
|
+
if (this._maxKeys > 0) {
|
|
90
|
+
this._cache.stats.enabled = true;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async set(key, value, ttl) {
|
|
94
|
+
if (this._maxKeys > 0) {
|
|
95
|
+
console.log(this._cache.stats.count, this._maxKeys);
|
|
96
|
+
if (this._cache.stats.count >= this._maxKeys) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const finalTtl = ttl ?? this._cache.ttl;
|
|
101
|
+
await this._cache.set(key.toString(), value, finalTtl);
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
async mset(list) {
|
|
105
|
+
const items = new Array();
|
|
106
|
+
for (const item of list) {
|
|
107
|
+
items.push({ key: item.key.toString(), value: item.value, ttl: item.ttl });
|
|
108
|
+
}
|
|
109
|
+
await this._cache.setMany(items);
|
|
110
|
+
}
|
|
111
|
+
async get(key) {
|
|
112
|
+
return this._cache.get(key.toString());
|
|
113
|
+
}
|
|
114
|
+
async mget(keys) {
|
|
115
|
+
const result = {};
|
|
116
|
+
for (const key of keys) {
|
|
117
|
+
result[key.toString()] = await this._cache.get(key.toString());
|
|
118
|
+
}
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
async del(key) {
|
|
122
|
+
return this._cache.delete(key.toString());
|
|
123
|
+
}
|
|
124
|
+
async mdel(keys) {
|
|
125
|
+
return this._cache.deleteMany(keys.map((key) => key.toString()));
|
|
126
|
+
}
|
|
127
|
+
async clear() {
|
|
128
|
+
return this._cache.clear();
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// src/index.ts
|
|
133
|
+
var NodeCacheErrors = /* @__PURE__ */ ((NodeCacheErrors2) => {
|
|
134
|
+
NodeCacheErrors2["ECACHEFULL"] = "Cache max keys amount exceeded";
|
|
135
|
+
NodeCacheErrors2["EKEYTYPE"] = "The key argument has to be of type `string` or `number`. Found: `__key`";
|
|
136
|
+
NodeCacheErrors2["EKEYSTYPE"] = "The keys argument has to be an array.";
|
|
137
|
+
NodeCacheErrors2["ETTLTYPE"] = "The ttl argument has to be a number.";
|
|
138
|
+
return NodeCacheErrors2;
|
|
139
|
+
})(NodeCacheErrors || {});
|
|
140
|
+
var NodeCache = class extends import_eventemitter3.default {
|
|
141
|
+
options = {
|
|
142
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
143
|
+
stdTTL: 0,
|
|
144
|
+
checkperiod: 600,
|
|
145
|
+
useClones: true,
|
|
146
|
+
deleteOnExpire: true,
|
|
147
|
+
maxKeys: -1
|
|
148
|
+
};
|
|
149
|
+
store = /* @__PURE__ */ new Map();
|
|
150
|
+
_stats = new import_cacheable2.CacheableStats({ enabled: true });
|
|
151
|
+
_cacheable = new import_cacheable2.CacheableMemory();
|
|
152
|
+
intervalId = 0;
|
|
153
|
+
constructor(options) {
|
|
154
|
+
super();
|
|
155
|
+
if (options) {
|
|
156
|
+
this.options = { ...this.options, ...options };
|
|
157
|
+
}
|
|
158
|
+
this.startInterval();
|
|
159
|
+
}
|
|
160
|
+
// Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
|
|
161
|
+
set(key, value, ttl) {
|
|
162
|
+
if (typeof key !== "string" && typeof key !== "number") {
|
|
163
|
+
throw this.createError("The key argument has to be of type `string` or `number`. Found: `__key`" /* EKEYTYPE */, key);
|
|
164
|
+
}
|
|
165
|
+
if (ttl && typeof ttl !== "number") {
|
|
166
|
+
throw this.createError("The ttl argument has to be a number." /* ETTLTYPE */, this.formatKey(key));
|
|
167
|
+
}
|
|
168
|
+
const keyValue = this.formatKey(key);
|
|
169
|
+
const ttlValue = ttl ?? this.options.stdTTL;
|
|
170
|
+
let expirationTimestamp = 0;
|
|
171
|
+
if (ttlValue && ttlValue > 0) {
|
|
172
|
+
expirationTimestamp = this.getExpirationTimestamp(ttlValue);
|
|
173
|
+
}
|
|
174
|
+
if (this.options.maxKeys) {
|
|
175
|
+
const maxKeys = this.options.maxKeys;
|
|
176
|
+
if (maxKeys > -1 && this.store.size >= maxKeys) {
|
|
177
|
+
throw this.createError("Cache max keys amount exceeded" /* ECACHEFULL */, this.formatKey(key));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
this.store.set(keyValue, { key: keyValue, value, ttl: expirationTimestamp });
|
|
181
|
+
this.emit("set", keyValue, value, ttlValue);
|
|
182
|
+
this._stats.incrementKSize(keyValue);
|
|
183
|
+
this._stats.incrementVSize(value);
|
|
184
|
+
this._stats.setCount(this.store.size);
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
// Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
|
|
188
|
+
mset(data) {
|
|
189
|
+
if (!Array.isArray(data)) {
|
|
190
|
+
throw this.createError("The keys argument has to be an array." /* EKEYSTYPE */);
|
|
191
|
+
}
|
|
192
|
+
for (const item of data) {
|
|
193
|
+
this.set(item.key, item.value, item.ttl);
|
|
194
|
+
}
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
// Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.
|
|
198
|
+
get(key) {
|
|
199
|
+
const result = this.store.get(this.formatKey(key));
|
|
200
|
+
if (result) {
|
|
201
|
+
if (result.ttl > 0) {
|
|
202
|
+
if (result.ttl < Date.now()) {
|
|
203
|
+
if (this.options.deleteOnExpire) {
|
|
204
|
+
this.del(key);
|
|
205
|
+
}
|
|
206
|
+
this._stats.incrementMisses();
|
|
207
|
+
this.emit("expired", this.formatKey(key), result.value);
|
|
208
|
+
return void 0;
|
|
209
|
+
}
|
|
210
|
+
this._stats.incrementHits();
|
|
211
|
+
if (this.options.useClones) {
|
|
212
|
+
return this._cacheable.clone(result.value);
|
|
213
|
+
}
|
|
214
|
+
return result.value;
|
|
215
|
+
}
|
|
216
|
+
this._stats.incrementHits();
|
|
217
|
+
if (this.options.useClones) {
|
|
218
|
+
return this._cacheable.clone(result.value);
|
|
219
|
+
}
|
|
220
|
+
return result.value;
|
|
221
|
+
}
|
|
222
|
+
this._stats.incrementMisses();
|
|
223
|
+
return void 0;
|
|
224
|
+
}
|
|
225
|
+
/*
|
|
226
|
+
Gets multiple saved values from the cache. Returns an empty object {} if not found or expired.
|
|
227
|
+
If the value was found it returns an object with the key value pair.
|
|
228
|
+
*/
|
|
229
|
+
mget(keys) {
|
|
230
|
+
const result = {};
|
|
231
|
+
for (const key of keys) {
|
|
232
|
+
const value = this.get(key);
|
|
233
|
+
if (value) {
|
|
234
|
+
result[this.formatKey(key)] = value;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return result;
|
|
238
|
+
}
|
|
239
|
+
/*
|
|
240
|
+
Get the cached value and remove the key from the cache.
|
|
241
|
+
Equivalent to calling get(key) + del(key).
|
|
242
|
+
Useful for implementing single use mechanism such as OTP, where once a value is read it will become obsolete.
|
|
243
|
+
*/
|
|
244
|
+
take(key) {
|
|
245
|
+
const result = this.get(key);
|
|
246
|
+
if (result) {
|
|
247
|
+
this.del(key);
|
|
248
|
+
if (this.options.useClones) {
|
|
249
|
+
return this._cacheable.clone(result);
|
|
250
|
+
}
|
|
251
|
+
return result;
|
|
252
|
+
}
|
|
253
|
+
return void 0;
|
|
254
|
+
}
|
|
255
|
+
// Delete a key. Returns the number of deleted entries. A delete will never fail.
|
|
256
|
+
del(key) {
|
|
257
|
+
if (Array.isArray(key)) {
|
|
258
|
+
return this.mdel(key);
|
|
259
|
+
}
|
|
260
|
+
const result = this.store.get(this.formatKey(key));
|
|
261
|
+
if (result) {
|
|
262
|
+
const keyValue = this.formatKey(key);
|
|
263
|
+
this.store.delete(keyValue);
|
|
264
|
+
this.emit("del", keyValue, result.value);
|
|
265
|
+
this._stats.decreaseKSize(keyValue);
|
|
266
|
+
this._stats.decreaseVSize(result.value);
|
|
267
|
+
this._stats.setCount(this.store.size);
|
|
268
|
+
return 1;
|
|
269
|
+
}
|
|
270
|
+
return 0;
|
|
271
|
+
}
|
|
272
|
+
// Delete all keys in Array that exist. Returns the number of deleted entries.
|
|
273
|
+
mdel(keys) {
|
|
274
|
+
let result = 0;
|
|
275
|
+
for (const key of keys) {
|
|
276
|
+
result += this.del(key);
|
|
277
|
+
}
|
|
278
|
+
return result;
|
|
279
|
+
}
|
|
280
|
+
// Redefine the ttl of a key. Returns true if the key has been found and changed.
|
|
281
|
+
// Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
|
|
282
|
+
ttl(key, ttl) {
|
|
283
|
+
const result = this.store.get(this.formatKey(key));
|
|
284
|
+
if (result) {
|
|
285
|
+
const ttlValue = ttl ?? this.options.stdTTL;
|
|
286
|
+
result.ttl = this.getExpirationTimestamp(ttlValue);
|
|
287
|
+
this.store.set(this.formatKey(key), result);
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
/*
|
|
293
|
+
Receive the ttl of a key. You will get:
|
|
294
|
+
|
|
295
|
+
undefined if the key does not exist
|
|
296
|
+
0 if this key has no ttl
|
|
297
|
+
a timestamp in ms representing the time at which the key will expire
|
|
298
|
+
*/
|
|
299
|
+
getTtl(key) {
|
|
300
|
+
const result = this.store.get(this.formatKey(key));
|
|
301
|
+
if (result) {
|
|
302
|
+
if (result.ttl === 0) {
|
|
303
|
+
return 0;
|
|
304
|
+
}
|
|
305
|
+
return result.ttl;
|
|
306
|
+
}
|
|
307
|
+
return void 0;
|
|
308
|
+
}
|
|
309
|
+
/*
|
|
310
|
+
Returns an array of all existing keys.
|
|
311
|
+
[ "all", "my", "keys", "foo", "bar" ]
|
|
312
|
+
*/
|
|
313
|
+
keys() {
|
|
314
|
+
const result = [];
|
|
315
|
+
for (const key of this.store.keys()) {
|
|
316
|
+
result.push(key);
|
|
317
|
+
}
|
|
318
|
+
return result;
|
|
319
|
+
}
|
|
320
|
+
// Returns boolean indicating if the key is cached.
|
|
321
|
+
has(key) {
|
|
322
|
+
return this.store.has(this.formatKey(key));
|
|
323
|
+
}
|
|
324
|
+
// Gets the stats of the cache.
|
|
325
|
+
getStats() {
|
|
326
|
+
const stats = {
|
|
327
|
+
keys: this._stats.count,
|
|
328
|
+
hits: this._stats.hits,
|
|
329
|
+
misses: this._stats.misses,
|
|
330
|
+
ksize: this._stats.ksize,
|
|
331
|
+
vsize: this._stats.vsize
|
|
332
|
+
};
|
|
333
|
+
return stats;
|
|
334
|
+
}
|
|
335
|
+
// Flush the whole data.
|
|
336
|
+
flushAll() {
|
|
337
|
+
this.store.clear();
|
|
338
|
+
this.flushStats();
|
|
339
|
+
this.emit("flush");
|
|
340
|
+
}
|
|
341
|
+
// Flush the stats
|
|
342
|
+
flushStats() {
|
|
343
|
+
this._stats = new import_cacheable2.CacheableStats({ enabled: true });
|
|
344
|
+
this.emit("flush_stats");
|
|
345
|
+
}
|
|
346
|
+
// Close the cache. This will clear the interval timeout which is set on check period option.
|
|
347
|
+
close() {
|
|
348
|
+
this.stopInterval();
|
|
349
|
+
}
|
|
350
|
+
// Get the interval id
|
|
351
|
+
getIntervalId() {
|
|
352
|
+
return this.intervalId;
|
|
353
|
+
}
|
|
354
|
+
formatKey(key) {
|
|
355
|
+
return key.toString();
|
|
356
|
+
}
|
|
357
|
+
getExpirationTimestamp(ttlInSeconds) {
|
|
358
|
+
const currentTimestamp = Date.now();
|
|
359
|
+
const ttlInMilliseconds = ttlInSeconds * 1e3;
|
|
360
|
+
const expirationTimestamp = currentTimestamp + ttlInMilliseconds;
|
|
361
|
+
return expirationTimestamp;
|
|
362
|
+
}
|
|
363
|
+
startInterval() {
|
|
364
|
+
if (this.options.checkperiod && this.options.checkperiod > 0) {
|
|
365
|
+
const checkPeriodinSeconds = this.options.checkperiod * 1e3;
|
|
366
|
+
this.intervalId = setInterval(() => {
|
|
367
|
+
this.checkData();
|
|
368
|
+
}, checkPeriodinSeconds);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
this.intervalId = 0;
|
|
372
|
+
}
|
|
373
|
+
checkData() {
|
|
374
|
+
for (const [key, value] of this.store.entries()) {
|
|
375
|
+
if (value.ttl > 0 && value.ttl < Date.now()) {
|
|
376
|
+
this.del(key);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
stopInterval() {
|
|
381
|
+
if (this.intervalId !== 0) {
|
|
382
|
+
clearInterval(this.intervalId);
|
|
383
|
+
this.intervalId = 0;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
createError(errorCode, key) {
|
|
387
|
+
let error = errorCode;
|
|
388
|
+
if (key) {
|
|
389
|
+
error = error.replace("__key", key);
|
|
390
|
+
}
|
|
391
|
+
return new Error(error);
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
395
|
+
0 && (module.exports = {
|
|
396
|
+
NodeCacheErrors,
|
|
397
|
+
NodeCacheStore
|
|
398
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import eventemitter from 'eventemitter3';
|
|
2
|
+
import { Cacheable } from 'cacheable';
|
|
3
|
+
import { Keyv } from 'keyv';
|
|
4
|
+
|
|
5
|
+
type NodeCacheStoreOptions = {
|
|
6
|
+
ttl?: number;
|
|
7
|
+
maxKeys?: number;
|
|
8
|
+
primary?: Keyv;
|
|
9
|
+
secondary?: Keyv;
|
|
10
|
+
};
|
|
11
|
+
declare class NodeCacheStore {
|
|
12
|
+
private _maxKeys;
|
|
13
|
+
private readonly _cache;
|
|
14
|
+
constructor(options?: NodeCacheStoreOptions);
|
|
15
|
+
get cache(): Cacheable;
|
|
16
|
+
get ttl(): number | undefined;
|
|
17
|
+
set ttl(ttl: number | undefined);
|
|
18
|
+
get primary(): Keyv;
|
|
19
|
+
set primary(primary: Keyv);
|
|
20
|
+
get secondary(): Keyv | undefined;
|
|
21
|
+
set secondary(secondary: Keyv | undefined);
|
|
22
|
+
get maxKeys(): number;
|
|
23
|
+
set maxKeys(maxKeys: number);
|
|
24
|
+
set(key: string | number, value: any, ttl?: number): Promise<boolean>;
|
|
25
|
+
mset(list: NodeCacheItem[]): Promise<void>;
|
|
26
|
+
get<T>(key: string | number): Promise<T | undefined>;
|
|
27
|
+
mget<T>(keys: Array<string | number>): Promise<Record<string, T | undefined>>;
|
|
28
|
+
del(key: string | number): Promise<boolean>;
|
|
29
|
+
mdel(keys: Array<string | number>): Promise<boolean>;
|
|
30
|
+
clear(): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type NodeCacheOptions = {
|
|
34
|
+
stdTTL?: number;
|
|
35
|
+
checkperiod?: number;
|
|
36
|
+
useClones?: boolean;
|
|
37
|
+
deleteOnExpire?: boolean;
|
|
38
|
+
maxKeys?: number;
|
|
39
|
+
};
|
|
40
|
+
type NodeCacheItem = {
|
|
41
|
+
key: string | number;
|
|
42
|
+
value: any;
|
|
43
|
+
ttl?: number;
|
|
44
|
+
};
|
|
45
|
+
declare enum NodeCacheErrors {
|
|
46
|
+
ECACHEFULL = "Cache max keys amount exceeded",
|
|
47
|
+
EKEYTYPE = "The key argument has to be of type `string` or `number`. Found: `__key`",
|
|
48
|
+
EKEYSTYPE = "The keys argument has to be an array.",
|
|
49
|
+
ETTLTYPE = "The ttl argument has to be a number."
|
|
50
|
+
}
|
|
51
|
+
type NodeCacheStats = {
|
|
52
|
+
keys: number;
|
|
53
|
+
hits: number;
|
|
54
|
+
misses: number;
|
|
55
|
+
ksize: number;
|
|
56
|
+
vsize: number;
|
|
57
|
+
};
|
|
58
|
+
declare class NodeCache extends eventemitter {
|
|
59
|
+
readonly options: NodeCacheOptions;
|
|
60
|
+
readonly store: Map<string, any>;
|
|
61
|
+
private _stats;
|
|
62
|
+
private readonly _cacheable;
|
|
63
|
+
private intervalId;
|
|
64
|
+
constructor(options?: NodeCacheOptions);
|
|
65
|
+
set(key: string | number, value: any, ttl?: number): boolean;
|
|
66
|
+
mset(data: NodeCacheItem[]): boolean;
|
|
67
|
+
get(key: string | number): any;
|
|
68
|
+
mget(keys: Array<string | number>): Record<string, unknown>;
|
|
69
|
+
take(key: string | number): any;
|
|
70
|
+
del(key: string | number | Array<string | number>): number;
|
|
71
|
+
mdel(keys: Array<string | number>): number;
|
|
72
|
+
ttl(key: string | number, ttl?: number): boolean;
|
|
73
|
+
getTtl(key: string | number): number | undefined;
|
|
74
|
+
keys(): string[];
|
|
75
|
+
has(key: string | number): boolean;
|
|
76
|
+
getStats(): NodeCacheStats;
|
|
77
|
+
flushAll(): void;
|
|
78
|
+
flushStats(): void;
|
|
79
|
+
close(): void;
|
|
80
|
+
getIntervalId(): number | NodeJS.Timeout;
|
|
81
|
+
private formatKey;
|
|
82
|
+
private getExpirationTimestamp;
|
|
83
|
+
private startInterval;
|
|
84
|
+
private checkData;
|
|
85
|
+
private stopInterval;
|
|
86
|
+
private createError;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { NodeCacheErrors, type NodeCacheItem, type NodeCacheOptions, type NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, NodeCache as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,33 +1,65 @@
|
|
|
1
1
|
import eventemitter from 'eventemitter3';
|
|
2
|
-
|
|
2
|
+
import { Cacheable } from 'cacheable';
|
|
3
|
+
import { Keyv } from 'keyv';
|
|
4
|
+
|
|
5
|
+
type NodeCacheStoreOptions = {
|
|
6
|
+
ttl?: number;
|
|
7
|
+
maxKeys?: number;
|
|
8
|
+
primary?: Keyv;
|
|
9
|
+
secondary?: Keyv;
|
|
10
|
+
};
|
|
11
|
+
declare class NodeCacheStore {
|
|
12
|
+
private _maxKeys;
|
|
13
|
+
private readonly _cache;
|
|
14
|
+
constructor(options?: NodeCacheStoreOptions);
|
|
15
|
+
get cache(): Cacheable;
|
|
16
|
+
get ttl(): number | undefined;
|
|
17
|
+
set ttl(ttl: number | undefined);
|
|
18
|
+
get primary(): Keyv;
|
|
19
|
+
set primary(primary: Keyv);
|
|
20
|
+
get secondary(): Keyv | undefined;
|
|
21
|
+
set secondary(secondary: Keyv | undefined);
|
|
22
|
+
get maxKeys(): number;
|
|
23
|
+
set maxKeys(maxKeys: number);
|
|
24
|
+
set(key: string | number, value: any, ttl?: number): Promise<boolean>;
|
|
25
|
+
mset(list: NodeCacheItem[]): Promise<void>;
|
|
26
|
+
get<T>(key: string | number): Promise<T | undefined>;
|
|
27
|
+
mget<T>(keys: Array<string | number>): Promise<Record<string, T | undefined>>;
|
|
28
|
+
del(key: string | number): Promise<boolean>;
|
|
29
|
+
mdel(keys: Array<string | number>): Promise<boolean>;
|
|
30
|
+
clear(): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type NodeCacheOptions = {
|
|
3
34
|
stdTTL?: number;
|
|
4
35
|
checkperiod?: number;
|
|
5
36
|
useClones?: boolean;
|
|
6
37
|
deleteOnExpire?: boolean;
|
|
7
38
|
maxKeys?: number;
|
|
8
39
|
};
|
|
9
|
-
|
|
40
|
+
type NodeCacheItem = {
|
|
10
41
|
key: string | number;
|
|
11
42
|
value: any;
|
|
12
43
|
ttl?: number;
|
|
13
44
|
};
|
|
14
|
-
|
|
45
|
+
declare enum NodeCacheErrors {
|
|
15
46
|
ECACHEFULL = "Cache max keys amount exceeded",
|
|
16
47
|
EKEYTYPE = "The key argument has to be of type `string` or `number`. Found: `__key`",
|
|
17
48
|
EKEYSTYPE = "The keys argument has to be an array.",
|
|
18
49
|
ETTLTYPE = "The ttl argument has to be a number."
|
|
19
50
|
}
|
|
20
|
-
|
|
51
|
+
type NodeCacheStats = {
|
|
21
52
|
keys: number;
|
|
22
53
|
hits: number;
|
|
23
54
|
misses: number;
|
|
24
55
|
ksize: number;
|
|
25
56
|
vsize: number;
|
|
26
57
|
};
|
|
27
|
-
|
|
58
|
+
declare class NodeCache extends eventemitter {
|
|
28
59
|
readonly options: NodeCacheOptions;
|
|
29
60
|
readonly store: Map<string, any>;
|
|
30
61
|
private _stats;
|
|
62
|
+
private readonly _cacheable;
|
|
31
63
|
private intervalId;
|
|
32
64
|
constructor(options?: NodeCacheOptions);
|
|
33
65
|
set(key: string | number, value: any, ttl?: number): boolean;
|
|
@@ -48,15 +80,10 @@ export default class NodeCache extends eventemitter {
|
|
|
48
80
|
getIntervalId(): number | NodeJS.Timeout;
|
|
49
81
|
private formatKey;
|
|
50
82
|
private getExpirationTimestamp;
|
|
51
|
-
private addHit;
|
|
52
|
-
private addMiss;
|
|
53
|
-
private roughSizeOfKey;
|
|
54
|
-
private roughSizeOfObject;
|
|
55
83
|
private startInterval;
|
|
56
84
|
private checkData;
|
|
57
85
|
private stopInterval;
|
|
58
86
|
private createError;
|
|
59
|
-
private isPrimitive;
|
|
60
|
-
private clone;
|
|
61
87
|
}
|
|
62
|
-
|
|
88
|
+
|
|
89
|
+
export { NodeCacheErrors, type NodeCacheItem, type NodeCacheOptions, type NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, NodeCache as default };
|