Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class LRU {
*
* @method entries
* @memberof LRU
* @param {string[]} [keys=this.keys()] - Array of keys to get entries for. Defaults to all keys.
* @param {string[]} [keys] - Array of keys to get entries for. If omitted, returns all keys.
* @returns {Array<Array<*>>} Array of [key, value] pairs in LRU order.
* @example
* cache.set('a', 1).set('b', 2);
Expand All @@ -118,11 +118,22 @@ export class LRU {
* @see {@link LRU#values}
* @since 11.1.0
*/
entries (keys = this.keys()) {
entries (keys) {
if (keys === undefined) {
keys = this.keys();
}

const entryMap = Object.create(null);
let x = this.first;

while (x !== null) {
entryMap[x.key] = x.value;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using entryMap[x.key] = x.value means entries()/values() no longer go through get(), so TTL expiration won’t be enforced (expired items won’t be deleted and may be returned as if still valid). If TTL is expected to apply to these accessors too, this changes observable behavior for caches with ttl > 0.

Severity: high

Other Locations
  • src/lru.js:458

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

x = x.next;
}

const result = new Array(keys.length);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
result[i] = [key, this.get(key)];
result[i] = [keys[i], entryMap[keys[i]]];
}

return result;
Expand Down Expand Up @@ -425,7 +436,7 @@ export class LRU {
*
* @method values
* @memberof LRU
* @param {string[]} [keys=this.keys()] - Array of keys to get values for. Defaults to all keys.
* @param {string[]} [keys] - Array of keys to get values for. If omitted, returns all values.
* @returns {Array<*>} Array of values corresponding to the keys in LRU order.
* @example
* cache.set('a', 1).set('b', 2);
Expand All @@ -435,10 +446,22 @@ export class LRU {
* @see {@link LRU#entries}
* @since 11.1.0
*/
values (keys = this.keys()) {
values (keys) {
if (keys === undefined) {
keys = this.keys();
}

const entryMap = Object.create(null);
let x = this.first;

while (x !== null) {
entryMap[x.key] = x.value;
x = x.next;
}

const result = new Array(keys.length);
for (let i = 0; i < keys.length; i++) {
result[i] = this.get(keys[i]);
result[i] = entryMap[keys[i]];
}

return result;
Expand Down
Loading