diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index dc6f85ff..d9551c9c 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -385,8 +385,6 @@ 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'); @@ -401,43 +399,48 @@ LayergroupController.prototype.sendResponse = function(req, res, body, status, h } res.set('Last-Modified', lastUpdated.toUTCString()); - 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); - } - - res.status(status); - - if (!Buffer.isBuffer(body) && typeof body === 'object') { - if (req.query && req.query.callback) { - res.jsonp(body); - } else { - res.json(body); - } - } else { - res.send(body); - } + this.setCacheChannel(req, res, (err) => { + if (err) { + global.logger.warn('ERROR generating cache channel: ' + err); } - ); + if (headers) { + res.set(headers); + } + + res.status(status); + + if (!Buffer.isBuffer(body) && typeof body === 'object') { + if (req.query && req.query.callback) { + res.jsonp(body); + } else { + res.json(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)); }