-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.js
More file actions
53 lines (48 loc) · 1.23 KB
/
cache.js
File metadata and controls
53 lines (48 loc) · 1.23 KB
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
var CACHE_TTL = (process.env.CACHE_TTL && parseInt(process.env.CACHE_TTL, 10)) || 60 * 1000;
var cache;
/**
* Get time from internal cache
*
* @returns {undefined|array} times
*/
function getCachedTime() {
var result;
if (cache && cache.time) {
var cachedTime = cache.time;
if (new Date() - cache.time < CACHE_TTL) {
var since = Math.round((new Date() - cache.time) / CACHE_TTL);
result = cache.data.map(function(time) { return time - since; });
result = result.filter(function (time) { return time > 0; })
}
}
return result;
}
/**
* Get time regardless of if it is still valid
*
* @returns {undefined|array} times
*/
function getStaleTime() {
var result;
if (cache && cache.time) {
var since = Math.round((new Date() - cache.time) / 60000);
result = cache.data.map(function(time) { return time - since; });
result = result.filter(function (time) { return time > 0; })
}
return result;
}
/**
* Update internal cache with BART times
*
* @param {number[]} times
*/
function setCachedTime(times) {
cache = cache || {};
cache.time = new Date();
cache.data = times;
}
module.exports = {
getCachedTime: getCachedTime,
getStaleTime: getStaleTime,
setCachedTime: setCachedTime
};