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');
|
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user