Skip to content

Commit

Permalink
added caching to the gets from formio to hopefully prevent some slowness
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonSharratt committed Jan 23, 2020
1 parent aea039b commit 815d182
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
47 changes: 47 additions & 0 deletions microservices/requestApi/clients/formio_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,37 @@
const config = require('config');
const httpReq = require('request');
const logger = require('npmlog');
let atob = require('atob');
const NodeCache = require('node-cache');
//30 minute ttl
const formioCache = new NodeCache({stdTTL: 1800});

var formio = {};

function isTokenExpired(token){
let currDate = new Date();

let base64Url = token.split('.')[1];
let base64 = base64Url.replace('-', '+').replace('_', '/');
let jwtObj = JSON.parse(atob(base64));
let exp = new Date(0);
exp.setUTCSeconds(jwtObj.exp);

return (currDate > exp);
}

formio.auth = function(callback){
var token = formioCache.get("token");
if (token !== undefined){
//check if the token is expired
if (!isTokenExpired(token)){
callback(null, token);
return;
}

//if we're here it is expired
formioCache.del("token");
}
var data = {
data: {
email: config.get("formio.username"),
Expand All @@ -23,6 +50,7 @@ formio.auth = function(callback){
callback(err);
}else{
logger.verbose("formio auth success");
formioCache.set("token", res.headers['x-jwt-token'], 300);
callback(err, res.headers['x-jwt-token']);
}
});
Expand All @@ -49,6 +77,12 @@ formio.getSubmissions = function(formName, callback) {
}

formio.getSubmission = function(formName, submissionId, callback) {
let cacheKey = formName + "/" + submissionId;
if (formioCache.has(cacheKey)){
callback(null, formioCache.get(cacheKey));
return;
}

this.auth(function(err, jwt){
if (err){
logger.error("Error getting jwt", err);
Expand All @@ -62,6 +96,7 @@ formio.getSubmission = function(formName, submissionId, callback) {
callback(err);
}else{
logger.verbose("formio get submission success");
formioCache.set(cacheKey, body);
callback(null, body);
}
});
Expand Down Expand Up @@ -140,6 +175,11 @@ formio.putSubmission = function(formName, submissionId, values, callback) {
};

formio.getForms = function(callback) {
let cacheKey = "forms"
if (formioCache.has(cacheKey)){
callback(null, formioCache.get(cacheKey));
return;
}
this.auth(function(err, jwt){
if (err){
logger.error("Error getting jwt", err);
Expand All @@ -153,13 +193,19 @@ formio.getForms = function(callback) {
callback(err);
}else{
logger.verbose("formio get forms success");
formioCache.set(cacheKey, body);
callback(null, body);
}
});
});
};

formio.getForm = function(formName, callback) {
let cacheKey = formName;
if (formioCache.has(cacheKey)){
callback(null, formioCache.get(cacheKey));
return;
}
this.auth(function(err, jwt){
if (err){
logger.error("Error getting jwt", err);
Expand All @@ -173,6 +219,7 @@ formio.getForm = function(formName, callback) {
callback(err);
}else{
logger.verbose("formio get form success");
formioCache.set(cacheKey, body);
callback(null, body);
}
});
Expand Down
15 changes: 14 additions & 1 deletion microservices/requestApi/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions microservices/requestApi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
"mongoose": "^5.7.5",
"morgan": "^1.9.1",
"ms": "^2.1.2",
"node-cache": "^5.1.0",
"nodemailer": "^6.3.0",
"npmlog": "^4.1.2",
"passport": "^0.4.0",
"passport-headerapikey": "^1.1.0",
"passport-jwt": "^4.0.0",
"ws": "^7.1.1",
"request": "^2.88.0"
"request": "^2.88.0",
"ws": "^7.1.1"
},
"devDependencies": {
"chai": "^4.1.2",
Expand Down
8 changes: 8 additions & 0 deletions microservices/requestApi/routes/v2/db/model/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ model.getAll = function(query, limit, page, user, callback){
}
});
}else{
let v1keys = Object.keys(workingReq);
workingReq['data'] = {};
for (var j=0; j<v1keys.length; j++){
if (schemaFields.indexOf(v1keys[j]) === -1){
workingReq['data'] = workingReq[v1keys[j]];
delete workingReq[v1keys[j]];
}
}
v2Results.push(workingReq);
processed += 1;
if (processed === results.length){
Expand Down

0 comments on commit 815d182

Please sign in to comment.