Skip to content

Commit 6bc5d44

Browse files
authored
Merge pull request #493 from topcoder-platform/develop
feat: cache ChallengeTrack & ChallengeType to avoid repeated DynamoDB lookups
2 parents 4118334 + 6d138bc commit 6bc5d44

File tree

9 files changed

+2002
-1943
lines changed

9 files changed

+2002
-1943
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ workflows:
7171
only:
7272
- develop
7373
- fix/challenge-timelines-edit-routes
74+
- test/performance-profile
7475

7576
# Production builds are exectuted only on tagged commits to the
7677
# master branch.

config/default.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,6 @@ module.exports = {
101101
ZENDESK_API_TOKEN: process.env.ZENDESK_API_TOKEN || '',
102102
ZENDESK_API_URL: process.env.ZENDESK_API_URL || '',
103103
ZENDESK_CUSTOM_FIELD_TAG_ID: process.env.ZENDESK_CUSTOM_FIELD_TAG_ID,
104-
ZENDESK_DEFAULT_PRIORITY: process.env.ZENDESK_DEFAULT_PRIORITY || 'high'
104+
ZENDESK_DEFAULT_PRIORITY: process.env.ZENDESK_DEFAULT_PRIORITY || 'high',
105+
INTERNAL_CACHE_TTL: process.env.INTERNAL_CACHE_TTL || 1800
105106
}

docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Use the base image with Node.js
2-
FROM node:10.20-jessie
2+
FROM node:12.22.12-buster
33

44
# Copy the current directory into the Docker image
55
COPY . /challenge-api

package-lock.json

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

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838
"standard": "^12.0.1"
3939
},
4040
"dependencies": {
41-
"aws-sdk": "^2.466.0",
41+
"aws-sdk": "^2.1145.0",
4242
"axios": "^0.19.0",
4343
"bluebird": "^3.5.1",
4444
"body-parser": "^1.15.1",
4545
"config": "^3.0.1",
4646
"cors": "^2.7.1",
4747
"dotenv": "^8.2.0",
48-
"dynamoose": "^1.8.0",
48+
"dynamoose": "^1.11.1",
4949
"elasticsearch": "^16.1.1",
5050
"express": "^4.15.4",
5151
"express-fileupload": "^1.1.6",
@@ -57,6 +57,7 @@
5757
"jsonwebtoken": "^8.3.0",
5858
"lodash": "^4.17.19",
5959
"moment": "^2.24.0",
60+
"node-cache": "^5.1.2",
6061
"swagger-ui-express": "^4.1.3",
6162
"tc-core-library-js": "appirio-tech/tc-core-library-js.git#v2.6.4",
6263
"topcoder-bus-api-wrapper": "topcoder-platform/tc-bus-api-wrapper.git",
@@ -77,6 +78,6 @@
7778
"node": "10.x"
7879
},
7980
"volta": {
80-
"node": "10.22.1"
81+
"node": "12.22.12"
8182
}
8283
}

src/common/helper.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_
1515
const axios = require('axios')
1616
const busApi = require('topcoder-bus-api-wrapper')
1717
const elasticsearch = require('elasticsearch')
18-
const moment = require('moment')
18+
const NodeCache = require('node-cache')
1919
const HttpStatus = require('http-status-codes')
2020
const xss = require('xss')
2121
const logger = require('./logger')
@@ -48,6 +48,9 @@ function wrapExpress (fn) {
4848
}
4949
}
5050

51+
// Internal cache
52+
const internalCache = new NodeCache({ stdTTL: config.INTERNAL_CACHE_TTL })
53+
5154
/**
5255
* Wrap all functions from object
5356
* @param obj the object (controller exports)
@@ -447,7 +450,7 @@ async function createResource (challengeId, memberHandle, roleId) {
447450
* @param {String} description The description
448451
* @param {String} type The type
449452
* @param {String} token The token
450-
* @returns
453+
* @returns
451454
*/
452455
async function createSelfServiceProject (name, description, type, token) {
453456
const projectObj = {
@@ -456,15 +459,15 @@ async function createSelfServiceProject (name, description, type, token) {
456459
type
457460
}
458461
const url = `${config.PROJECTS_API_URL}`
459-
const res = await axios.post(url, projectObj, {headers: {Authorization: `Bearer ${token}`}})
462+
const res = await axios.post(url, projectObj, { headers: { Authorization: `Bearer ${token}` } })
460463
return _.get(res, 'data.id')
461464
}
462465

463466
/**
464467
* Get project payment
465468
* @param {String} projectId the project id
466469
*/
467-
async function getProjectPayment (projectId) {
470+
async function getProjectPayment (projectId) {
468471
const token = await getM2MToken()
469472
const url = `${config.CUSTOMER_PAYMENTS_URL}`
470473
const res = await axios.get(url, {
@@ -473,7 +476,7 @@ async function createSelfServiceProject (name, description, type, token) {
473476
referenceId: projectId,
474477
reference: 'project'
475478
}
476-
})
479+
})
477480
const [payment] = res.data
478481
return payment
479482
}
@@ -514,7 +517,7 @@ async function cancelPayment (paymentId) {
514517
* @param {String} cancelReason the cancel reasonn
515518
* @param {Object} currentUser the current user
516519
*/
517-
async function cancelProject (projectId, cancelReason, currentUser) {
520+
async function cancelProject (projectId, cancelReason, currentUser) {
518521
let payment = await getProjectPayment(projectId)
519522
const project = await ensureProjectExist(projectId, currentUser)
520523
if (project.status === 'cancelled') return // already canceled
@@ -998,7 +1001,7 @@ async function validateChallengeTerms (terms = []) {
9981001
async function _filterChallengesByGroupsAccess (currentUser, challenges) {
9991002
const res = []
10001003
const needToCheckForGroupAccess = !currentUser ? true : !currentUser.isMachine && !hasAdminRole(currentUser)
1001-
if(!needToCheckForGroupAccess) return challenges
1004+
if (!needToCheckForGroupAccess) return challenges
10021005

10031006
let userGroups
10041007

@@ -1234,6 +1237,14 @@ async function submitZendeskRequest (request) {
12341237
}
12351238
}
12361239

1240+
function getFromInternalCache (key) {
1241+
return internalCache.get(key)
1242+
}
1243+
1244+
function setToInternalCache (key, value) {
1245+
internalCache.set(key, value)
1246+
}
1247+
12371248
module.exports = {
12381249
wrapExpress,
12391250
autoWrapExpress,
@@ -1287,5 +1298,7 @@ module.exports = {
12871298
sendSelfServiceNotification,
12881299
getMemberByHandle,
12891300
submitZendeskRequest,
1290-
updateSelfServiceProjectInfo
1301+
updateSelfServiceProjectInfo,
1302+
getFromInternalCache,
1303+
setToInternalCache
12911304
}

0 commit comments

Comments
 (0)