@common.js/quick-lru 7.0.0 → 7.3.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 CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  The [quick-lru](https://www.npmjs.com/package/quick-lru) package exported as CommonJS modules.
4
4
 
5
- Exported from [quick-lru@7.0.0](https://www.npmjs.com/package/quick-lru/v/7.0.0) using https://github.com/etienne-martin/common.js.
5
+ Exported from [quick-lru@7.3.0](https://www.npmjs.com/package/quick-lru/v/7.3.0) using https://github.com/etienne-martin/common.js.
package/index.d.ts CHANGED
@@ -5,21 +5,25 @@ export type Options<KeyType, ValueType> = {
5
5
  @default Infinity
6
6
 
7
7
  By default, `maxAge` will be `Infinity`, which means that items will never expire.
8
- Lazy expiration upon the next write or read call.
8
+ Lazy expiration occurs upon the next write or read call.
9
9
 
10
- Individual expiration of an item can be specified by the `set(key, value, maxAge)` method.
10
+ Individual expiration of an item can be specified with the `set(key, value, {maxAge})` method.
11
11
  */
12
12
  readonly maxAge?: number;
13
13
 
14
14
  /**
15
- The maximum number of items before evicting the least recently used items.
15
+ The target maximum number of items before evicting the least recently used items.
16
+
17
+ __Note:__ This package uses an [algorithm](https://github.com/sindresorhus/quick-lru#algorithm) which maintains between `maxSize` and `2 × maxSize` items for performance reasons. The cache may temporarily contain up to twice the specified size due to the dual-cache design that avoids expensive delete operations.
16
18
  */
17
19
  readonly maxSize: number;
18
20
 
19
21
  /**
20
- Called right before an item is evicted from the cache.
22
+ Called right before an item is evicted from the cache due to LRU pressure, TTL expiration, or manual eviction via `evict()`.
21
23
 
22
24
  Useful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).
25
+
26
+ __Note:__ This callback is not called for manual removals via `delete()` or `clear()`. It fires for automatic evictions and manual evictions via `evict()`.
23
27
  */
24
28
  onEviction?: (key: KeyType, value: ValueType) => void;
25
29
  };
@@ -53,9 +57,9 @@ export default class QuickLRU<KeyType, ValueType> extends Map<KeyType, ValueType
53
57
  /**
54
58
  Set an item. Returns the instance.
55
59
 
56
- Individual expiration of an item can be specified with the `maxAge` option. If not specified, the global `maxAge` value will be used in case it is specified in the constructor, otherwise the item will never expire.
60
+ Individual expiration of an item can be specified with the `maxAge` option. If not specified, the global `maxAge` value will be used in case it is specified in the constructor; otherwise the item will never expire.
57
61
 
58
- @returns The list instance.
62
+ @returns The cache instance.
59
63
  */
60
64
  set(key: KeyType, value: ValueType, options?: {maxAge?: number}): this;
61
65
 
@@ -90,6 +94,18 @@ export default class QuickLRU<KeyType, ValueType> extends Map<KeyType, ValueType
90
94
  */
91
95
  clear(): void;
92
96
 
97
+ /**
98
+ Get the remaining time to live (in milliseconds) for the given item, or `undefined` when the item is not in the cache.
99
+
100
+ - Does not mark the item as recently used.
101
+ - Does not trigger lazy expiration or remove the entry when it is expired.
102
+ - Returns `Infinity` if the item has no expiration.
103
+ - May return a negative number if the item is already expired but not yet lazily removed.
104
+
105
+ @returns Remaining time to live in milliseconds when set, `Infinity` when there is no expiration, or `undefined` when the item does not exist.
106
+ */
107
+ expiresIn(key: KeyType): number | undefined;
108
+
93
109
  /**
94
110
  Update the `maxSize` in-place, discarding items as necessary. Insertion order is mostly preserved, though this is not a strong guarantee.
95
111
 
@@ -107,6 +123,11 @@ export default class QuickLRU<KeyType, ValueType> extends Map<KeyType, ValueType
107
123
  */
108
124
  get maxSize(): number;
109
125
 
126
+ /**
127
+ The set max age.
128
+ */
129
+ get maxAge(): number;
130
+
110
131
  /**
111
132
  Iterable for all the keys.
112
133
  */
@@ -126,4 +147,32 @@ export default class QuickLRU<KeyType, ValueType> extends Map<KeyType, ValueType
126
147
  Iterable for all entries, starting with the newest (descending in recency).
127
148
  */
128
149
  entriesDescending(): IterableIterator<[KeyType, ValueType]>;
150
+
151
+ /**
152
+ Evict the least recently used items from the cache.
153
+
154
+ @param count - The number of items to evict. Defaults to 1.
155
+
156
+ It will always keep at least one item in the cache.
157
+
158
+ @example
159
+ ```
160
+ import QuickLRU from 'quick-lru';
161
+
162
+ const lru = new QuickLRU({maxSize: 10});
163
+
164
+ lru.set('a', 1);
165
+ lru.set('b', 2);
166
+ lru.set('c', 3);
167
+
168
+ lru.evict(2); // Evicts 'a' and 'b'
169
+
170
+ console.log(lru.has('a'));
171
+ //=> false
172
+
173
+ console.log(lru.has('c'));
174
+ //=> true
175
+ ```
176
+ */
177
+ evict(count?: number): void;
129
178
  }
package/index.js CHANGED
@@ -378,7 +378,7 @@ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
378
378
  }
379
379
  };
