UNPKG

978 BJavaScriptView Raw
1"use strict"
2exports.__esModule = true
3
4class ModuleCache {
5 constructor(map) {
6 this.map = map || new Map()
7 }
8
9 /**
10 * returns value for returning inline
11 * @param {[type]} cacheKey [description]
12 * @param {[type]} result [description]
13 */
14 set(cacheKey, result) {
15 this.map.set(cacheKey, { result, lastSeen: Date.now() })
16 return result
17 }
18
19 get(cacheKey, settings) {
20 if (this.map.has(cacheKey)) {
21 const f = this.map.get(cacheKey)
22 // check fresness
23 if (Date.now() - f.lastSeen < (settings.lifetime * 1000)) return f.result
24 }
25 // cache miss
26 return undefined
27 }
28
29}
30
31ModuleCache.getSettings = function (settings) {
32 const cacheSettings = Object.assign({
33 lifetime: 30, // seconds
34 }, settings['import/cache'])
35
36 // parse infinity
37 if (cacheSettings.lifetime === '∞' || cacheSettings.lifetime === 'Infinity') {
38 cacheSettings.lifetime = Infinity
39 }
40
41 return cacheSettings
42}
43
44exports.default = ModuleCache