-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.js
48 lines (39 loc) · 1.15 KB
/
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
import EventEmitter from "events";
import config from "./config.js";
import { Jsoning, MathOps } from 'jsoning';
import path from "path";
// thanks to https://www.npmjs.com/package/jsoning for the great storage library
// caches the counts of tasks so we know without reaching postgres
class TaskCache extends EventEmitter{
constructor(data_filepath){
super();
this.db = new Jsoning(data_filepath);
}
async get(key){
if(await this.db.has(key)){
return (await this.db.get(key));
}else{
return 0;
}
}
async sync(key, value){
// for when values are desynced
await this.db.set(key, value);
this.emit("change", key);
}
async inc(key){
await this.db.math(key, MathOps.Add, 1);
this.emit("change", key);
}
async dec(key){
await this.db.math(key, MathOps.Subtract, 1);
this.emit("change", key);
}
async toJSON(){
return (await this.db.all());
}
async keys(){
return Object.keys(await this.db.all());
}
}
export const cache = new TaskCache(path.join(config.dataPath, "task_cache.json"));