@cacheable/memory 1.0.0 → 1.0.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 CHANGED
@@ -4,8 +4,8 @@
4
4
 
5
5
  [![codecov](https://codecov.io/gh/jaredwray/cacheable/graph/badge.svg?token=lWZ9OBQ7GM)](https://codecov.io/gh/jaredwray/cacheable)
6
6
  [![tests](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml/badge.svg)](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)
7
- [![npm](https://img.shields.io/npm/dm/cacheable.svg)](https://www.npmjs.com/package/cacheable)
8
- [![npm](https://img.shields.io/npm/v/cacheable)](https://www.npmjs.com/package/cacheable)
7
+ [![npm](https://img.shields.io/npm/dm/@cacheable/memory.svg)](https://www.npmjs.com/package/@cacheable/memory)
8
+ [![npm](https://img.shields.io/npm/v/@cacheable/memory.svg)](https://www.npmjs.com/package/@cacheable/memory)
9
9
  [![license](https://img.shields.io/github/license/jaredwray/cacheable)](https://github.com/jaredwray/cacheable/blob/main/LICENSE)
10
10
 
11
11
  You can use `CacheableMemory` as a standalone cache or as a primary store for `cacheable`. You can also set the `useClones` property to `false` if you want to use the same reference for the values. This is useful if you are using large objects and want to save memory. The `lruSize` property is the size of the LRU cache and is set to `0` by default which is unlimited. When setting the `lruSize` property it will limit the number of keys in the cache.
package/dist/index.cjs CHANGED
@@ -28,13 +28,12 @@ __export(index_exports, {
28
28
  maximumMapSize: () => maximumMapSize
29
29
  });
30
30
  module.exports = __toCommonJS(index_exports);
31
- var import_hookified = require("hookified");
32
31
  var import_memoize = require("@cacheable/memoize");
33
32
  var import_utils = require("@cacheable/utils");
33
+ var import_hookified = require("hookified");
34
34
 
35
35
  // src/memory-lru.ts
36
36
  var ListNode = class {
37
- // eslint-disable-next-line @typescript-eslint/parameter-properties
38
37
  value;
39
38
  prev = void 0;
40
39
  next = void 0;
@@ -114,7 +113,10 @@ var CacheableMemory = class extends import_hookified.Hookified {
114
113
  _storeHashSize = defaultStoreHashSize;
115
114
  _storeHashAlgorithm = import_utils.HashAlgorithm.DJB2;
116
115
  // Default is djb2Hash
117
- _store = Array.from({ length: this._storeHashSize }, () => /* @__PURE__ */ new Map());
116
+ _store = Array.from(
117
+ { length: this._storeHashSize },
118
+ () => /* @__PURE__ */ new Map()
119
+ );
118
120
  _ttl;
119
121
  // Turned off by default
120
122
  _useClone = true;
@@ -142,7 +144,12 @@ var CacheableMemory = class extends import_hookified.Hookified {
142
144
  }
143
145
  if (options?.lruSize) {
144
146
  if (options.lruSize > maximumMapSize) {
145
- this.emit("error", new Error(`LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`));
147
+ this.emit(
148
+ "error",
149
+ new Error(
150
+ `LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`
151
+ )
152
+ );
146
153
  } else {
147
154
  this._lruSize = options.lruSize;
148
155
  }
@@ -153,7 +160,10 @@ var CacheableMemory = class extends import_hookified.Hookified {
153
160
  if (options?.storeHashAlgorithm) {
154
161
  this._storeHashAlgorithm = options.storeHashAlgorithm;
155
162
  }
156
- this._store = Array.from({ length: this._storeHashSize }, () => /* @__PURE__ */ new Map());
163
+ this._store = Array.from(
164
+ { length: this._storeHashSize },
165
+ () => /* @__PURE__ */ new Map()
166
+ );
157
167
  this.startIntervalCheck();
158
168
  }
159
169
  /**
@@ -197,7 +207,12 @@ var CacheableMemory = class extends import_hookified.Hookified {
197
207
  */
198
208
  set lruSize(value) {
199
209
  if (value > maximumMapSize) {
200
- this.emit("error", new Error(`LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`));
210
+ this.emit(
211
+ "error",
212
+ new Error(
213
+ `LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`
214
+ )
215
+ );
201
216
  return;
202
217
  }
203
218
  this._lruSize = value;
@@ -248,7 +263,10 @@ var CacheableMemory = class extends import_hookified.Hookified {
248
263
  return;
249
264
  }
250
265
  this._storeHashSize = value;
251
- this._store = Array.from({ length: this._storeHashSize }, () => /* @__PURE__ */ new Map());
266
+ this._store = Array.from(
267
+ { length: this._storeHashSize },
268
+ () => /* @__PURE__ */ new Map()
269
+ );
252
270
  }
253
271
  /**
254
272
  * Gets the store hash algorithm
@@ -269,7 +287,7 @@ var CacheableMemory = class extends import_hookified.Hookified {
269
287
  * @returns {IterableIterator<string>} - The keys
270
288
  */
271
289
  get keys() {
272
- const keys = new Array();
290
+ const keys = [];
273
291
  for (const store of this._store) {
274
292
  for (const key of store.keys()) {
275
293
  const item = store.get(key);
@@ -287,7 +305,7 @@ var CacheableMemory = class extends import_hookified.Hookified {
287
305
  * @returns {IterableIterator<CacheableStoreItem>} - The items
288
306
  */
289
307
  get items() {
290
- const items = new Array();
308
+ const items = [];
291
309
  for (const store of this._store) {
292
310
  for (const item of store.values()) {
293
311
  if (this.hasExpired(item)) {
@@ -333,7 +351,7 @@ var CacheableMemory = class extends import_hookified.Hookified {
333
351
  * @returns {T[]} - The values of the keys
334
352
  */
335
353
  getMany(keys) {
336
- const result = new Array();
354
+ const result = [];
337
355
  for (const key of keys) {
338
356
  result.push(this.get(key));
339
357
  }
@@ -363,7 +381,7 @@ var CacheableMemory = class extends import_hookified.Hookified {
363
381
  * @returns {CacheableStoreItem[]} - The raw values of the keys
364
382
  */
365
383
  getManyRaw(keys) {
366
- const result = new Array();
384
+ const result = [];
367
385
  for (const key of keys) {
368
386
  result.push(this.getRaw(key));
369
387
  }
@@ -414,10 +432,7 @@ var CacheableMemory = class extends import_hookified.Hookified {
414
432
  }
415
433
  }
416
434
  const item = { key, value, expires };
417
- store.set(
418
- key,
419
- item
420
- );
435
+ store.set(key, item);
421
436
  }
422
437
  /**
423
438
  * Sets the values of the keys
@@ -444,7 +459,7 @@ var CacheableMemory = class extends import_hookified.Hookified {
444
459
  * @returns {boolean[]} - If true, the key exists. If false, the key does not exist.
445
460
  */
446
461
  hasMany(keys) {
447
- const result = new Array();
462
+ const result = [];
448
463
  for (const key of keys) {
449
464
  const item = this.get(key);
450
465
  result.push(Boolean(item));
@@ -470,7 +485,7 @@ var CacheableMemory = class extends import_hookified.Hookified {
470
485
  * @returns {T[]} - The values of the keys
471
486
  */
472
487
  takeMany(keys) {
473
- const result = new Array();
488
+ const result = [];
474
489
  for (const key of keys) {
475
490
  result.push(this.take(key));
476
491
  }
@@ -500,7 +515,10 @@ var CacheableMemory = class extends import_hookified.Hookified {
500
515
  * @returns {void}
501
516
  */
502
517
  clear() {
503
- this._store = Array.from({ length: this._storeHashSize }, () => /* @__PURE__ */ new Map());
518
+ this._store = Array.from(
519
+ { length: this._storeHashSize },
520
+ () => /* @__PURE__ */ new Map()
521
+ );
504
522
  this._lru = new DoublyLinkedList();
505
523
  }
506
524
  /**
@@ -535,6 +553,7 @@ var CacheableMemory = class extends import_hookified.Hookified {
535
553
  * @param {any} value - The value to clone
536
554
  * @returns {any} - The cloned value
537
555
  */
556
+ // biome-ignore lint/suspicious/noExplicitAny: type format
538
557
  clone(value) {
539
558
  if (this.isPrimitive(value)) {
540
559
  return value;
@@ -620,14 +639,17 @@ var CacheableMemory = class extends import_hookified.Hookified {
620
639
  * @param {Object} [options] - The options to wrap
621
640
  * @returns {Function} - The wrapped function
622
641
  */
642
+ // biome-ignore lint/suspicious/noExplicitAny: type format
623
643
  wrap(function_, options) {
624
644
  const wrapOptions = {
625
645
  ttl: options?.ttl ?? this._ttl,
626
646
  keyPrefix: options?.keyPrefix,
647
+ createKey: options?.createKey,
627
648
  cache: this
628
649
  };
629
650
  return (0, import_memoize.wrapSync)(function_, wrapOptions);
630
651
  }
652
+ // biome-ignore lint/suspicious/noExplicitAny: type format
631
653
  isPrimitive(value) {
632
654
  const result = false;
633
655
  if (value === null || value === void 0) {
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
- import { Hookified } from 'hookified';
2
1
  import { WrapFunctionOptions } from '@cacheable/memoize';
3
2
  import { HashAlgorithm, CacheableStoreItem, CacheableItem } from '@cacheable/utils';
4
3
  export { CacheableItem, CacheableStoreItem, HashAlgorithm, hash, hashToNumber } from '@cacheable/utils';
4
+ import { Hookified } from 'hookified';
5
5
 
6
6
  type StoreHashAlgorithmFunction = (key: string, storeHashSize: number) => number;
7
7
  /**
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Hookified } from 'hookified';
2
1
  import { WrapFunctionOptions } from '@cacheable/memoize';
3
2
  import { HashAlgorithm, CacheableStoreItem, CacheableItem } from '@cacheable/utils';
4
3
  export { CacheableItem, CacheableStoreItem, HashAlgorithm, hash, hashToNumber } from '@cacheable/utils';
4
+ import { Hookified } from 'hookified';
5
5
 
6
6
  type StoreHashAlgorithmFunction = (key: string, storeHashSize: number) => number;
7
7
  /**
package/dist/index.js CHANGED
@@ -1,15 +1,16 @@
1
1
  // src/index.ts
2
- import { Hookified } from "hookified";
3
- import { wrapSync } from "@cacheable/memoize";
4
2
  import {
5
- shorthandToTime,
3
+ wrapSync
4
+ } from "@cacheable/memoize";
5
+ import {
6
+ HashAlgorithm,
6
7
  hashToNumber,
7
- HashAlgorithm
8
+ shorthandToTime
8
9
  } from "@cacheable/utils";
10
+ import { Hookified } from "hookified";
9
11
 
10
12
  // src/memory-lru.ts
11
13
  var ListNode = class {
12
- // eslint-disable-next-line @typescript-eslint/parameter-properties
13
14
  value;
14
15
  prev = void 0;
15
16
  next = void 0;
@@ -93,7 +94,10 @@ var CacheableMemory = class extends Hookified {
93
94
  _storeHashSize = defaultStoreHashSize;
94
95
  _storeHashAlgorithm = HashAlgorithm.DJB2;
95
96
  // Default is djb2Hash
96
- _store = Array.from({ length: this._storeHashSize }, () => /* @__PURE__ */ new Map());
97
+ _store = Array.from(
98
+ { length: this._storeHashSize },
99
+ () => /* @__PURE__ */ new Map()
100
+ );
97
101
  _ttl;
98
102
  // Turned off by default
99
103
  _useClone = true;
@@ -121,7 +125,12 @@ var CacheableMemory = class extends Hookified {
121
125
  }
122
126
  if (options?.lruSize) {
123
127
  if (options.lruSize > maximumMapSize) {
124
- this.emit("error", new Error(`LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`));
128
+ this.emit(
129
+ "error",
130
+ new Error(
131
+ `LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`
132
+ )
133
+ );
125
134
  } else {
126
135
  this._lruSize = options.lruSize;
127
136
  }
@@ -132,7 +141,10 @@ var CacheableMemory = class extends Hookified {
132
141
  if (options?.storeHashAlgorithm) {
133
142
  this._storeHashAlgorithm = options.storeHashAlgorithm;
134
143
  }
135
- this._store = Array.from({ length: this._storeHashSize }, () => /* @__PURE__ */ new Map());
144
+ this._store = Array.from(
145
+ { length: this._storeHashSize },
146
+ () => /* @__PURE__ */ new Map()
147
+ );
136
148
  this.startIntervalCheck();
137
149
  }
138
150
  /**
@@ -176,7 +188,12 @@ var CacheableMemory = class extends Hookified {
176
188
  */
177
189
  set lruSize(value) {
178
190
  if (value > maximumMapSize) {
179
- this.emit("error", new Error(`LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`));
191
+ this.emit(
192
+ "error",
193
+ new Error(
194
+ `LRU size cannot be larger than ${maximumMapSize} due to Map limitations.`
195
+ )
196
+ );
180
197
  return;
181
198
  }
182
199
  this._lruSize = value;
@@ -227,7 +244,10 @@ var CacheableMemory = class extends Hookified {
227
244
  return;
228
245
  }
229
246
  this._storeHashSize = value;
230
- this._store = Array.from({ length: this._storeHashSize }, () => /* @__PURE__ */ new Map());
247
+ this._store = Array.from(
248
+ { length: this._storeHashSize },
249
+ () => /* @__PURE__ */ new Map()
250
+ );
231
251
  }
232
252
  /**
233
253
  * Gets the store hash algorithm
@@ -248,7 +268,7 @@ var CacheableMemory = class extends Hookified {
248
268
  * @returns {IterableIterator<string>} - The keys
249
269
  */
250
270
  get keys() {
251
- const keys = new Array();
271
+ const keys = [];
252
272
  for (const store of this._store) {
253
273
  for (const key of store.keys()) {
254
274
  const item = store.get(key);
@@ -266,7 +286,7 @@ var CacheableMemory = class extends Hookified {
266
286
  * @returns {IterableIterator<CacheableStoreItem>} - The items
267
287
  */
268
288
  get items() {
269
- const items = new Array();
289
+ const items = [];
270
290
  for (const store of this._store) {
271
291
  for (const item of store.values()) {
272
292
  if (this.hasExpired(item)) {
@@ -312,7 +332,7 @@ var CacheableMemory = class extends Hookified {
312
332
  * @returns {T[]} - The values of the keys
313
333
  */
314
334
  getMany(keys) {
315
- const result = new Array();
335
+ const result = [];
316
336
  for (const key of keys) {
317
337
  result.push(this.get(key));
318
338
  }
@@ -342,7 +362,7 @@ var CacheableMemory = class extends Hookified {
342
362
  * @returns {CacheableStoreItem[]} - The raw values of the keys
343
363
  */
344
364
  getManyRaw(keys) {
345
- const result = new Array();
365
+ const result = [];
346
366
  for (const key of keys) {
347
367
  result.push(this.getRaw(key));
348
368
  }
@@ -393,10 +413,7 @@ var CacheableMemory = class extends Hookified {
393
413
  }
394
414
  }
395
415
  const item = { key, value, expires };
396
- store.set(
397
- key,
398
- item
399
- );
416
+ store.set(key, item);
400
417
  }
401
418
  /**
402
419
  * Sets the values of the keys
@@ -423,7 +440,7 @@ var CacheableMemory = class extends Hookified {
423
440
  * @returns {boolean[]} - If true, the key exists. If false, the key does not exist.
424
441
  */
425
442
  hasMany(keys) {
426
- const result = new Array();
443
+ const result = [];
427
444
  for (const key of keys) {
428
445
  const item = this.get(key);
429
446
  result.push(Boolean(item));
@@ -449,7 +466,7 @@ var CacheableMemory = class extends Hookified {
449
466
  * @returns {T[]} - The values of the keys
450
467
  */
451
468
  takeMany(keys) {
452
- const result = new Array();
469
+ const result = [];
453
470
  for (const key of keys) {
454
471
  result.push(this.take(key));
455
472
  }
@@ -479,7 +496,10 @@ var CacheableMemory = class extends Hookified {
479
496
  * @returns {void}
480
497
  */
481
498
  clear() {
482
- this._store = Array.from({ length: this._storeHashSize }, () => /* @__PURE__ */ new Map());
499
+ this._store = Array.from(
500
+ { length: this._storeHashSize },
501
+ () => /* @__PURE__ */ new Map()
502
+ );
483
503
  this._lru = new DoublyLinkedList();
484
504
  }
485
505
  /**
@@ -514,6 +534,7 @@ var CacheableMemory = class extends Hookified {
514
534
  * @param {any} value - The value to clone
515
535
  * @returns {any} - The cloned value
516
536
  */
537
+ // biome-ignore lint/suspicious/noExplicitAny: type format
517
538
  clone(value) {
518
539
  if (this.isPrimitive(value)) {
519
540
  return value;
@@ -599,14 +620,17 @@ var CacheableMemory = class extends Hookified {
599
620
  * @param {Object} [options] - The options to wrap
600
621
  * @returns {Function} - The wrapped function
601
622
  */
623
+ // biome-ignore lint/suspicious/noExplicitAny: type format
602
624
  wrap(function_, options) {
603
625
  const wrapOptions = {
604
626
  ttl: options?.ttl ?? this._ttl,
605
627
  keyPrefix: options?.keyPrefix,
628
+ createKey: options?.createKey,
606
629
  cache: this
607
630
  };
608
631
  return wrapSync(function_, wrapOptions);
609
632
  }
633
+ // biome-ignore lint/suspicious/noExplicitAny: type format
610
634
  isPrimitive(value) {
611
635
  const result = false;
612
636
  if (value === null || value === void 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cacheable/memory",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "High Performance In-Memory Cache for Node.js",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -21,51 +21,37 @@
21
21
  "license": "MIT",
22
22
  "private": false,
23
23
  "devDependencies": {
24
- "@faker-js/faker": "^9.9.0",
25
- "@keyv/redis": "^5.0.0",
26
- "@keyv/valkey": "^1.0.7",
27
- "@types/eslint": "^9.6.1",
28
- "@types/node": "^24.1.0",
24
+ "@biomejs/biome": "^2.2.2",
25
+ "@faker-js/faker": "^10.0.0",
26
+ "@types/node": "^24.3.0",
29
27
  "@vitest/coverage-v8": "^3.2.4",
30
28
  "lru-cache": "^11.1.0",
31
29
  "rimraf": "^6.0.1",
32
30
  "tsup": "^8.5.0",
33
- "typescript": "^5.8.3",
34
- "vitest": "^3.2.4",
35
- "xo": "^1.2.1"
31
+ "typescript": "^5.9.2",
32
+ "vitest": "^3.2.4"
36
33
  },
37
34
  "dependencies": {
38
35
  "@keyv/bigmap": "^1.0.0",
39
- "hookified": "^1.10.0",
40
- "keyv": "^5.4.0",
41
- "@cacheable/memoize": "^1.1.0",
42
- "@cacheable/utils": "^1.1.0"
36
+ "hookified": "^1.12.0",
37
+ "keyv": "^5.5.0",
38
+ "@cacheable/memoize": "^1.1.1",
39
+ "@cacheable/utils": "^1.1.1"
43
40
  },
44
41
  "keywords": [
45
42
  "cacheable",
46
43
  "high performance",
47
- "layer 1 caching",
48
- "layer 2 caching",
49
44
  "distributed caching",
50
45
  "Keyv storage engine",
46
+ "keyv",
51
47
  "memory caching",
52
48
  "LRU cache",
53
- "expiration",
54
- "CacheableMemory",
55
- "offline support",
56
- "distributed sync",
57
- "secondary store",
58
- "primary store",
59
- "non-blocking operations",
60
- "cache statistics",
61
- "layered caching",
62
- "fault tolerant",
49
+ "memory",
50
+ "in-memory",
63
51
  "scalable cache",
64
52
  "in-memory cache",
65
- "distributed cache",
66
53
  "lruSize",
67
- "lru",
68
- "multi-tier cache"
54
+ "lru"
69
55
  ],
70
56
  "files": [
71
57
  "dist",
@@ -74,8 +60,9 @@
74
60
  "scripts": {
75
61
  "build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean",
76
62
  "prepublish": "pnpm build",
77
- "test": "xo --fix && vitest run --coverage",
78
- "test:ci": "xo && vitest run --coverage",
63
+ "lint": "biome check --write --error-on-warnings",
64
+ "test": "pnpm lint && vitest run --coverage",
65
+ "test:ci": "biome check --error-on-warnings && vitest run --coverage",
79
66
  "clean": "rimraf ./dist ./coverage ./node_modules"
80
67
  }
81
68
  }