This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.js
72 lines (64 loc) · 1.6 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var cache = {};
/*
* Returns whether or not the object is in our cache, and saved
* within the last 12 hours.
*
* @param {Object} options
* @returns {int}
* - returns > 0 when (now - time saved) < 12 hours
* - returns 0 when (now - time saved) > 12 hours
* - returns < 0 when not cached
*
*/
module.exports.isCached = function(options) {
options.host = options.host || 'api-gw.it.umich.edu';
var searchQuery = options.host + options.path;
if(searchQuery in cache) {
var timeDiff = Date.now() - cache[searchQuery].timestamp;
// (now - time saved) < 12 hours
if(timeDiff < 43200000) {
return 1;
} else {
return 0;
}
} else {
return -1;
}
};
/*
* Get the object from our cache if it exists, else do not
* call the callback
*
* @param {Object} options
* @callback callback
*
*/
module.exports.getCachedResponse = function(options, callback) {
options.host = options.host || 'api-gw.it.umich.edu';
var searchQuery = options.host + options.path;
if(searchQuery in cache) {
callback({ result: cache[searchQuery].data });
}
};
/*
* Store a new object to our cache
*
* @param {Object} options
* @callback callback
*
*/
module.exports.storeCachedResponse = function(options, responseObj) {
options.host = options.host || 'api-gw.it.umich.edu';
var searchQuery = options.host + options.path;
cache[searchQuery] = {
timestamp: Date.now(),
data: responseObj
};
};
/*
* Empty the cache
*
*/
module.exports.emptyCache = function() {
cache = {};
};