@cacheable/node-cache 3.0.1 → 3.1.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/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@cacheable/node-cache",
3
- "version": "3.0.1",
3
+ "version": "3.1.1",
4
4
  "description": "Simple and Maintained fast NodeJS internal caching",
5
5
  "type": "module",
6
- "main": "./dist/index.js",
7
- "module": "./dist/index.js",
8
- "types": "./dist/index.d.ts",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.mjs",
8
+ "types": "./dist/index.d.mts",
9
9
  "exports": {
10
10
  ".": {
11
11
  "import": {
12
- "types": "./dist/index.d.ts",
13
- "default": "./dist/index.js"
12
+ "types": "./dist/index.d.mts",
13
+ "default": "./dist/index.mjs"
14
14
  },
15
15
  "require": {
16
16
  "types": "./dist/index.d.cts",
@@ -19,7 +19,7 @@
19
19
  }
20
20
  },
21
21
  "engines": {
22
- "node": ">=18"
22
+ "node": ">=22"
23
23
  },
24
24
  "repository": {
25
25
  "type": "git",
@@ -40,20 +40,20 @@
40
40
  "cacheable-node"
41
41
  ],
42
42
  "devDependencies": {
43
- "tsup": "^8.5.1",
43
+ "tsdown": "^0.22.0",
44
44
  "typescript": "^5.9.3"
45
45
  },
46
46
  "dependencies": {
47
47
  "hookified": "^2.1.0",
48
48
  "keyv": "^5.6.0",
49
- "@cacheable/utils": "^2.4.1"
49
+ "@cacheable/utils": "^2.5.0"
50
50
  },
51
51
  "files": [
52
52
  "dist",
53
53
  "license"
54
54
  ],
55
55
  "scripts": {
56
- "build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean",
56
+ "build": "rimraf ./dist && tsdown src/index.ts --format cjs,esm --dts --clean",
57
57
  "lint": "biome check --write --error-on-warnings",
58
58
  "test": "pnpm lint && vitest run --coverage",
59
59
  "test:ci": "biome check --error-on-warnings && vitest run --coverage",
package/dist/index.d.ts DELETED
@@ -1,346 +0,0 @@
1
- import { Hookified } from 'hookified';
2
- import Keyv from 'keyv';
3
-
4
- type NodeCacheStoreOptions<T> = {
5
- /**
6
- * Time to live in milliseconds. This is a breaking change from the original NodeCache.
7
- */
8
- ttl?: number | string;
9
- /**
10
- * The Keyv store instance.
11
- */
12
- store?: Keyv<T>;
13
- };
14
- declare class NodeCacheStore<T> extends Hookified {
15
- private _keyv;
16
- private _ttl?;
17
- private _stats;
18
- constructor(options?: NodeCacheStoreOptions<T>);
19
- /**
20
- * Time to live in milliseconds.
21
- * @returns {number | string | undefined}
22
- * @readonly
23
- */
24
- get ttl(): number | string | undefined;
25
- /**
26
- * Time to live in milliseconds.
27
- * @param {number | string | undefined} ttl
28
- */
29
- set ttl(ttl: number | string | undefined);
30
- /**
31
- * The Keyv store instance.
32
- * @returns {Keyv<T>}
33
- * @readonly
34
- */
35
- get store(): Keyv<T>;
36
- /**
37
- * Set a key/value pair in the cache.
38
- * @param {string | number} key
39
- * @param {T} value
40
- * @param {number} [ttl]
41
- * @returns {boolean}
42
- */
43
- set(key: string | number, value: T, ttl?: number | string): Promise<boolean>;
44
- /**
45
- * Set multiple key/value pairs in the cache.
46
- * @param {PartialNodeCacheItem[]} list
47
- * @returns {void}
48
- */
49
- mset(list: Array<PartialNodeCacheItem<T>>): Promise<void>;
50
- /**
51
- * Get a value from the cache.
52
- * @param {string | number} key
53
- * @returns {any | undefined}
54
- */
55
- get<V = T>(key: string | number): Promise<V | undefined>;
56
- /**
57
- * Get multiple values from the cache.
58
- * @param {Array<string | number>} keys
59
- * @returns {Record<string, any | undefined>}
60
- */
61
- mget<V = T>(keys: Array<string | number>): Promise<Record<string, V | undefined>>;
62
- /**
63
- * Delete a key from the cache.
64
- * @param {string | number} key
65
- * @returns {boolean}
66
- */
67
- del(key: string | number): Promise<boolean>;
68
- /**
69
- * Delete multiple keys from the cache.
70
- * @param {Array<string | number>} keys
71
- * @returns {boolean}
72
- */
73
- mdel(keys: Array<string | number>): Promise<boolean>;
74
- /**
75
- * Clear the cache.
76
- * @returns {void}
77
- */
78
- clear(): Promise<void>;
79
- /**
80
- * Set the TTL of an existing key in the cache.
81
- * @param {string | number} key
82
- * @param {number | string} [ttl]
83
- * @returns {boolean}
84
- */
85
- setTtl(key: string | number, ttl?: number | string): Promise<boolean>;
86
- /**
87
- * Get a key from the cache and delete it. If it does exist it will get the value and delete the item from the cache.
88
- * @param {string | number} key
89
- * @returns {T | undefined}
90
- */
91
- take<V = T>(key: string | number): Promise<V | undefined>;
92
- /**
93
- * Gets the stats of the cache
94
- * @returns {NodeCacheStats} the stats of the cache
95
- */
96
- getStats(): NodeCacheStats;
97
- /**
98
- * Flush the stats.
99
- * @returns {void}
100
- */
101
- flushStats(): void;
102
- /**
103
- * Disconnect from the cache.
104
- * @returns {void}
105
- */
106
- disconnect(): Promise<void>;
107
- /**
108
- * Resolve the TTL to milliseconds.
109
- * @param {number | string | undefined} ttl
110
- * @returns {number | undefined}
111
- */
112
- private resolveTtl;
113
- }
114
-
115
- type NodeCacheOptions = {
116
- /**
117
- * 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
118
- */
119
- stdTTL?: number | string;
120
- /**
121
- * The interval to check for expired items in seconds. Default is 600 = 5 minutes
122
- */
123
- checkperiod?: number;
124
- /**
125
- * If set to true (Default Setting), the cache will clone the returned items via get() functions.
126
- * This means that every time you set a value into the cache, node-cache makes a deep clone of it.
127
- * When you get that value back, you receive another deep clone.
128
- * This mimics the behavior of an external cache like Redis or Memcached, meaning mutations to the
129
- * returned object do not affect the cached copy (and vice versa). If set to false, the original
130
- * object will be returned, and mutations will affect the cached copy.
131
- */
132
- useClones?: boolean;
133
- /**
134
- * Delete expired items during an interval check or a single item on a get request. Default is true.
135
- */
136
- deleteOnExpire?: boolean;
137
- /**
138
- * The maximum number of keys that will be stored in the cache. Default is -1 = unlimited
139
- * If the limit is reached, it will no longer add any new items until some expire.
140
- */
141
- maxKeys?: number;
142
- };
143
- type PartialNodeCacheItem<T> = {
144
- /**
145
- * The key of the item
146
- */
147
- key: string | number;
148
- /**
149
- * The value of the item
150
- */
151
- value: T;
152
- /**
153
- * The ttl of the item in seconds. 0 = unlimited
154
- */
155
- ttl?: number;
156
- };
157
- type NodeCacheItem<T> = PartialNodeCacheItem<T> & {
158
- /**
159
- * The ttl of the item in milliseconds. 0 = unlimited
160
- */
161
- ttl: number;
162
- };
163
- declare enum NodeCacheErrors {
164
- ECACHEFULL = "Cache max keys amount exceeded",
165
- EKEYTYPE = "The key argument has to be of type `string` or `number`. Found: `__key`",
166
- EKEYSTYPE = "The keys argument has to be an array.",
167
- ETTLTYPE = "The ttl argument has to be a number or a string for shorthand ttl."
168
- }
169
- type NodeCacheStats = {
170
- /**
171
- * The number of keys stored in the cache
172
- */
173
- keys: number;
174
- /**
175
- * The number of hits
176
- */
177
- hits: number;
178
- /**
179
- * The number of misses
180
- */
181
- misses: number;
182
- /**
183
- * The global key size count in approximately bytes
184
- */
185
- ksize: number;
186
- /**
187
- * The global value size count in approximately bytes
188
- */
189
- vsize: number;
190
- };
191
- declare class NodeCache<T> extends Hookified {
192
- readonly options: NodeCacheOptions;
193
- readonly store: Map<string, NodeCacheItem<T>>;
194
- private _stats;
195
- private intervalId;
196
- constructor(options?: NodeCacheOptions);
197
- /**
198
- * Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
199
- *
200
- * TTL behavior:
201
- * - `ttl > 0`: cache expires after the given number of seconds
202
- * - `ttl === 0`: cache indefinitely (overrides stdTTL)
203
- * - `ttl < 0`: store the value but it expires immediately on next access (matches original node-cache behavior)
204
- * - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
205
- * - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
206
- *
207
- * @param {string | number} key - it will convert the key to a string
208
- * @param {T} value
209
- * @param {number | string} [ttl] - TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
210
- * @returns {boolean} true on success
211
- * @throws {Error} If the `key` or `ttl` is of an invalid type.
212
- */
213
- set(key: string | number, value: T, ttl?: number | string): boolean;
214
- /**
215
- * Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success.
216
- *
217
- * Each item follows the same TTL behavior as `set()`:
218
- * - Positive TTL: expires after the given seconds
219
- * - `0`: cache indefinitely
220
- * - Negative TTL: stored but expires immediately on next access
221
- * - Omitted: uses `stdTTL` from options
222
- *
223
- * @param {PartialNodeCacheItem<T>[]} data an array of key value pairs with optional ttl
224
- * @returns {boolean} true on success
225
- * @throws {Error} If `data` is not an array, or if any item has an invalid key or ttl type.
226
- */
227
- mset(data: Array<PartialNodeCacheItem<T>>): boolean;
228
- /**
229
- * Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.
230
- * @param {string | number} key if the key is a number it will convert it to a string
231
- * @returns {T} the value or undefined
232
- */
233
- get(key: string | number): T | undefined;
234
- /**
235
- * Gets multiple saved values from the cache. Returns an empty object {} if not found or expired.
236
- * If the value was found it returns an object with the key value pair.
237
- * @param {Array<string | number} keys an array of keys
238
- * @returns {Record<string, T | undefined>} an object with the key as a property and the value as the value
239
- */
240
- mget<V = T>(keys: Array<string | number>): Record<string, V | undefined>;
241
- /**
242
- * Get the cached value and remove the key from the cache. Equivalent to calling get(key) + del(key).
243
- * Useful for implementing single use mechanism such as OTP, where once a value is read it will become obsolete.
244
- * @param {string | number} key
245
- * @returns {T | undefined} the value or undefined
246
- */
247
- take<V = T>(key: string | number): V | undefined;
248
- /**
249
- * Delete a key. Returns the number of deleted entries. A delete will never fail.
250
- * @param {string | number | Array<string | number>} key if the key is a number it will convert it to a string. if an array is passed it will delete all keys in the array.
251
- * @returns {number} if it was successful it will return the count that was deleted
252
- */
253
- del(key: string | number | Array<string | number>): number;
254
- /**
255
- * Delete all keys in Array that exist. Returns the number of deleted entries.
256
- * @param {Array<string | number>} keys an array of keys
257
- * @returns {number} the count of deleted keys
258
- */
259
- mdel(keys: Array<string | number>): number;
260
- /**
261
- * Redefine the ttl of a key. Returns true if the key has been found and changed.
262
- * Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
263
- *
264
- * TTL behavior:
265
- * - `ttl > 0`: key expires after the given number of seconds
266
- * - `ttl === 0`: key lives indefinitely (overrides stdTTL)
267
- * - `ttl < 0`: key expires immediately on next access (matches original node-cache behavior)
268
- * - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set)
269
- * - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d'
270
- *
271
- * @param {string | number} key if the key is a number it will convert it to a string
272
- * @param {number | string} [ttl] TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format
273
- * @returns {boolean} true if the key has been found and changed. Otherwise returns false.
274
- * @throws {Error} If the `ttl` is of an invalid type (must be a number or string).
275
- */
276
- ttl(key: string | number, ttl?: number | string): boolean;
277
- /**
278
- * Receive the ttl of a key.
279
- * @param {string | number} key if the key is a number it will convert it to a string
280
- * @returns {number | undefined} 0 if this key has no ttl, undefined if this key is not in the cache,
281
- * a timestamp in ms representing the time at which this key will expire
282
- */
283
- getTtl(key: string | number): number | undefined;
284
- /**
285
- * Returns an array of all existing keys. [ "all", "my", "keys", "foo", "bar" ]
286
- * @returns {string[]} an array of all keys
287
- */
288
- keys(): string[];
289
- /**
290
- * Returns boolean indicating if the key is cached. If the key has expired, it
291
- * will return false and the key will be removed from the cache when
292
- * `deleteOnExpire` is enabled (matches the original node-cache behavior).
293
- * @param {string | number} key if the key is a number it will convert it to a string
294
- * @returns {boolean} true if the key is cached and not expired
295
- */
296
- has(key: string | number): boolean;
297
- /**
298
- * Gets the stats of the cache
299
- * @returns {NodeCacheStats} the stats of the cache
300
- */
301
- getStats(): NodeCacheStats;
302
- /**
303
- * Flush the whole data.
304
- * @returns {void}
305
- */
306
- flushAll(): void;
307
- /**
308
- * Flush the stats.
309
- * @returns {void}
310
- */
311
- flushStats(): void;
312
- /**
313
- * Close the cache. This will clear the interval timeout which is set on check period option.
314
- * @returns {void}
315
- */
316
- close(): void;
317
- /**
318
- * Get the interval id
319
- * @returns {number | NodeJS.Timeout} the interval id
320
- */
321
- getIntervalId(): number | NodeJS.Timeout;
322
- startInterval(): void;
323
- stopInterval(): void;
324
- private formatKey;
325
- private clone;
326
- private getExpirationTimestamp;
327
- /**
328
- * Resolves a TTL value to an expiration timestamp, returning 0 (unlimited) if the
329
- * resolved timestamp is not in the future (e.g. "0s" or a zero-duration string).
330
- */
331
- private resolveExpiration;
332
- /**
333
- * Checks whether a TTL value is negative. Handles both numbers and
334
- * purely numeric strings (e.g. "-1").
335
- */
336
- private isNegativeTtl;
337
- private checkData;
338
- /**
339
- * Handles expiration for a cache entry. Deletes the entry when
340
- * `deleteOnExpire` is enabled and emits the "expired" event.
341
- */
342
- private handleExpired;
343
- private createError;
344
- }
345
-
346
- export { NodeCache, NodeCacheErrors, type NodeCacheItem, type NodeCacheOptions, type NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, type PartialNodeCacheItem, NodeCache as default };