diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 98616484..208ac027 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -386,6 +386,8 @@ LayergroupController.prototype.staticMap = function(req, res, width, height, zoo }; LayergroupController.prototype.sendResponse = function(req, res, body, status, headers) { + var self = this; + req.profiler.done('res'); res.set('Cache-Control', 'public,max-age=31536000'); @@ -400,48 +402,42 @@ LayergroupController.prototype.sendResponse = function(req, res, body, status, h } res.set('Last-Modified', lastUpdated.toUTCString()); - this.setCacheChannel(req, res, (err) => { - if (err) { - global.logger.warn('ERROR generating cache channel: ' + err); - } + var dbName = res.locals.dbname; + step( + function getAffectedTables() { + self.getAffectedTables(res.locals.user, dbName, res.locals.token, this); + }, + function sendResponse(err, affectedTables) { + req.profiler.done('affectedTables'); + if (err) { + global.logger.warn('ERROR generating cache channel: ' + err); + } + if (!!affectedTables) { + res.set('X-Cache-Channel', affectedTables.getCacheChannel()); + self.surrogateKeysCache.tag(res, affectedTables); + } - if (headers) { - res.set(headers); - } + if (headers) { + res.set(headers); + } - res.status(status); + res.status(status); - if (!Buffer.isBuffer(body) && typeof body === 'object') { - if (req.query && req.query.callback) { - res.jsonp(body); + if (!Buffer.isBuffer(body) && typeof body === 'object') { + if (req.query && req.query.callback) { + res.jsonp(body); + } else { + res.json(body); + } } else { - res.json(body); + res.send(body); } - } else { - res.send(body); } - }); -}; - -LayergroupController.prototype.setCacheChannel = function(req, res, callback) { - const { dbname, user, token } = res.locals; - - this.getAffectedTables(user, dbname, token, (err, affectedTables) => { - req.profiler.done('affectedTables'); - if (err) { - return callback(err); - } - - if (!!affectedTables) { - res.set('X-Cache-Channel', affectedTables.getCacheChannel()); - this.surrogateKeysCache.tag(res, affectedTables); - } - - callback(); - }); + ); }; LayergroupController.prototype.getAffectedTables = function(user, dbName, layergroupId, callback) { + if (this.layergroupAffectedTables.hasAffectedTables(dbName, layergroupId)) { return callback(null, this.layergroupAffectedTables.get(dbName, layergroupId)); }