Skip to content

Commit

Permalink
Cache /v1/perspectives
Browse files Browse the repository at this point in the history
  • Loading branch information
pallavi2209 committed Jan 11, 2017
1 parent 85888fa commit 7c8c108
Showing 1 changed file with 96 additions and 40 deletions.
136 changes: 96 additions & 40 deletions api/v1/helpers/verbs/doFind.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const u = require('./utils');
const fu = require('./findUtils');
const COUNT_HEADER_NAME = require('../../constants').COUNT_HEADER_NAME;
const httpStatus = require('../../constants').httpStatus;
const redisCache = require('../../../../cache/redisCache').client;
const SECS_IN_MIN = 60;

/**
* Finds all matching records but only returns a subset of the results for
Expand All @@ -32,19 +34,25 @@ const httpStatus = require('../../constants').httpStatus;
* find command
*/
function doFindAndCountAll(reqResNext, props, opts) {
u.getScopedModel(props, opts.attributes).findAndCountAll(opts)
.then((o) => {
reqResNext.res.set(COUNT_HEADER_NAME, o.count);
const retval = o.rows.map((row) => {
if (props.modelName === 'Lens') {
delete row.dataValues.library;
}
return new Promise((resolve, reject) => {
u.getScopedModel(props, opts.attributes).findAndCountAll(opts)
.then((o) => {
reqResNext.res.set(COUNT_HEADER_NAME, o.count);
const retval = o.rows.map((row) => {
if (props.modelName === 'Lens') {
delete row.dataValues.library;
}

return u.responsify(row, props, reqResNext.req.method);
});
reqResNext.res.status(httpStatus.OK).json(retval);
})
.catch((err) => u.handleError(reqResNext.next, err, props.modelName));
return u.responsify(row, props, reqResNext.req.method);
});
resolve(retval);
// reqResNext.res.status(httpStatus.OK).json(retval);
})
.catch((err) => reject(err));
});


// .catch((err) => u.handleError(reqResNext.next, err, props.modelName));
} // doFindAndCountAll

/**
Expand All @@ -67,43 +75,48 @@ function doFindAll(reqResNext, props, opts) {
opts.where.tags.$contains = [];
}

u.getScopedModel(props, opts.attributes).findAll(opts)
.then((o) => {
reqResNext.res.set(COUNT_HEADER_NAME, o.length);
let retval = o.map((row) => {
if (props.modelName === 'Lens') {
delete row.dataValues.library;
}
return new Promise((resolve, reject) => {
u.getScopedModel(props, opts.attributes).findAll(opts)
.then((o) => {
reqResNext.res.set(COUNT_HEADER_NAME, o.length);
let retval = o.map((row) => {
if (props.modelName === 'Lens') {
delete row.dataValues.library;
}

return u.responsify(row, props, reqResNext.req.method);
});
return u.responsify(row, props, reqResNext.req.method);
});

const { tags } = reqResNext.req.swagger.params;
if (tags && tags.value && tags.value.length) {
retval = fu.filterArrFromArr(retval, tags.value);
}

reqResNext.res.status(httpStatus.OK).json(retval);
})
.catch((err) => u.handleError(reqResNext.next, err, props.modelName));
const { tags } = reqResNext.req.swagger.params;
if (tags && tags.value && tags.value.length) {
retval = fu.filterArrFromArr(retval, tags.value);
}
resolve(retval);
// reqResNext.res.status(httpStatus.OK).json(retval);
})
.catch((err) => reject(err));
});
// .catch((err) => u.handleError(reqResNext.next, err, props.modelName));
} // doFindAll

/**
* Finds all matching records. This function is just a wrapper around
* doFindAll or doFindAndCountAll, depending upon whether we need to paginate
* the results.
* the results. If cacheKey is provided, we cache the response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
* @param {Object} reqResNext - The object containing the request object, the
* response object and the next middleware function in the stack
* @param {Object} props - The helpers/nouns module for the given DB model
* @param {Object} opts - The "options" object to pass into the Sequelize
* find command
* @param {Object} cacheKey - Optional cache key used to cache the response in
* redis
*/
module.exports = function doFind(req, res, next, props) {
const opts = fu.options(req.swagger.params, props);
function doFindResponse(reqResNext, props, opts, cacheKey) {
if (opts.limit || opts.offset) {
res.links({
prev: req.originalUrl,
next: fu.getNextUrl(req.originalUrl, opts.limit, opts.offset),
reqResNext.res.links({
prev: reqResNext.req.originalUrl,
next: fu.getNextUrl(reqResNext.req.originalUrl, opts.limit, opts.offset),
});
}

Expand All @@ -112,9 +125,52 @@ module.exports = function doFind(req, res, next, props) {
* we're not doing a "limit" query, just to findAll, avoiding the extra
* "SELECT count(*)... " database call.
*/
let doFindPromise;
if (opts.limit) {
doFindAndCountAll({ req, res, next }, props, opts);
doFindPromise = doFindAndCountAll(reqResNext, props, opts);
} else {
doFindPromise = doFindAll(reqResNext, props, opts);
}

doFindPromise.then((retval) => {
reqResNext.res.status(httpStatus.OK).json(retval);

if (cacheKey) {
// cache the object by cacheKey. Store the key-value pair in cache
// with an expiry of 1 minute (60s)
const strObj = JSON.stringify(retval);
redisCache.setex(cacheKey, SECS_IN_MIN, strObj);
}
})
.catch((err) => u.handleError(reqResNext.next, err, props.modelName));
}

/**
* Finds all matching records. Get result from cache if present, else, get
* results from db and store in cache, if caching is enabled.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
* @param {Object} props - The helpers/nouns module for the given DB model
*/
module.exports = function doFind(req, res, next, props) {
const opts = fu.options(req.swagger.params, props);

if (props.cacheEnabled) {
const cacheKey = JSON.stringify(opts);

redisCache.get(cacheKey, (cacheErr, reply) => {
if (cacheErr || !reply) {
// if err or no reply, get resuls from db and set redis cache
doFindResponse({ req, res, next }, props, opts, cacheKey);
} else {
// get from cache
const dbObj = JSON.parse(reply);
res.status(httpStatus.OK).json(dbObj);
}
});
} else {
doFindAll({ req, res, next }, props, opts);
doFindResponse({ req, res, next }, props, opts);
}
}; // exports

0 comments on commit 7c8c108

Please sign in to comment.