From 85f58dec6a5f62a9df9623a39c0eb9dab2a360b5 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 16 Feb 2019 15:27:46 -0500 Subject: [PATCH] Cached responses on the server to prevent overloading our api limit. --- routes/api.js | 52 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/routes/api.js b/routes/api.js index 2f5bb4b..8fc71b0 100644 --- a/routes/api.js +++ b/routes/api.js @@ -13,43 +13,39 @@ const authenticate = "?client_id=" + configLoader.getClientID() + "&client_secret=" + configLoader.getClientSecret(); +//caching program to make the application run faster +const cache = require('memory-cache'); + + function queryGitHubAPI(requestURL) { + const apiData = cache.get(requestURL); + return new Promise(function(reject, resolve) { - const queryRUL = GITHUB_API + requestURL + authenticate; - - got(queryRUL, { json: true }).then(response => + if(apiData == null) { - resolve(response.body); - }).catch(error => + + const queryRUL = GITHUB_API + requestURL + authenticate; + + got(queryRUL, { json: true }).then(response => + { + resolve(response.body); + cache.put(requestURL, response.body); + }).catch(error => + { + resolve(response.body); + cache.put(requestURL, response.body); + }); + + } + else { - resolve(response.body) - }); + resolve(apiData); + } }) } -//https://api.github.com/users/whatever?client_id=xxxx&client_secret=yyyy -// function authenticateWithGitHub() -// { -// const authURL = GITHUB_API + "/users/" + configLoader.getAPIUser() + "?client_id=" + configLoader.getClientID() + -// "&client_secret=" + configLoader.getClientSecret(); -// -// return new Promise(function(resolve, reject) -// { -// got(authURL, { json: true }).then(response => -// { -// console.log(response); -// resolve(response); -// }).catch(error => { -// reject(error); -// console.log(error.response.body); -// }); -// }) -// -// } - - routes.get('/*', (request, result) =>