Skip to content

Commit

Permalink
Merge pull request #89 from ELEVATE-Project/caching
Browse files Browse the repository at this point in the history
Caching
  • Loading branch information
rakeshSgr authored Jul 21, 2022
2 parents bc43ba0 + 5918689 commit 50887f8
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 60 deletions.
8 changes: 7 additions & 1 deletion src/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,10 @@ ENABLE_LOG = true


# Api doc url
API_DOC_URL = '/api-doc'
API_DOC_URL = '/api-doc'

#Internal cache expiry time
INTERNAL_CACHE_EXP_TIME = 86400

#Redis Cache expiry Time
REDIS_CACHE_EXP_TIME = 86400
1 change: 0 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const path = require('path')
const i18next = require('i18next')
const Backend = require('i18next-fs-backend')
const middleware = require('i18next-http-middleware')

let environmentData = require('./envVariables')()

if (!environmentData.success) {
Expand Down
6 changes: 6 additions & 0 deletions src/configs/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { RedisConfig, InternalCache, RedisHelper } = require('elevate-node-cache')
module.exports = () => {
RedisConfig.config(process.env.REDIS_HOST)
InternalCache.init(process.env.INTERNAL_CACHE_EXP_TIME)
RedisHelper.init(process.env.REDIS_CACHE_EXP_TIME)
}
2 changes: 2 additions & 0 deletions src/configs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
require('./mongodb')()

require('./kafka')()

require('./cache')()
16 changes: 14 additions & 2 deletions src/constants/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@
* Description : All commonly used constants through out the service
*/

const successResponse = ({ statusCode = 500, responseCode = 'OK', message, result = [], meta = {} }) => {
const FormsData = require('@db/forms/queries')
const utils = require('@generics/utils')
const successResponse = async ({ statusCode = 500, responseCode = 'OK', message, result = [], meta = {} }) => {
const formVersionData = (await utils.internalGet('formVersion')) || false
let versions = {}
if (formVersionData) {
versions = formVersionData
} else {
versions = await FormsData.findAllTypeFormVersion()
await utils.internalSet('formVersion', versions)
}
return {
statusCode,
responseCode,
message,
result,
meta,
meta: { ...meta, formsVersion: versions },
}
}

Expand Down Expand Up @@ -43,4 +53,6 @@ module.exports = {
PUBLISHED_STATUS: 'published',
LIVE_STATUS: 'live',
MENTOR_EVALUATING: 'mentor',
internalCacheExpirationTime: process.env.INTERNAL_CACHE_EXP_TIME, // In Seconds
RedisCacheExpiryTime: process.env.REDIS_CACHE_EXP_TIME,
}
11 changes: 11 additions & 0 deletions src/db/entities/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,15 @@ module.exports = class UserEntityData {
}
})
}

