Skip to content

feat: cache ChallengeTrack & ChallengeType to avoid repeated DynamoDB lookups #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ workflows:
only:
- develop
- fix/challenge-timelines-edit-routes
- test/performance-profile

# Production builds are exectuted only on tagged commits to the
# master branch.
Expand Down
3 changes: 2 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,6 @@ module.exports = {
ZENDESK_API_TOKEN: process.env.ZENDESK_API_TOKEN || '',
ZENDESK_API_URL: process.env.ZENDESK_API_URL || '',
ZENDESK_CUSTOM_FIELD_TAG_ID: process.env.ZENDESK_CUSTOM_FIELD_TAG_ID,
ZENDESK_DEFAULT_PRIORITY: process.env.ZENDESK_DEFAULT_PRIORITY || 'high'
ZENDESK_DEFAULT_PRIORITY: process.env.ZENDESK_DEFAULT_PRIORITY || 'high',
INTERNAL_CACHE_TTL: process.env.INTERNAL_CACHE_TTL || 1800
}
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use the base image with Node.js
FROM node:10.20-jessie
FROM node:12.22.12-buster

# Copy the current directory into the Docker image
COPY . /challenge-api
Expand Down
3,747 changes: 1,921 additions & 1,826 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
"standard": "^12.0.1"
},
"dependencies": {
"aws-sdk": "^2.466.0",
"aws-sdk": "^2.1145.0",
"axios": "^0.19.0",
"bluebird": "^3.5.1",
"body-parser": "^1.15.1",
"config": "^3.0.1",
"cors": "^2.7.1",
"dotenv": "^8.2.0",
"dynamoose": "^1.8.0",
"dynamoose": "^1.11.1",
"elasticsearch": "^16.1.1",
"express": "^4.15.4",
"express-fileupload": "^1.1.6",
Expand All @@ -57,6 +57,7 @@
"jsonwebtoken": "^8.3.0",
"lodash": "^4.17.19",
"moment": "^2.24.0",
"node-cache": "^5.1.2",
"swagger-ui-express": "^4.1.3",
"tc-core-library-js": "appirio-tech/tc-core-library-js.git#v2.6.4",
"topcoder-bus-api-wrapper": "topcoder-platform/tc-bus-api-wrapper.git",
Expand All @@ -77,6 +78,6 @@
"node": "10.x"
},
"volta": {
"node": "10.22.1"
"node": "12.22.12"
}
}
29 changes: 21 additions & 8 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_
const axios = require('axios')
const busApi = require('topcoder-bus-api-wrapper')
const elasticsearch = require('elasticsearch')
const moment = require('moment')
const NodeCache = require('node-cache')
const HttpStatus = require('http-status-codes')
const xss = require('xss')
const logger = require('./logger')
Expand Down Expand Up @@ -48,6 +48,9 @@ function wrapExpress (fn) {
}
}

// Internal cache
const internalCache = new NodeCache({ stdTTL: config.INTERNAL_CACHE_TTL })

/**
* Wrap all functions from object
* @param obj the object (controller exports)
Expand Down Expand Up @@ -447,7 +450,7 @@ async function createResource (challengeId, memberHandle, roleId) {
* @param {String} description The description
* @param {String} type The type
* @param {String} token The token
* @returns
* @returns
*/
async function createSelfServiceProject (name, description, type, token) {
const projectObj = {
Expand All @@ -456,15 +459,15 @@ async function createSelfServiceProject (name, description, type, token) {
type
}
const url = `${config.PROJECTS_API_URL}`
const res = await axios.post(url, projectObj, {headers: {Authorization: `Bearer ${token}`}})
const res = await axios.post(url, projectObj, { headers: { Authorization: `Bearer ${token}` } })
return _.get(res, 'data.id')
}

/**
* Get project payment
* @param {String} projectId the project id
*/
async function getProjectPayment (projectId) {
async function getProjectPayment (projectId) {
const token = await getM2MToken()
const url = `${config.CUSTOMER_PAYMENTS_URL}`
const res = await axios.get(url, {
Expand All @@ -473,7 +476,7 @@ async function createSelfServiceProject (name, description, type, token) {
referenceId: projectId,
reference: 'project'
}
})
})
const [payment] = res.data
return payment
}
Expand Down Expand Up @@ -514,7 +517,7 @@ async function cancelPayment (paymentId) {
* @param {String} cancelReason the cancel reasonn
* @param {Object} currentUser the current user
*/
async function cancelProject (projectId, cancelReason, currentUser) {
async function cancelProject (projectId, cancelReason, currentUser) {
let payment = await getProjectPayment(projectId)
const project = await ensureProjectExist(projectId, currentUser)
if (project.status === 'cancelled') return // already canceled
Expand Down Expand Up @@ -998,7 +1001,7 @@ async function validateChallengeTerms (terms = []) {
async function _filterChallengesByGroupsAccess (currentUser, challenges) {
const res = []
const needToCheckForGroupAccess = !currentUser ? true : !currentUser.isMachine && !hasAdminRole(currentUser)
if(!needToCheckForGroupAccess) return challenges
if (!needToCheckForGroupAccess) return challenges

let userGroups

Expand Down Expand Up @@ -1234,6 +1237,14 @@ async function submitZendeskRequest (request) {
}
}

function getFromInternalCache (key) {
return internalCache.get(key)
}

function setToInternalCache (key, value) {
internalCache.set(key, value)
}

module.exports = {
wrapExpress,
autoWrapExpress,
Expand Down Expand Up @@ -1287,5 +1298,7 @@ module.exports = {
sendSelfServiceNotification,
getMemberByHandle,
submitZendeskRequest,
updateSelfServiceProjectInfo
updateSelfServiceProjectInfo,
getFromInternalCache,
setToInternalCache
}
Loading