Files
codeql-action/node_modules/eslint-module-utils/ModuleCache.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
1.1 KiB
JavaScript
Raw Normal View History

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