Skip to content

Commit ace2150

Browse files
committed
added 15 min caching
1 parent 4904f69 commit ace2150

File tree

6 files changed

+48
-13
lines changed

6 files changed

+48
-13
lines changed

lib/api.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const NodeCache = require( "node-cache" );
2+
const axios = require('axios');
3+
const myCache = new NodeCache( { stdTTL: 100, checkperiod: 600 } );
4+
const CORONA_ALL_KEY = 'coronaAll';
5+
6+
exports.getCoronaData = async () => {
7+
const coronaCache = myCache.get(CORONA_ALL_KEY);
8+
if (coronaCache) {
9+
return coronaCache;
10+
}
11+
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
12+
if (!result || !result.data) {
13+
throw new Error(`Source API faliure.`);
14+
}
15+
myCache.set(CORONA_ALL_KEY, result.data, 60 * 15);
16+
return result.data;
17+
}

lib/byCountry.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const axios = require('axios');
33
const _ = require('lodash');
44
const chalk = require('chalk');
55
const helpers = require('./helpers');
6+
const api = require('./api');
67
const {
78
getCountry,
89
getConfirmed,
@@ -62,13 +63,13 @@ function extraStats(dataArr) {
6263
}
6364

6465
exports.getJSONData = async () => {
65-
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
66-
return result.data;
66+
const data = await api.getCoronaData();
67+
return data;
6768
}
6869

6970
exports.getJSONDataForCountry = async (countryCode) => {
70-
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
71-
const { latest, confirmed, deaths, recovered } = result.data;
71+
const data = await api.getCoronaData();
72+
const { latest, confirmed, deaths, recovered } = data;
7273
const countryData = getDataByState(confirmed, deaths, recovered, countryCode)
7374
.filter(obj => obj.countryCode === countryCode);
7475
return countryData;
@@ -96,9 +97,9 @@ exports.getCountryTable = async (countryCode) => {
9697
, 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
9798
, 'right': '║' , 'right-mid': '╢' , 'middle': '│' }
9899
});
99-
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
100-
const { latest, confirmed, deaths, recovered } = result.data;
101-
const countryData = getDataByState(confirmed, deaths, recovered, countryCode)
100+
const data = await api.getCoronaData();
101+
const { latest, confirmed, deaths, recovered } = data;
102+
const countryData = getDataByState(confirmed, deaths, recovered)
102103
.filter(obj => obj.countryCode === countryCode);
103104
const totalStats = getTotalStats(countryData);
104105
table.push({

lib/corona.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const axios = require('axios');
33
const _ = require('lodash');
44
const chalk = require('chalk');
55
const helpers = require('./helpers');
6+
const api = require('./api');
67
const {
78
getCountry,
89
getConfirmed,
@@ -22,8 +23,6 @@ const {
2223
footer,
2324
} = require('./helpers');
2425

25-
26-
2726
function getDataByCountry(confirmed, deaths, recovered) {
2827
const countryMap = {};
2928
const confirmedMap = _.keyBy(confirmed.locations, (i) => i.country + i.province);
@@ -100,8 +99,8 @@ exports.getCompleteTable = async () => {
10099
, 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
101100
, 'right': '║' , 'right-mid': '╢' , 'middle': '│' }
102101
});
103-
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
104-
const { latest, confirmed, deaths, recovered } = result.data;
102+
const data = await api.getCoronaData();
103+
const { latest, confirmed, deaths, recovered } = data;
105104
const countryData = getDataByCountry(confirmed, deaths, recovered)
106105
const worldStats = getTotalStats(countryData);
107106
table.push({

lib/helpers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ e.getTotalStats = (countryData) => {
147147

148148
e.footer = `
149149
150-
Source Code: https://github.com/sagarkarira/coronavirus-tracker-cli/
150+
Stay safe everyone.
151+
152+
Code: https://github.com/sagarkarira/coronavirus-tracker-cli/
151153
Twitter: http://twitter.com/ekrysis
152154
`;

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"express": "^4.17.1",
2727
"humanize-number": "0.0.2",
2828
"lodash": "^4.17.15",
29-
"moment": "^2.24.0"
29+
"moment": "^2.24.0",
30+
"node-cache": "^5.1.0"
3031
},
3132
"devDependencies": {
3233
"nodemon": "^2.0.2"

0 commit comments

Comments
 (0)