1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/eslint-module-utils/ModuleCache.js

51 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-12-28 23:41:32 +08:00
'use strict';
2023-12-18 13:12:25 +08:00
2023-12-28 23:41:32 +08:00
exports.__esModule = true;
const log = require('debug')('eslint-module-utils:ModuleCache');
2023-12-18 13:12:25 +08:00
class ModuleCache {
constructor(map) {
2023-12-28 23:41:32 +08:00
this.map = map || new Map();
2023-12-18 13:12:25 +08:00
}
/**
* returns value for returning inline
* @param {[type]} cacheKey [description]
* @param {[type]} result [description]
*/
set(cacheKey, result) {
2023-12-28 23:41:32 +08:00
this.map.set(cacheKey, { result, lastSeen: process.hrtime() });
log('setting entry for', cacheKey);
return result;
2023-12-18 13:12:25 +08:00
}
get(cacheKey, settings) {
if (this.map.has(cacheKey)) {
2023-12-28 23:41:32 +08:00
const f = this.map.get(cacheKey);
// check freshness
if (process.hrtime(f.lastSeen)[0] < settings.lifetime) { return f.result; }
} else {
log('cache miss for', cacheKey);
}
2023-12-18 13:12:25 +08:00
// cache miss
2023-12-28 23:41:32 +08:00
return undefined;
2023-12-18 13:12:25 +08:00
}
}
ModuleCache.getSettings = function (settings) {
const cacheSettings = Object.assign({
lifetime: 30, // seconds
2023-12-28 23:41:32 +08:00
}, settings['import/cache']);
2023-12-18 13:12:25 +08:00
// parse infinity
if (cacheSettings.lifetime === '∞' || cacheSettings.lifetime === 'Infinity') {
2023-12-28 23:41:32 +08:00
cacheSettings.lifetime = Infinity;
2023-12-18 13:12:25 +08:00
}
2023-12-28 23:41:32 +08:00
return cacheSettings;
};
2023-12-18 13:12:25 +08:00
2023-12-28 23:41:32 +08:00
exports.default = ModuleCache;