static async findOne(_id) {
try {
const filter = {
_id: ObjectId(_id),
}
return await Entities.findOne(filter)
} catch (error) {
return error
}
}
}
1 change: 1 addition & 0 deletions src/db/forms/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Schema = mongoose.Schema
const formSchema = new Schema({
type: {
type: String,
index: { unique: true },
required: true,
},
subType: {
Expand Down
36 changes: 33 additions & 3 deletions src/db/forms/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Description : Users database operations
*/

const utils = require('@generics/utils')
const Forms = require('./model')

module.exports = class FormsData {
Expand All @@ -19,8 +20,8 @@ module.exports = class FormsData {
})
}

static findOneForm(type, subType, action, ver, templateName) {
const filter = { type, subType, action, ver, 'data.templateName': templateName }
static findOneForm(type) {
const filter = { type }
const projection = {}
return new Promise(async (resolve, reject) => {
try {
Expand All @@ -32,12 +33,30 @@ module.exports = class FormsData {
})
}

static findAllTypeFormVersion() {
const projection = {
type: 1,
ver: 1,
}
return new Promise(async (resolve, reject) => {
try {
const formData = await Forms.find({}, projection)
let versions = {}
formData.forEach((version) => {
versions[version.type] = version.ver
})
resolve(versions)
} catch (error) {
reject(error)
}
})
}

static updateOneForm(update, options = {}) {
const filter = {
type: update.type,
subType: update.subType,
action: update.action,
ver: update.ver,
'data.templateName': update.data.templateName,
}
return new Promise(async (resolve, reject) => {
Expand All @@ -58,4 +77,15 @@ module.exports = class FormsData {
}
})
}

static async checkVersion(bodyData) {
try {
const filter = { type: bodyData.type }
const projection = { type: 1, ver: 1 }
const formData = await Forms.findOne(filter, projection)
return utils.compareVersion(formData.ver, bodyData.ver)
} catch (err) {
return err
}
}
}
8 changes: 8 additions & 0 deletions src/envVariables.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ let enviromentVariables = {
message: 'Required kafka topic',
optional: false,
},
INTERNAL_CACHE_EXP_TIME: {
message: 'Internal Cache Expiry Time',
optional: false,
},
REDIS_CACHE_EXP_TIME: {
message: 'Redis Cache Expiry Time',
optional: false,
},
}

let success = true
Expand Down
43 changes: 43 additions & 0 deletions src/generics/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const momentTimeZone = require('moment-timezone')
const moment = require('moment')
const path = require('path')
const md5 = require('md5')
const { RedisHelper, InternalCache } = require('elevate-node-cache')

const hash = (str) => {
const salt = bcryptJs.genSaltSync(10)
Expand Down Expand Up @@ -119,6 +120,41 @@ function md5Hash(value) {
return md5(value)
}

function compareVersion(dbValue, apiValue) {
if (dbValue == apiValue) {
return false
} else {
dbValue = dbValue.split('.')
apiValue = apiValue.split('.')
for (let i = 0; i < dbValue.length; i++) {
if (dbValue[i] > apiValue[i]) {
return false
}
}
return true
}
}

function internalSet(key, value) {
return InternalCache.setKey(key, value)
}
function internalGet(key) {
return InternalCache.getKey(key)
}
function internalDel(key) {
return InternalCache.delKey(key)
}

function redisSet(key, value, exp) {
return RedisHelper.setKey(key, value, exp)
}
function redisGet(key) {
return RedisHelper.getKey(key)
}
function redisDel(key) {
return RedisHelper.deleteKey(key)
}

module.exports = {
hash: hash,
getCurrentMonthRange: getCurrentMonthRange,
Expand All @@ -131,4 +167,11 @@ module.exports = {
getTimeZone,
utcFormat: utcFormat,
md5Hash: md5Hash,
compareVersion: compareVersion,
internalSet: internalSet,
internalDel: internalDel,
internalGet: internalGet,
redisSet: redisSet,
redisGet: redisGet,
redisDel: redisDel,
}
2 changes: 1 addition & 1 deletion src/globalConfig.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "mongoUri": "mongodb://127.0.0.1:60964/", "mongoDBName": "jest" }
{ "mongoUri": "mongodb://127.0.0.1:51929/", "mongoDBName": "jest" }
3 changes: 2 additions & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@
"SESSION_DURATION_TIME": "Session cannot be created ! Session duration should be less than 24 hours.",
"SESSION__MINIMUM_DURATION_TIME": "End time should be atleast 30 minutes after start time.",
"PROFILE_FTECHED_SUCCESSFULLY": "Profile fetched successfully.",
"UPCOMING_SESSION_FETCHED": "Upcoming session fetched successfully."
"UPCOMING_SESSION_FETCHED": "Upcoming session fetched successfully.",
"UPDATE_FORM_VERSION": "Update form version."
}
1 change: 1 addition & 0 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"cors": "^2.8.5",
"crypto": "^1.0.1",
"dotenv": "^10.0.0",
"elevate-node-cache": "^1.0.2",
"express": "^4.17.1",
"express-validator": "^5.3.1",
"files-cloud-storage": "^1.2.2",
Expand Down
25 changes: 23 additions & 2 deletions src/services/helper/entity.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Dependencies
const ObjectId = require('mongoose').Types.ObjectId
const utilsHelper = require('@generics/utils')

const httpStatusCode = require('@generics/http-status')
const common = require('@constants/common')
const entitiesData = require('@db/entities/query')

const utils = require('@generics/utils')
module.exports = class EntityHelper {
/**
* Create entity.
Expand All @@ -28,6 +29,8 @@ module.exports = class EntityHelper {
})
}
await entitiesData.createEntity(bodyData)
const key = 'entity_' + bodyData.type
await utils.internalDel(key)
return common.successResponse({
statusCode: httpStatusCode.created,
message: 'ENTITY_CREATED_SUCCESSFULLY',
Expand Down Expand Up @@ -65,6 +68,15 @@ module.exports = class EntityHelper {
responseCode: 'CLIENT_ERROR',
})
}
let key = ''
if (bodyData.type) {
key = 'entity_' + bodyData.type
await utils.internalDel(key)
} else {
const entities = await entitiesData.findOne(_id)
key = 'entity_' + entities.type
await utils.internalDel(key)
}
return common.successResponse({
statusCode: httpStatusCode.accepted,
message: 'ENTITY_UPDATED_SUCCESSFULLY',
Expand All @@ -87,7 +99,12 @@ module.exports = class EntityHelper {
bodyData.deleted = false
}
try {
const entities = await entitiesData.findAllEntities(bodyData)
const key = 'entity_' + bodyData.type
let entities = (await utils.internalGet(key)) || false
if (!entities) {
entities = await entitiesData.findAllEntities(bodyData)
await utils.internalSet(key, entities)
}
return common.successResponse({
statusCode: httpStatusCode.ok,
message: 'ENTITY_FETCHED_SUCCESSFULLY',
Expand Down Expand Up @@ -122,6 +139,10 @@ module.exports = class EntityHelper {
responseCode: 'CLIENT_ERROR',
})
}
const entities = await entitiesData.findOne(_id)
let key = 'entity_' + entities.type
await utils.internalDel(key)

return common.successResponse({
statusCode: httpStatusCode.accepted,
message: 'ENTITY_DELETED_SUCCESSFULLY',
Expand Down
Loading

0 comments on commit 50887f8

Please sign in to comment.