-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.ts
43 lines (41 loc) · 1.19 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
import fs from "fs";
import path from "path";
/**
* 缓存机制
*/
export default class Cache {
static $map: Record<string, string> = {};
static $file = "";
static $modified = false;
static debug = false;
static initialize() {
Cache.$file = path.join(__dirname, ".cache");
if (fs.existsSync(Cache.$file)) {
try {
Cache.$map = JSON.parse(fs.readFileSync(Cache.$file, { encoding: "utf-8" }));
} catch (e) {
Cache.$map = {};
}
}
}
static compare(key: string, value: string) {
return Cache.read(key) === value;
}
static read(key: string) {
return Cache.$map[key];
}
static get modified() {
return Cache.$modified;
}
static write(key: string, value: string) {
Cache.$map[key] = value;
Cache.$modified = true;
Cache.debug && console.log("文件加入缓存", key, value);
}
static save() {
if (!Cache.$modified) return;
fs.writeFileSync(Cache.$file, JSON.stringify(Cache.$map, null, 2), { encoding: "utf-8" });
Cache.$modified = false;
Cache.debug && console.log("缓存已更新");
}
}