perf: cache config.json reads by mtime, reduce sync I/O

This commit is contained in:
2026-07-13 04:18:52 +08:00
parent 4f4df7e479
commit 2a18b4b1a8

View File

@@ -6,12 +6,17 @@ const crypto = require('crypto');
const CONFIG_PATH = path.join(__dirname, '..', 'data', 'config.json'); const CONFIG_PATH = path.join(__dirname, '..', 'data', 'config.json');
let pool = null; let pool = null;
let _configCache = null;
let _configMtime = 0;
function getConfig() { function getConfig() {
if (fs.existsSync(CONFIG_PATH)) { try {
return JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8')); const stat = fs.statSync(CONFIG_PATH);
} if (_configCache && stat.mtimeMs === _configMtime) return _configCache;
return null; _configCache = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8'));
_configMtime = stat.mtimeMs;
return _configCache;
} catch { return null; }
} }
function saveConfig(cfg) { function saveConfig(cfg) {