From e0ffeb0adc0c5ea6b248667b3c41b0dfed2b8af9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 31 Oct 2017 19:49:12 +0100 Subject: [PATCH] extract surrogate key functionality to its own middleware --- lib/cartodb/controllers/map.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 1859dd07..ac874e59 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -274,6 +274,7 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn res.locals.layergroup = layergroup; res.locals.template = mapConfigProvider.template; res.locals.context = mapConfigProvider.context; + res.locals.templateName = mapConfigProvider.getTemplateName(); const afterLayergroupCreate = self.afterLayergroupCreateBuilder({ useTemplateHash: true @@ -289,8 +290,6 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn const { layergroup } = res.locals; - self.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(cdbuser, mapConfigProvider.getTemplateName())); - res.status(200); if (req.query && req.query.callback) { @@ -360,6 +359,10 @@ MapController.prototype.afterLayergroupCreateBuilder = function (options = {}) { assert.ifError(err); self.setTurboCartoMetadataToLayergroup(req, res, this); }, + function setSurrogateKeyHeader (err) { + assert.ifError(err); + self.setSurrogateKeyHeader(req, res, this); + }, function finish(err) { callback(err); } @@ -431,15 +434,11 @@ MapController.prototype.getAffectedTables = function (req, res, callback) { }; MapController.prototype.setCacheChannel = function (req, res, callback) { - const self = this; const { affectedTables } = res.locals; if (req.method === 'GET') { res.set('Last-Modified', (new Date()).toUTCString()); res.set('X-Cache-Channel', affectedTables.getCacheChannel()); - if (affectedTables.tables && affectedTables.tables.length > 0) { - self.surrogateKeysCache.tag(res, affectedTables); - } } callback(); @@ -547,6 +546,20 @@ function addContextMetadata(layergroup, mapConfig, context) { } } +MapController.prototype.setSurrogateKeyHeader = function (req, res, callback) { + const { affectedTables, user, templateName } = res.locals; + + if (req.method === 'GET' && affectedTables.tables && affectedTables.tables.length > 0) { + this.surrogateKeysCache.tag(res, affectedTables); + } + + if (templateName) { + this.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(user, templateName)); + } + + callback(); +}; + function getLastUpdatedTime(analysesResults, lastUpdateTime) { if (!Array.isArray(analysesResults)) { return lastUpdateTime;