-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.ts
58 lines (45 loc) · 1.15 KB
/
cache.ts
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
import * as _ from "lodash";
import * as temp from "temp";
import * as fs from "fs";
import * as path from "path";
export const stringHash = (str: string) => {
var hash = 5381,
i = str.length;
while (i) {
hash = (hash * 33) ^ str.charCodeAt(--i);
}
return hash >>> 0;
};
export const objectHash = (obj: any) => {
return stringHash(JSON.stringify(obj)).toString();
};
export class Cache {
private _hits = 0;
private _domain: string;
constructor(domain = "global") {
this._domain = domain;
}
get hits() {
return this._hits;
}
get dir() {
return path.join(temp.dir, "tablet-cache-" + this._domain);
}
async promise(f: Function, ...args) {
if (!fs.existsSync(this.dir)) {
fs.mkdirSync(this.dir);
}
const key = objectHash(args);
const keyPath = path.join(this.dir, key + ".json");
let result;
try {
result = JSON.parse(fs.readFileSync(keyPath).toString());
this._hits++;
} catch (error) {}
if (!result) {
result = await f.apply(this, args);
fs.writeFileSync(keyPath, JSON.stringify(result));
}
return new Promise(resolve => resolve(result));
}
}