-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacher.js
74 lines (65 loc) · 1.68 KB
/
cacher.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
export default class Cacher {
constructor (options) {
this.cacheMap = new Map()
this.maxCacheSize = options.maxCacheSize || 15
this.ttl = options.ttl || 60 * 60 * 1000
this.filters = options.filters || []
}
_needCache (option) {
return this.filters.some(item => {
return item === option.url
})
}
_getCache (option) {
let key = this._formatCacheKey(option)
return JSON.parse(this.cacheMap.get(key).data)
}
_setCache (option, val) {
let key = this._formatCacheKey(option)
if (this.maxCacheSize && this.cacheMap.size >= this.maxCacheSize) {
this.cacheMap.delete([...this.cacheMap.keys()][0])
}
let timeout = new Date().getTime() + this.ttl
return this.cacheMap.set(key, {
timeout: timeout,
data: JSON.stringify(val)
})
}
_hasCache (option) {
let key = this._formatCacheKey(option)
return this.cacheMap.has(key)
}
_formatCacheKey (option) {
let key = option.url
if (option.params) {
let keyMap = Object.keys(option.params)
keyMap.sort()
let obj = {}
keyMap.forEach(i => {
obj[i] = JSON.parse(JSON.stringify(option.params[i]))
})
let paramsStr = JSON.stringify(obj)
key += `?${paramsStr}`
}
return key
}
_cacheIfTimeout (option) {
let key = this._formatCacheKey(option)
let { timeout } = this.cacheMap.get(key)
return timeout < new Date().getTime()
}
_clear () {
this.cacheMap.clear()
}
_addFilter (key) {
if (!this.filters.includes(key)) {
this.filters.push(key)
}
}
_removeFilter (key) {
let index = this.filters.indexOf(key)
if (~index) {
this.filters.splice(index, 1)
}
}
}