-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.js
34 lines (29 loc) · 889 Bytes
/
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
import fs from 'fs';
import {parse} from 'csv';
import {promisify} from 'bluebird';
let parseAsync = promisify(parse);
export class Cache {
constructor(file) {
this.file = file;
this.cache = new Map();
}
static fromCsvFile(file) {
let cacheInstance = new Cache(file);
fs.readFileAsync('cache.csv', {flag: 'a+'})
.then(parseAsync)
.then(parsed => {
debugger;
cacheInstance.cache = parsed.reduce((map, row) => map.set(row[0], row[1]), new Map());
return Promise.resolve();
})
.then(() => console.log('Cache is ready!'));
return cacheInstance;
}
get(hash) {
return this.cache.get(hash);
}
set(hash, link) {
this.cache.set(hash, link);
return fs.appendFileAsync(this.file, hash+","+link+"\n")
}
}