-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoCache.js
59 lines (51 loc) · 1.58 KB
/
AutoCache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { existsSync, readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
// Constants
const AUTOCACHE_PATH = join(__dirname, 'alphavantage.cache')
// Cache to circumvent AlphaVantage API restrictions
export default class AutoCache {
static tryInit () {
if (
Object.keys(AutoCache.cache).length === 0 &&
existsSync(AUTOCACHE_PATH)
) {
AutoCache.cache = JSON.parse(readFileSync(AUTOCACHE_PATH))
}
}
static verifyArgs (args1, args2) {
let valid = args1.length === args2.length
if (!valid) console.log('[Cache] Parameter count mismatch')
for (const i of args1.keys()) {
if (!valid) break
valid &= args1[i] === args2[i]
}
if (!valid) console.log('[Cache] Detected invalid args')
return valid
}
static retrieve (key, args) {
console.log(`[Cache::FetchDry] ${key}`)
AutoCache.tryInit()
return key in AutoCache.cache &&
AutoCache.verifyArgs(args, AutoCache.cache[key].args)
? AutoCache.cache[key].data
: null
}
static store (key, args, data) {
console.log(`[Cache::Store] ${key}`)
AutoCache.cache[key] = { args, data }
writeFileSync(AUTOCACHE_PATH, JSON.stringify(AutoCache.cache))
return data
}
static async _call (key, fn, ...args) {
console.log(`[Cache::FetchWet] ${key}`)
return fn(...args)
}
static async call (key, fn, ...args) {
const newKey = `${key}<${JSON.stringify(args)}>`
return (
AutoCache.retrieve(newKey, args) ||
AutoCache.store(newKey, args, await AutoCache._call(newKey, fn, ...args))
)
}
}
AutoCache.cache = {}