ModuleCache.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 log('cache miss for', cacheKey);
  24. // cache miss
  25. return undefined;
  26. }
  27. }
  28. ModuleCache.getSettings = function (settings) {
  29. const cacheSettings = Object.assign({
  30. lifetime: 30, // seconds
  31. }, settings['import/cache']);
  32. // parse infinity
  33. if (cacheSettings.lifetime === '∞' || cacheSettings.lifetime === 'Infinity') {
  34. cacheSettings.lifetime = Infinity;
  35. }
  36. return cacheSettings;
  37. };
  38. exports.default = ModuleCache;