-
Notifications
You must be signed in to change notification settings - Fork 11
/
dac-cache.js
67 lines (51 loc) · 1.42 KB
/
dac-cache.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
class DacCache {
constructor(){
this.cache = new Map;
}
normalise_key(key){
const [name, data_str] = key.split('?');
if (!data_str){
return key;
}
const url_parts = data_str.split('&').sort();
return `${name}?${url_parts.join('&')}`;
}
get(dac_id, key){
key = this.normalise_key(key);
// console.log(`GET ${key}`);
if (this.cache.has(dac_id)){
const dac_cache = this.cache.get(dac_id);
if (dac_cache && dac_cache.has(key)){
return dac_cache.get(key);
}
}
return null;
}
set(dac_id, key, value=null){
// console.log(`SET ${key}`);
key = this.normalise_key(key);
let dac_cache = this.cache.get(dac_id);
if (!dac_cache){
dac_cache = new Map;
}
if (value === null){
dac_cache.delete(key);
}
else {
dac_cache.set(key, value);
}
this.cache.set(dac_id, dac_cache);
}
removePrefix(dac_id, prefix){
let dac_cache = this.cache.get(dac_id);
if (dac_cache){
Array.from(dac_cache.keys()).forEach((k) => {
if (k.indexOf(prefix) === 0){
dac_cache.delete(k);
}
});
this.cache.set(dac_id, dac_cache);
}
}
};
module.exports = DacCache;