blob: b910a5810aa5c03ca93cc40b9e9b0c3218234f3c [file] [log] [blame]
Tim van der Lippefdbd42e2020-04-07 15:14:36 +01001'use strict'
2exports.__esModule = true
3
4const log = require('debug')('eslint-module-utils:ModuleCache')
5
6class 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
34ModuleCache.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
47exports.default = ModuleCache