Add caching
This commit is contained in:
parent
7063092393
commit
296841756d
1 changed files with 21 additions and 15 deletions
|
@ -28,7 +28,9 @@ const shopProto = grpc.load(PROTO_PATH).hipstershop;
|
||||||
* Helper function that gets currency data from an XML webpage
|
* Helper function that gets currency data from an XML webpage
|
||||||
* Uses public data from European Central Bank
|
* Uses public data from European Central Bank
|
||||||
*/
|
*/
|
||||||
|
let _data;
|
||||||
function _getCurrencyData (callback) {
|
function _getCurrencyData (callback) {
|
||||||
|
if (!_data) {
|
||||||
request(DATA_URL, (err, res) => {
|
request(DATA_URL, (err, res) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw new Error(`Error getting data: ${err}`);
|
throw new Error(`Error getting data: ${err}`);
|
||||||
|
@ -45,9 +47,13 @@ function _getCurrencyData (callback) {
|
||||||
acc[x['currency']] = x['rate'];
|
acc[x['currency']] = x['rate'];
|
||||||
return acc;
|
return acc;
|
||||||
}, { 'EUR': '1.0' });
|
}, { 'EUR': '1.0' });
|
||||||
callback(results);
|
_data = results;
|
||||||
|
callback(_data);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
callback(_data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue