-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a92fa5
commit 1aa49d7
Showing
6 changed files
with
181 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,55 @@ | ||
const cacheHolder = { cache: new Map() }; | ||
|
||
module.exports = { | ||
get(key) { | ||
const ref = cacheHolder.cache.get(key); | ||
if (ref) { | ||
return ref.deref(); | ||
const { readFile } = require('fs/promises'); | ||
class BuildCache { | ||
/** | ||
* @param {(...args: any[]) => void} log | ||
*/ | ||
constructor(log) { | ||
this.log = log || ((...args) => console.log(...args)); | ||
/** | ||
* @type {Map<string, {result: import('esbuild').OnLoadResult; input: string}} | ||
*/ | ||
this.cache = new Map(); | ||
} | ||
/** | ||
* @description key should be absolute path | ||
* @param {string} key | ||
* @returns {Promise<import('esbuild').OnLoadResult|void>} | ||
*/ | ||
async get(key) { | ||
const cachedData = this.cache.get(key); | ||
if (cachedData) { | ||
this.log(`find cache data, check if input changed(${key})...`); | ||
const input = await readFile(key, { encoding: 'utf8' }); | ||
if (input === cachedData.input) { | ||
this.log(`input not changed, return cache(${key})`); | ||
return cachedData.result; | ||
} | ||
this.log(`input changed(${key})`); | ||
return void 0; | ||
} | ||
this.log(`cache data not found(${key})`); | ||
return void 0; | ||
} | ||
/** | ||
* @description key should be absolute path | ||
* @param {string} key | ||
* @param {import('esbuild').OnLoadResult} result | ||
* @param {string} originContent | ||
* @returns {Promise<void>} | ||
*/ | ||
async set(key, result, originContent) { | ||
const m = process.memoryUsage.rss(); | ||
if (m / 1024 / 1024 > 250) { | ||
this.log('memory usage > 250M'); | ||
this.clear(); | ||
} | ||
}, | ||
set(key, data) { | ||
const wr = new WeakRef(data); | ||
cacheHolder.cache.set(key, wr); | ||
}, | ||
const input = originContent || (await readFile(key, { encoding: 'utf8' })); | ||
this.cache.set(key, { input, result }); | ||
} | ||
clear() { | ||
cacheHolder.cache.clear(); | ||
this.log('clear cache'); | ||
this.cache.clear(); | ||
} | ||
}; | ||
} | ||
|
||
module.exports = BuildCache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.