Set directive 'max-age' to 5 min when there are affacted tables where we can't know when were updated for the last time, e.g: non cartodified tables or foreing tables without cartodb support
This commit is contained in:
parent
9cb149fa32
commit
bc29587c55
@ -326,6 +326,7 @@ var config = {
|
|||||||
purge_enabled: false, // whether the purge/invalidation mechanism is enabled in varnish or not
|
purge_enabled: false, // whether the purge/invalidation mechanism is enabled in varnish or not
|
||||||
secret: 'xxx',
|
secret: 'xxx',
|
||||||
ttl: 86400,
|
ttl: 86400,
|
||||||
|
fallbackTtl: 300,
|
||||||
layergroupTtl: 86400 // the max-age for cache-control header in layergroup responses
|
layergroupTtl: 86400 // the max-age for cache-control header in layergroup responses
|
||||||
}
|
}
|
||||||
// this [OPTIONAL] configuration enables invalidating by surrogate key in fastly
|
// this [OPTIONAL] configuration enables invalidating by surrogate key in fastly
|
||||||
|
@ -326,6 +326,7 @@ var config = {
|
|||||||
purge_enabled: false, // whether the purge/invalidation mechanism is enabled in varnish or not
|
purge_enabled: false, // whether the purge/invalidation mechanism is enabled in varnish or not
|
||||||
secret: 'xxx',
|
secret: 'xxx',
|
||||||
ttl: 86400,
|
ttl: 86400,
|
||||||
|
fallbackTtl: 300,
|
||||||
layergroupTtl: 86400 // the max-age for cache-control header in layergroup responses
|
layergroupTtl: 86400 // the max-age for cache-control header in layergroup responses
|
||||||
}
|
}
|
||||||
// this [OPTIONAL] configuration enables invalidating by surrogate key in fastly
|
// this [OPTIONAL] configuration enables invalidating by surrogate key in fastly
|
||||||
|
@ -326,6 +326,7 @@ var config = {
|
|||||||
purge_enabled: false, // whether the purge/invalidation mechanism is enabled in varnish or not
|
purge_enabled: false, // whether the purge/invalidation mechanism is enabled in varnish or not
|
||||||
secret: 'xxx',
|
secret: 'xxx',
|
||||||
ttl: 86400,
|
ttl: 86400,
|
||||||
|
fallbackTtl: 300,
|
||||||
layergroupTtl: 86400 // the max-age for cache-control header in layergroup responses
|
layergroupTtl: 86400 // the max-age for cache-control header in layergroup responses
|
||||||
}
|
}
|
||||||
// this [OPTIONAL] configuration enables invalidating by surrogate key in fastly
|
// this [OPTIONAL] configuration enables invalidating by surrogate key in fastly
|
||||||
|
@ -328,6 +328,7 @@ var config = {
|
|||||||
purge_enabled: false, // whether the purge/invalidation mechanism is enabled in varnish or not
|
purge_enabled: false, // whether the purge/invalidation mechanism is enabled in varnish or not
|
||||||
secret: 'xxx',
|
secret: 'xxx',
|
||||||
ttl: 86400,
|
ttl: 86400,
|
||||||
|
fallbackTtl: 300,
|
||||||
layergroupTtl: 86400 // the max-age for cache-control header in layergroup responses
|
layergroupTtl: 86400 // the max-age for cache-control header in layergroup responses
|
||||||
}
|
}
|
||||||
// this [OPTIONAL] configuration enables invalidating by surrogate key in fastly
|
// this [OPTIONAL] configuration enables invalidating by surrogate key in fastly
|
||||||
|
@ -1,21 +1,42 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const ONE_YEAR_IN_SECONDS = 60 * 60 * 24 * 365;
|
const ONE_YEAR_IN_SECONDS = 60 * 60 * 24 * 365;
|
||||||
|
const FIVE_MINUTES_IN_SECONDS = 60 * 5;
|
||||||
|
const FALLBACK_TTL = global.environment.varnish.fallbackTtl || FIVE_MINUTES_IN_SECONDS
|
||||||
|
|
||||||
module.exports = function setCacheControlHeader ({ ttl = ONE_YEAR_IN_SECONDS, revalidate = false } = {}) {
|
module.exports = function setCacheControlHeader ({
|
||||||
|
ttl = ONE_YEAR_IN_SECONDS,
|
||||||
|
fallbackTtl = FALLBACK_TTL,
|
||||||
|
revalidate = false
|
||||||
|
} = {}) {
|
||||||
return function setCacheControlHeaderMiddleware (req, res, next) {
|
return function setCacheControlHeaderMiddleware (req, res, next) {
|
||||||
if (req.method !== 'GET') {
|
if (req.method !== 'GET') {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
const directives = [ 'public', `max-age=${ttl}` ];
|
const { mapConfigProvider = { getAffectedTables: callback => callback() } } = res.locals;
|
||||||
|
|
||||||
if (revalidate) {
|
mapConfigProvider.getAffectedTables((err, affectedTables) => {
|
||||||
directives.push('must-revalidate');
|
if (err) {
|
||||||
}
|
global.logger.warn('ERROR generating Cache Control Header:', err);
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
res.set('Cache-Control', directives.join(','));
|
const directives = [ 'public' ]
|
||||||
|
|
||||||
next();
|
if (affectedTables && !affectedTables.getTables().some(table => !!table.updated_at)) {
|
||||||
|
directives.push(`max-age=${fallbackTtl}`);
|
||||||
|
} else {
|
||||||
|
directives.push(`max-age=${ttl}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (revalidate) {
|
||||||
|
directives.push('must-revalidate');
|
||||||
|
}
|
||||||
|
|
||||||
|
res.set('Cache-Control', directives.join(','));
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user