Inline method to set cache-cannel, rollback extract setCacheChannel method for named maps controller

This commit is contained in:
Daniel García Aubert 2017-11-13 12:35:46 +01:00
parent 590233e3ee
commit 38ca5db51b

View File

@ -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);
}
}
);