Last active
March 1, 2018 14:13
-
-
Save ryancoughlin/1ed02ea88dfc01a2a990f9f48a431057 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import LRU from 'lru-cache' | |
const options = { | |
max: 40000, | |
maxAge: 8000 * 60 * 60 | |
} | |
const cache = LRU(options) | |
export function checkCache(key) { | |
const cachedData = cache.get(key) | |
return new Promise(function(resolve, reject) { | |
if (cachedData !== undefined) { | |
return resolve(cachedData) | |
} else { | |
reject() | |
} | |
}) | |
} | |
export function setCache(key, data) { | |
cache.set(key, data) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_fetchRawPredictionTides(stationId) { | |
const yesterday = moment() | |
.add(-1, 'days') | |
.format(API_DATE_FORMAT) | |
const future = moment(yesterday, API_DATE_FORMAT) | |
.add(7, 'days') | |
.format(API_DATE_FORMAT) | |
const params = | |
'?begin_date=' + | |
yesterday + | |
'&end_date=' + | |
future + | |
'&station=' + | |
stationId + | |
'&interval=hilo&product=predictions&datum=mllw&units=english&time_zone=gmt&application=web_services&format=json' | |
return checkCache(stationId).catch(() => { | |
return request(`${process.env.NOAA_URL}`, params).then(json => { | |
setCache(stationId, json) | |
return json | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment