From 38ca5db51b0157f3af812f7b15b8940f6b34b838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Mon, 13 Nov 2017 12:35:46 +0100 Subject: [PATCH] Inline method to set cache-cannel, rollback extract setCacheChannel method for named maps controller --- lib/cartodb/controllers/named_maps.js | 77 +++++++++++---------------- 1 file changed, 32 insertions(+), 45 deletions(-) diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index ffea03a2..55790bbf 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -39,56 +39,43 @@ NamedMapsController.prototype.register = function(app) { ); }; -NamedMapsController.prototype.sendResponse = function(req, res, body, headers) { - const { namedMapProvider } = res.locals; - +NamedMapsController.prototype.sendResponse = function(req, res, body, headers, namedMapProvider) { this.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(res.locals.user, namedMapProvider.getTemplateName())); res.set('Content-Type', headers['content-type'] || headers['Content-Type'] || 'image/png'); res.set('Cache-Control', 'public,max-age=7200,must-revalidate'); - this.setCacheChannel(req, res, (err) => { - if (err) { - global.logger.log('ERROR generating cache channel: ' + err); - } + var self = this; - res.status(200); - res.send(body); - }); -}; - -NamedMapsController.prototype.setCacheChannel = function(req, res, callback) { - const { namedMapProvider } = res.locals; - - this.getAffectedTables(namedMapProvider, (err, result) => { - - req.profiler.done('affectedTables'); - if (err) { - global.logger.log('ERROR generating cache channel: ' + err); - } - if (!result || !!result.tables) { - // we increase cache control as we can invalidate it - res.set('Cache-Control', 'public,max-age=31536000'); - - var lastModifiedDate; - if (Number.isFinite(result.lastUpdatedTime)) { - lastModifiedDate = new Date(result.getLastUpdatedAt()); - } else { - lastModifiedDate = new Date(); + step( + function getAffectedTablesAndLastUpdatedTime() { + namedMapProvider.getAffectedTablesAndLastUpdatedTime(this); + }, + function sendResponse(err, result) { + req.profiler.done('affectedTables'); + if (err) { + global.logger.log('ERROR generating cache channel: ' + err); } - res.set('Last-Modified', lastModifiedDate.toUTCString()); + if (!result || !!result.tables) { + // we increase cache control as we can invalidate it + res.set('Cache-Control', 'public,max-age=31536000'); - res.set('X-Cache-Channel', result.getCacheChannel()); - if (result.tables.length > 0) { - this.surrogateKeysCache.tag(res, result); + var lastModifiedDate; + if (Number.isFinite(result.lastUpdatedTime)) { + lastModifiedDate = new Date(result.getLastUpdatedAt()); + } else { + lastModifiedDate = new Date(); + } + res.set('Last-Modified', lastModifiedDate.toUTCString()); + + res.set('X-Cache-Channel', result.getCacheChannel()); + if (result.tables.length > 0) { + self.surrogateKeysCache.tag(res, result); + } } + res.status(200); + res.send(body); } - - callback(); - }); -}; - -NamedMapsController.prototype.getAffectedTables = function(namedMapProvider, callback) { - namedMapProvider.getAffectedTablesAndLastUpdatedTime(callback); + ); }; NamedMapsController.prototype.tile = function(req, res, next) { @@ -110,7 +97,7 @@ NamedMapsController.prototype.tile = function(req, res, next) { }, function getTile(err, _namedMapProvider) { assert.ifError(err); - res.locals.namedMapProvider = namedMapProvider = _namedMapProvider; + namedMapProvider = _namedMapProvider; self.tileBackend.getTile(namedMapProvider, req.params, this); }, function handleImage(err, tile, headers, stats) { @@ -119,7 +106,7 @@ NamedMapsController.prototype.tile = function(req, res, next) { err.label = 'NAMED_MAP_TILE'; next(err); } else { - self.sendResponse(req, res, tile, headers); + self.sendResponse(req, res, tile, headers, namedMapProvider); } } ); @@ -149,7 +136,7 @@ NamedMapsController.prototype.staticMap = function(req, res, next) { function prepareLayerVisibility(err, _namedMapProvider) { assert.ifError(err); - res.locals.namedMapProvider = namedMapProvider = _namedMapProvider; + namedMapProvider = _namedMapProvider; self.prepareLayerFilterFromPreviewLayers(cdbUser, req, res.locals, namedMapProvider, this); }, @@ -192,7 +179,7 @@ NamedMapsController.prototype.staticMap = function(req, res, next) { err.label = 'STATIC_VIZ_MAP'; next(err); } else { - self.sendResponse(req, res, image, headers); + self.sendResponse(req, res, image, headers, namedMapProvider); } } );