perf: cache config.json reads by mtime, reduce sync I/O
This commit is contained in:
@@ -6,12 +6,17 @@ const crypto = require('crypto');
|
||||
const CONFIG_PATH = path.join(__dirname, '..', 'data', 'config.json');
|
||||
|
||||
let pool = null;
|
||||
let _configCache = null;
|
||||
let _configMtime = 0;
|
||||
|
||||
function getConfig() {
|
||||
if (fs.existsSync(CONFIG_PATH)) {
|
||||
return JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8'));
|
||||
}
|
||||
return null;
|
||||
try {
|
||||
const stat = fs.statSync(CONFIG_PATH);
|
||||
if (_configCache && stat.mtimeMs === _configMtime) return _configCache;
|
||||
_configCache = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8'));
|
||||
_configMtime = stat.mtimeMs;
|
||||
return _configCache;
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
function saveConfig(cfg) {
|
||||
|
||||
Reference in New Issue
Block a user