380
380
  var _size = /*#__PURE__*/ new WeakMap(), _cache = /*#__PURE__*/ new WeakMap(), _oldCache = /*#__PURE__*/ new WeakMap(), _maxSize = /*#__PURE__*/ new WeakMap(), _maxAge = /*#__PURE__*/ new WeakMap(), _onEviction = /*#__PURE__*/ new WeakMap(), _emitEvictions = /*#__PURE__*/ new WeakSet(), _deleteIfExpired = /*#__PURE__*/ new WeakSet(), _getOrDeleteIfExpired = /*#__PURE__*/ new WeakSet(), _getItemValue = /*#__PURE__*/ new WeakSet(), _peek = /*#__PURE__*/ new WeakSet(), _set = /*#__PURE__*/ new WeakSet(), _moveToRecent = /*#__PURE__*/ new WeakSet(), _entriesAscending = /*#__PURE__*/ new WeakSet();
381
- var _iterator = Symbol.iterator, _toStringTag = Symbol.toStringTag;
381
+ var _iterator = Symbol.iterator, _toStringTag = Symbol.toStringTag, tmp = Symbol.for("nodejs.util.inspect.custom");
382
382
  var QuickLRU = /*#__PURE__*/ function(Map1) {
383
383
  "use strict";
384
384
  _inherits(QuickLRU, Map1);
@@ -497,6 +497,16 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
497
497
  }
498
498
  }
499
499
  },
500
+ {
501
+ key: "expiresIn",
502
+ value: function expiresIn(key) {
503
+ var ref;
504
+ var item = (ref = _classPrivateFieldGet(this, _cache).get(key)) !== null && ref !== void 0 ? ref : _classPrivateFieldGet(this, _oldCache).get(key);
505
+ if (item) {
506
+ return item.expiry ? item.expiry - Date.now() : Number.POSITIVE_INFINITY;
507
+ }
508
+ }
509
+ },
500
510
  {
501
511
  key: "delete",
502
512
  value: function _delete(key) {
@@ -538,6 +548,25 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
538
548
  _classPrivateFieldSet(this, _maxSize, newSize);
539
549
  }
540
550
  },
551
+ {
552
+ key: "evict",
553
+ value: function evict() {
554
+ var count = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 1;
555
+ var requested = Number(count);
556
+ if (!requested || requested <= 0) {
557
+ return;
558
+ }
559
+ var items = _toConsumableArray(_classPrivateMethodGet(this, _entriesAscending, entriesAscending).call(this));
560
+ var evictCount = Math.trunc(Math.min(requested, Math.max(items.length - 1, 0)));
561
+ if (evictCount <= 0) {
562
+ return;
563
+ }
564
+ _classPrivateMethodGet(this, _emitEvictions, emitEvictions).call(this, items.slice(0, evictCount));
565
+ _classPrivateFieldSet(this, _oldCache, new Map(items.slice(evictCount)));
566
+ _classPrivateFieldSet(this, _cache, new Map());
567
+ _classPrivateFieldSet(this, _size, 0);
568
+ }
569
+ },
541
570
  {
542
571
  key: "keys",
543
572
  value: function keys() {
@@ -1024,6 +1053,12 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
1024
1053
  return _classPrivateFieldGet(this, _maxSize);
1025
1054
  }
1026
1055
  },
1056
+ {
1057
+ key: "maxAge",
1058
+ get: function get() {
1059
+ return _classPrivateFieldGet(this, _maxAge);
1060
+ }
1061
+ },
1027
1062
  {
1028
1063
  key: "entries",
1029
1064
  value: function entries() {
@@ -1059,7 +1094,19 @@ var QuickLRU = /*#__PURE__*/ function(Map1) {
1059
1094
  {
1060
1095
  key: _toStringTag,
1061
1096
  get: function get() {
1062
- return JSON.stringify(_toConsumableArray(this.entriesAscending()));
1097
+ return "QuickLRU";
1098
+ }
1099
+ },
1100
+ {
1101
+ key: "toString",
1102
+ value: function toString() {
1103
+ return "QuickLRU(".concat(this.size, "/").concat(this.maxSize, ")");
1104
+ }
1105
+ },
1106
+ {
1107
+ key: tmp,
1108
+ value: function value() {
1109
+ return this.toString();
1063
1110
  }
1064
1111
  }
1065
1112
  ]);
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@common.js/quick-lru",
3
- "version": "7.0.0",
3
+ "version": "7.3.0",
4
4
  "description": "quick-lru package exported as CommonJS modules",
5
5
  "license": "MIT",
6
6
  "repository": "etienne-martin/common.js",
7
7
  "funding": "https://github.com/sponsors/sindresorhus",
8
8
  "type": "commonjs",
9
+ "sideEffects": false,
9
10
  "engines": {
10
11
  "node": ">=18"
11
12
  },
@@ -30,6 +31,6 @@
30
31
  },
31
32
  "homepage": "https://github.com/etienne-martin/common.js#readme",
32
33
  "dependencies": {},
33
- "types": "./index.d.ts",
34
- "main": "./index.js"
34
+ "main": "./index.js",
35
+ "types": "./index.d.ts"
35
36
  }