UNPKG

576 BJavaScriptView Raw
1"use strict"
2
3const LRU = require("lru-cache")
4
5const LRU_SIZE = 5000
6
7class CacheStore {
8 constructor(fetchFn) {
9 this.fetchFn = fetchFn
10 this.store = new LRU(LRU_SIZE)
11 }
12
13 async fetch(key) {
14 if (!key) {
15 return null
16 }
17
18 if (this.store.has(key)) {
19 return this.store.get(key)
20 }
21
22 let res = null
23 try {
24 res = await this.fetchFn(key)
25 this.store.set(key, res)
26 } catch (e) {} // eslint-disable-line no-empty
27
28 return res
29 }
30
31 set(key, value) {
32 return this.store.set(key, value)
33 }
34}
35
36module.exports = CacheStore