Extract cache-channel and surrogate-key middlewares
This commit is contained in:
parent
c48b8b2f69
commit
ae4b3a9f4f
@ -20,6 +20,8 @@ const affectedTables = require('../middlewares/affected-tables');
|
||||
const accessValidator = require('../middlewares/access-validator');
|
||||
const queryMayWrite = require('../middlewares/query-may-write');
|
||||
const cacheControl = require('../middlewares/cache-control');
|
||||
const cacheChannel = require('../middlewares/cache-channel');
|
||||
const surrogateKey = require('../middlewares/surrogate-key');
|
||||
|
||||
function QueryController(metadataBackend, userDatabaseService, statsd_client, userLimitsService) {
|
||||
this.metadataBackend = metadataBackend;
|
||||
@ -48,6 +50,8 @@ QueryController.prototype.route = function (app) {
|
||||
accessValidator(),
|
||||
queryMayWrite(),
|
||||
cacheControl(),
|
||||
cacheChannel(),
|
||||
surrogateKey(),
|
||||
this.handleQuery.bind(this),
|
||||
errorMiddleware()
|
||||
];
|
||||
@ -92,13 +96,6 @@ QueryController.prototype.handleQuery = function (req, res, next) {
|
||||
res.header("Content-Disposition", getContentDisposition(formatter, filename, useInline));
|
||||
res.header("Content-Type", formatter.getContentType());
|
||||
|
||||
// Only set an X-Cache-Channel for responses we want Varnish to cache.
|
||||
var skipNotUpdatedAtTables = true;
|
||||
if (!!affectedTables && affectedTables.getTables(skipNotUpdatedAtTables).length > 0 && !mayWrite) {
|
||||
res.header('X-Cache-Channel', affectedTables.getCacheChannel(skipNotUpdatedAtTables));
|
||||
res.header('Surrogate-Key', affectedTables.key(skipNotUpdatedAtTables).join(' '));
|
||||
}
|
||||
|
||||
if(!!affectedTables) {
|
||||
res.header('Last-Modified',
|
||||
new Date(affectedTables.getLastUpdatedAt(Number(new Date()))).toUTCString());
|
||||
|
14
app/middlewares/cache-channel.js
Normal file
14
app/middlewares/cache-channel.js
Normal file
@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function cacheChannel () {
|
||||
return function cacheChannelMiddleware (req, res, next) {
|
||||
const { affectedTables, mayWrite } = res.locals;
|
||||
const skipNotUpdatedAtTables = true;
|
||||
|
||||
if (!!affectedTables && affectedTables.getTables(skipNotUpdatedAtTables).length > 0 && !mayWrite) {
|
||||
res.header('X-Cache-Channel', affectedTables.getCacheChannel(skipNotUpdatedAtTables));
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
};
|
14
app/middlewares/surrogate-key.js
Normal file
14
app/middlewares/surrogate-key.js
Normal file
@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function surrogateKey () {
|
||||
return function surrogateKeyMiddleware (req, res, next) {
|
||||
const { affectedTables, mayWrite } = res.locals;
|
||||
const skipNotUpdatedAtTables = true;
|
||||
|
||||
if (!!affectedTables && affectedTables.getTables(skipNotUpdatedAtTables).length > 0 && !mayWrite) {
|
||||
res.header('Surrogate-Key', affectedTables.key(skipNotUpdatedAtTables).join(' '));
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue
Block a user