-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcaching.js
62 lines (38 loc) · 1.38 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// caching.js
// Where we load json files from local storage to ease load on Airtable rate limit
const fs = require('fs');
// ^ fs allows us to read or write to a file for caching
var cacheInterval = 60 * 5; //5 minutes
// ^ If the cache is less than this many minutes old, serve it
module.exports = {
setCacheInterval: function(interval) {
cacheInterval = interval;
},
writeCacheWithPath: function(path, object) {
var pathComponents = path.split("/");
var intermediatePath = "";
for (var i = 0; i < pathComponents.length - 1; i++) {
var pathComponent = pathComponents[i];
pathComponent = pathComponent + "/";
intermediatePath = intermediatePath + pathComponent;
if (fs.existsSync(intermediatePath) != true) {
fs.mkdirSync(intermediatePath);
}
}
fs.writeFile(path, JSON.stringify(object), function(err) {
if (err) throw err;
else console.log("Cache write succeeded: " + path);
});
},
readCacheWithPath: function(path) {
var shouldSendCache = false;
if (fs.existsSync(path)) {
var cachedTime = fs.statSync(path).ctime;
if ((new Date().getTime() / 1000.0 - cachedTime / 1000.0) < cacheInterval) {
shouldSendCache = true;
}
}
if (!shouldSendCache) return null;
else return JSON.parse(fs.readFileSync(path, 'utf8'));
}
}