Skip to content

pathUtils | implement custom LRU caching #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
45 changes: 45 additions & 0 deletions lib/LRUCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class LRUCache {
/**
* @param {number} maxSize maxSize
*/
constructor(maxSize) {
this._maxSize = maxSize;
/** @type {string[]} */
this._doublyQueue = [];
this._cacheMap = new Map();
}

/**
* @param {string} item item
*/
get(item) {
if (this._cacheMap.has(item)) {
const itemData = this._doublyQueue.splice(
this._doublyQueue.indexOf(item),
1
);
this._doublyQueue.unshift(item);

if (itemData.length > 0) {
this._cacheMap.set(item, itemData[0]);
}
}
}

/**
* @param {any} item item
* @param {any} itemData itemData
*/
set(item, itemData) {
if (this._doublyQueue.length === this._maxSize) {
const last = this._doublyQueue[this._doublyQueue.length - 1];
this._doublyQueue.pop();
this._cacheMap.delete(last);
}

this._doublyQueue.unshift(item);
this._cacheMap.set(item, itemData);
}
}

module.exports = LRUCache;
5 changes: 3 additions & 2 deletions lib/util/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"use strict";

const path = require("path");
const { LRUCache } = require("lru-cache");

const CHAR_HASH = "#".charCodeAt(0);
const CHAR_SLASH = "/".charCodeAt(0);
Expand Down Expand Up @@ -170,8 +171,8 @@ const join = (rootPath, request) => {
};
exports.join = join;

/** @type {Map<string, Map<string, string | undefined>>} */
const joinCache = new Map();
/** @type {LRUCache<string, Map<string, string | undefined>>} */
const joinCache = new LRUCache({ max: 500 });

/**
* @param {string} rootPath the root path
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "enhanced-resolve",
"name": "@galcarmi/enhanced-resolve",
"version": "5.16.0",
"author": "Tobias Koppers @sokra",
"description": "Offers a async require.resolve function. It's highly configurable.",
Expand All @@ -14,6 +14,7 @@
},
"dependencies": {
"graceful-fs": "^4.2.4",
"lru-cache": "^10.2.2",
"tapable": "^2.2.0"
},
"license": "MIT",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3063,6 +3063,11 @@ log-update@^4.0.0:
slice-ansi "^4.0.0"
wrap-ansi "^6.2.0"

lru-cache@^10.2.2:
version "10.2.2"
resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-10.2.2.tgz#48206bc114c1252940c41b25b41af5b545aca878"
integrity sha512-9hp3Vp2/hFQUiIwKo8XCeFVnrg8Pk3TYNPIR7tJADKi5YfcF7vEaK7avFHTlSy3kOKYaJQaalfEo6YuXdceBOQ==

lru-cache@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"
Expand Down