Skip to content

Instantly share code, notes, and snippets.

@ryancoughlin
Last active March 1, 2018 14:13
Show Gist options
  • Save ryancoughlin/1ed02ea88dfc01a2a990f9f48a431057 to your computer and use it in GitHub Desktop.
Save ryancoughlin/1ed02ea88dfc01a2a990f9f48a431057 to your computer and use it in GitHub Desktop.
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)
}
_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