-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaching.js
34 lines (27 loc) · 924 Bytes
/
caching.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
'use strict';
const fs = require('fs');
module.exports = {
read: function (cacheName, cache) {
if (!sanityCheck(cacheName)) return;
if(!fs.existsSync(__dirname+process.env.DATASTORE+cacheName)) {
console.error(`File not found for ${cacheName}`);
return
}
var data = fs.readFileSync(__dirname+process.env.DATASTORE+cacheName);
if (data) {
cache.load(JSON.parse(data));
cache.prune();
}
},
write: function (cacheName, cache) {
if (!sanityCheck(cacheName)) return;
let cachedump = cache.dump();
fs.writeFile(__dirname+process.env.DATASTORE+cacheName, JSON.stringify(cachedump), (err) => {
if (err) { return console.error(err); }
cache.prune();
});
}
}
function sanityCheck(cacheName) {
return /^[a-z0-9]+$/i.test(cacheName);
}