webhook/node_modules/eslint-module-utils/ModuleCache.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

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