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