ModuleCache.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. exports.__esModule = true;
  3. const log = require('debug')('eslint-module-utils:ModuleCache');
  4. class ModuleCache {
  5. constructor(map) {
  6. this.map = map || new Map();
  7. }
  8. /**
  9. * returns value for returning inline
  10. * @param {[type]} cacheKey [description]
  11. * @param {[type]} result [description]
  12. */
  13. set(cacheKey, result) {
  14. this.map.set(cacheKey, { result, lastSeen: process.hrtime() });
  15. log('setting entry for', cacheKey);
  16. return result;
  17. }
  18. get(cacheKey, settings) {
  19. if (this.map.has(cacheKey)) {
  20. const f = this.map.get(cacheKey);
  21. // check freshness
  22. if (process.hrtime(f.lastSeen)[0] < settings.lifetime) { return f.result; }
  23. } else {
  24. log('cache miss for', cacheKey);
  25. }
  26. // cache miss
  27. return undefined;
  28. }
  29. }
  30. ModuleCache.getSettings = function (settings) {
  31. const cacheSettings = Object.assign({
  32. lifetime: 30, // seconds
  33. }, settings['import/cache']);
  34. // parse infinity
  35. if (cacheSettings.lifetime === '∞' || cacheSettings.lifetime === 'Infinity') {
  36. cacheSettings.lifetime = Infinity;
  37. }
  38. return cacheSettings;
  39. };
  40. exports.default = ModuleCache;