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