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