cacheable 1.3.0 → 1.5.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/README.md +15 -3
- package/dist/index.cjs +39 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +39 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -155,13 +155,13 @@ _This does not enable statistics for your layer 2 cache as that is a distributed
|
|
|
155
155
|
|
|
156
156
|
* `set(key, value, ttl? | [{string, string, ttl?}])`: Sets a value in the cache.
|
|
157
157
|
* `setMany([{key, value, ttl?}])`: Sets multiple values in the cache.
|
|
158
|
-
* `get(key
|
|
158
|
+
* `get(key)`: Gets a value from the cache.
|
|
159
159
|
* `getMany([keys])`: Gets multiple values from the cache.
|
|
160
|
-
* `has(key
|
|
160
|
+
* `has(key)`: Checks if a value exists in the cache.
|
|
161
161
|
* `hasMany([keys])`: Checks if multiple values exist in the cache.
|
|
162
162
|
* `take(key)`: Takes a value from the cache and deletes it.
|
|
163
163
|
* `takeMany([keys])`: Takes multiple values from the cache and deletes them.
|
|
164
|
-
* `delete(key
|
|
164
|
+
* `delete(key)`: Deletes a value from the cache.
|
|
165
165
|
* `deleteMany([keys])`: Deletes multiple values from the cache.
|
|
166
166
|
* `clear()`: Clears the cache stores. Be careful with this as it will clear both layer 1 and layer 2.
|
|
167
167
|
* `wrap(function, options)`: Wraps a function in a cache. (coming soon)
|
|
@@ -195,6 +195,15 @@ You can use `CacheableMemory` as a standalone cache or as a primary store for `c
|
|
|
195
195
|
|
|
196
196
|
This simple in-memory cache uses multiple Map objects and a with `expiration` and `lru` policies if set to manage the in memory cache at scale.
|
|
197
197
|
|
|
198
|
+
By default we use lazy expiration deletion which means on `get` and `getMany` type functions we look if it is expired and then delete it. If you want to have a more aggressive expiration policy you can set the `checkInterval` property to a value greater than `0` which will check for expired keys at the interval you set.
|
|
199
|
+
|
|
200
|
+
### CacheableMemory Options
|
|
201
|
+
|
|
202
|
+
* `ttl`: The time to live for the cache in milliseconds.
|
|
203
|
+
* `useClones`: If the cache should use clones for the values. Default is `true`.
|
|
204
|
+
* `lruSize`: The size of the LRU cache. Default is `0` which is unlimited.
|
|
205
|
+
* `checkInterval`: The interval to check for expired keys in milliseconds. Default is `0` which is disabled.
|
|
206
|
+
|
|
198
207
|
### CacheableMemory API
|
|
199
208
|
|
|
200
209
|
* `set(key, value, ttl?)`: Sets a value in the cache.
|
|
@@ -204,6 +213,9 @@ This simple in-memory cache uses multiple Map objects and a with `expiration` an
|
|
|
204
213
|
* `clear()`: Clears the cache.
|
|
205
214
|
* `size()`: The number of keys in the cache.
|
|
206
215
|
* `keys()`: The keys in the cache.
|
|
216
|
+
* `checkExpired()`: Checks for expired keys in the cache. This is used by the `checkInterval` property.
|
|
217
|
+
* `startIntervalCheck()`: Starts the interval check for expired keys if `checkInterval` is above 0 ms.
|
|
218
|
+
* `stopIntervalCheck()`: Stops the interval check for expired keys.
|
|
207
219
|
|
|
208
220
|
|
|
209
221
|
## How to Contribute
|
package/dist/index.cjs
CHANGED
|
@@ -293,8 +293,15 @@ var CacheableMemory = class {
|
|
|
293
293
|
_hash9 = /* @__PURE__ */ new Map();
|
|
294
294
|
_lru = new DoublyLinkedList();
|
|
295
295
|
_ttl = 0;
|
|
296
|
+
// Turned off by default
|
|
296
297
|
_useClone = true;
|
|
298
|
+
// Turned on by default
|
|
297
299
|
_lruSize = 0;
|
|
300
|
+
// Turned off by default
|
|
301
|
+
_checkInterval = 0;
|
|
302
|
+
// Turned off by default
|
|
303
|
+
_interval = 0;
|
|
304
|
+
// Turned off by default
|
|
298
305
|
constructor(options) {
|
|
299
306
|
if (options?.ttl) {
|
|
300
307
|
this._ttl = options.ttl;
|
|
@@ -305,6 +312,10 @@ var CacheableMemory = class {
|
|
|
305
312
|
if (options?.lruSize) {
|
|
306
313
|
this._lruSize = options.lruSize;
|
|
307
314
|
}
|
|
315
|
+
if (options?.checkInterval) {
|
|
316
|
+
this._checkInterval = options.checkInterval;
|
|
317
|
+
}
|
|
318
|
+
this.startIntervalCheck();
|
|
308
319
|
}
|
|
309
320
|
get ttl() {
|
|
310
321
|
return this._ttl;
|
|
@@ -325,6 +336,12 @@ var CacheableMemory = class {
|
|
|
325
336
|
this._lruSize = value;
|
|
326
337
|
this.lruResize();
|
|
327
338
|
}
|
|
339
|
+
get checkInterval() {
|
|
340
|
+
return this._checkInterval;
|
|
341
|
+
}
|
|
342
|
+
set checkInterval(value) {
|
|
343
|
+
this._checkInterval = value;
|
|
344
|
+
}
|
|
328
345
|
get size() {
|
|
329
346
|
return this._hash0.size + this._hash1.size + this._hash2.size + this._hash3.size + this._hash4.size + this._hash5.size + this._hash6.size + this._hash7.size + this._hash8.size + this._hash9.size;
|
|
330
347
|
}
|
|
@@ -482,6 +499,28 @@ var CacheableMemory = class {
|
|
|
482
499
|
}
|
|
483
500
|
}
|
|
484
501
|
}
|
|
502
|
+
checkExpiration() {
|
|
503
|
+
const stores = this.concatStores();
|
|
504
|
+
for (const item of stores.values()) {
|
|
505
|
+
if (item.expires && Date.now() > item.expires) {
|
|
506
|
+
this.delete(item.key);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
startIntervalCheck() {
|
|
511
|
+
if (this._checkInterval > 0) {
|
|
512
|
+
this._interval = setInterval(() => {
|
|
513
|
+
this.checkExpiration();
|
|
514
|
+
}, this._checkInterval);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
stopIntervalCheck() {
|
|
518
|
+
if (this._interval) {
|
|
519
|
+
clearInterval(this._interval);
|
|
520
|
+
}
|
|
521
|
+
this._interval = 0;
|
|
522
|
+
this._checkInterval = 0;
|
|
523
|
+
}
|
|
485
524
|
isPrimitive(value) {
|
|
486
525
|
const result = false;
|
|
487
526
|
if (value === null || value === void 0) {
|
package/dist/index.d.cts
CHANGED
|
@@ -50,6 +50,7 @@ type CacheableMemoryOptions = {
|
|
|
50
50
|
ttl?: number;
|
|
51
51
|
useClone?: boolean;
|
|
52
52
|
lruSize?: number;
|
|
53
|
+
checkInterval?: number;
|
|
53
54
|
};
|
|
54
55
|
declare class CacheableMemory {
|
|
55
56
|
private readonly _hashCache;
|
|
@@ -67,6 +68,8 @@ declare class CacheableMemory {
|
|
|
67
68
|
private _ttl;
|
|
68
69
|
private _useClone;
|
|
69
70
|
private _lruSize;
|
|
71
|
+
private _checkInterval;
|
|
72
|
+
private _interval;
|
|
70
73
|
constructor(options?: CacheableMemoryOptions);
|
|
71
74
|
get ttl(): number;
|
|
72
75
|
set ttl(value: number);
|
|
@@ -74,6 +77,8 @@ declare class CacheableMemory {
|
|
|
74
77
|
set useClone(value: boolean);
|
|
75
78
|
get lruSize(): number;
|
|
76
79
|
set lruSize(value: number);
|
|
80
|
+
get checkInterval(): number;
|
|
81
|
+
set checkInterval(value: number);
|
|
77
82
|
get size(): number;
|
|
78
83
|
get keys(): IterableIterator<string>;
|
|
79
84
|
get<T>(key: string): any;
|
|
@@ -88,6 +93,9 @@ declare class CacheableMemory {
|
|
|
88
93
|
lruAddToFront(key: string): void;
|
|
89
94
|
lruMoveToFront(key: string): void;
|
|
90
95
|
lruResize(): void;
|
|
96
|
+
checkExpiration(): void;
|
|
97
|
+
startIntervalCheck(): void;
|
|
98
|
+
stopIntervalCheck(): void;
|
|
91
99
|
private isPrimitive;
|
|
92
100
|
private concatStores;
|
|
93
101
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -50,6 +50,7 @@ type CacheableMemoryOptions = {
|
|
|
50
50
|
ttl?: number;
|
|
51
51
|
useClone?: boolean;
|
|
52
52
|
lruSize?: number;
|
|
53
|
+
checkInterval?: number;
|
|
53
54
|
};
|
|
54
55
|
declare class CacheableMemory {
|
|
55
56
|
private readonly _hashCache;
|
|
@@ -67,6 +68,8 @@ declare class CacheableMemory {
|
|
|
67
68
|
private _ttl;
|
|
68
69
|
private _useClone;
|
|
69
70
|
private _lruSize;
|
|
71
|
+
private _checkInterval;
|
|
72
|
+
private _interval;
|
|
70
73
|
constructor(options?: CacheableMemoryOptions);
|
|
71
74
|
get ttl(): number;
|
|
72
75
|
set ttl(value: number);
|
|
@@ -74,6 +77,8 @@ declare class CacheableMemory {
|
|
|
74
77
|
set useClone(value: boolean);
|
|
75
78
|
get lruSize(): number;
|
|
76
79
|
set lruSize(value: number);
|
|
80
|
+
get checkInterval(): number;
|
|
81
|
+
set checkInterval(value: number);
|
|
77
82
|
get size(): number;
|
|
78
83
|
get keys(): IterableIterator<string>;
|
|
79
84
|
get<T>(key: string): any;
|
|
@@ -88,6 +93,9 @@ declare class CacheableMemory {
|
|
|
88
93
|
lruAddToFront(key: string): void;
|
|
89
94
|
lruMoveToFront(key: string): void;
|
|
90
95
|
lruResize(): void;
|
|
96
|
+
checkExpiration(): void;
|
|
97
|
+
startIntervalCheck(): void;
|
|
98
|
+
stopIntervalCheck(): void;
|
|
91
99
|
private isPrimitive;
|
|
92
100
|
private concatStores;
|
|
93
101
|
}
|
package/dist/index.js
CHANGED
|
@@ -265,8 +265,15 @@ var CacheableMemory = class {
|
|
|
265
265
|
_hash9 = /* @__PURE__ */ new Map();
|
|
266
266
|
_lru = new DoublyLinkedList();
|
|
267
267
|
_ttl = 0;
|
|
268
|
+
// Turned off by default
|
|
268
269
|
_useClone = true;
|
|
270
|
+
// Turned on by default
|
|
269
271
|
_lruSize = 0;
|
|
272
|
+
// Turned off by default
|
|
273
|
+
_checkInterval = 0;
|
|
274
|
+
// Turned off by default
|
|
275
|
+
_interval = 0;
|
|
276
|
+
// Turned off by default
|
|
270
277
|
constructor(options) {
|
|
271
278
|
if (options?.ttl) {
|
|
272
279
|
this._ttl = options.ttl;
|
|
@@ -277,6 +284,10 @@ var CacheableMemory = class {
|
|
|
277
284
|
if (options?.lruSize) {
|
|
278
285
|
this._lruSize = options.lruSize;
|
|
279
286
|
}
|
|
287
|
+
if (options?.checkInterval) {
|
|
288
|
+
this._checkInterval = options.checkInterval;
|
|
289
|
+
}
|
|
290
|
+
this.startIntervalCheck();
|
|
280
291
|
}
|
|
281
292
|
get ttl() {
|
|
282
293
|
return this._ttl;
|
|
@@ -297,6 +308,12 @@ var CacheableMemory = class {
|
|
|
297
308
|
this._lruSize = value;
|
|
298
309
|
this.lruResize();
|
|
299
310
|
}
|
|
311
|
+
get checkInterval() {
|
|
312
|
+
return this._checkInterval;
|
|
313
|
+
}
|
|
314
|
+
set checkInterval(value) {
|
|
315
|
+
this._checkInterval = value;
|
|
316
|
+
}
|
|
300
317
|
get size() {
|
|
301
318
|
return this._hash0.size + this._hash1.size + this._hash2.size + this._hash3.size + this._hash4.size + this._hash5.size + this._hash6.size + this._hash7.size + this._hash8.size + this._hash9.size;
|
|
302
319
|
}
|
|
@@ -454,6 +471,28 @@ var CacheableMemory = class {
|
|
|
454
471
|
}
|
|
455
472
|
}
|
|
456
473
|
}
|
|
474
|
+
checkExpiration() {
|
|
475
|
+
const stores = this.concatStores();
|
|
476
|
+
for (const item of stores.values()) {
|
|
477
|
+
if (item.expires && Date.now() > item.expires) {
|
|
478
|
+
this.delete(item.key);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
startIntervalCheck() {
|
|
483
|
+
if (this._checkInterval > 0) {
|
|
484
|
+
this._interval = setInterval(() => {
|
|
485
|
+
this.checkExpiration();
|
|
486
|
+
}, this._checkInterval);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
stopIntervalCheck() {
|
|
490
|
+
if (this._interval) {
|
|
491
|
+
clearInterval(this._interval);
|
|
492
|
+
}
|
|
493
|
+
this._interval = 0;
|
|
494
|
+
this._checkInterval = 0;
|
|
495
|
+
}
|
|
457
496
|
isPrimitive(value) {
|
|
458
497
|
const result = false;
|
|
459
498
|
if (value === null || value === void 0) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cacheable",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Simple Caching Engine using Keyv",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"private": false,
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@keyv/redis": "^3.0.1",
|
|
21
|
+
"@types/node": "^22.6.1",
|
|
21
22
|
"@vitest/coverage-v8": "^2.1.1",
|
|
22
23
|
"lru-cache": "^11.0.1",
|
|
23
24
|
"rimraf": "^6.0.1",
|