Skip to content

Commit a3de8cd

Browse files
committed
Add cache
1 parent b746aef commit a3de8cd

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

include/file-config.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ const fs = require('fs');
22
const path = require('path');
33
const config = require('./config');
44
const yaml = require('read-yaml');
5+
const Hashids = require('hashids');
6+
const hashids = new Hashids();
57

68
const globalConfigDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'] + '/.gtt';
79
const globalConfigFile = globalConfigDir + '/config.yml';
810
const frameDir = globalConfigDir + '/frames';
11+
const cacheDir = globalConfigDir + '/cache';
912
const localConfigFile = '/.gtt.yml';
1013

1114
/**
@@ -21,6 +24,10 @@ class fileConfig extends config {
2124
this.assertGlobalConfig();
2225
this.workDir = workDir;
2326
this.data = Object.assign(this.data, this.localExists() ? this.parseLocal() : this.parseGlobal());
27+
this.cache = {
28+
get: this._cacheGet,
29+
set: this._cacheSet
30+
};
2431
}
2532

2633
/**
@@ -66,13 +73,33 @@ class fileConfig extends config {
6673
assertGlobalConfig() {
6774
if (!fs.existsSync(this.globalDir)) fs.mkdirSync(this.globalDir, '0644', true);
6875
if (!fs.existsSync(this.frameDir)) fs.mkdirSync(this.frameDir, '0744', true);
76+
if (!fs.existsSync(this.cacheDir)) fs.mkdirSync(this.cacheDir, '0744', true);
6977
if (!fs.existsSync(this.global)) fs.appendFileSync(this.global, '');
7078
}
7179

7280
assertLocalConfig() {
7381
if (!this.localExists()) fs.appendFileSync(this.local, '');
7482
}
7583

84+
_cacheGet(key) {
85+
let file = this.cacheDir + '/' + hashids.encode(key);
86+
if (!fs.existsSync(file)) return false;
87+
88+
return JSON.parse(fs.readFileSync(file));
89+
}
90+
91+
_cacheSet(key, value) {
92+
let file = this.cacheDir + '/' + hashids.encode(key);
93+
if (fs.existsSync(file)) fs.unlinkSync(file);
94+
fs.appendFileSync(file, JSON.stringify(value));
95+
96+
return value;
97+
}
98+
99+
get cacheDir() {
100+
return cacheDir;
101+
}
102+
76103
get frameDir() {
77104
return frameDir;
78105
}

0 commit comments

Comments
 (0)