Add 'cache_policy' parameter. Closes #62

This commit is contained in:
Sandro Santilli 2012-11-12 19:14:20 +01:00
parent 005ae48e3a
commit 120cf3f0c5
4 changed files with 33 additions and 2 deletions

View File

@ -2,6 +2,7 @@
-----
* Support for specifying a filename for exports (#64)
* Support for specifying a list of fields to skip from output (#63)
* Add 'cache_policy' parameter (#62)
1.2.1 (DD/MM/YY)
-----

View File

@ -224,9 +224,17 @@ function handleQuery(req, res) {
setCrossDomain(res);
// set cache headers
res.header('Last-Modified', new Date().toUTCString());
res.header('Cache-Control', 'no-cache,max-age=3600,must-revalidate,public');
res.header('X-Cache-Channel', generateCacheKey(database, tableCache[sql_md5], authenticated));
var cache_policy = req.query.cache_policy;
if ( cache_policy == 'persist' ) {
res.header('Cache-Control', 'public,max-age=31536000'); // 1 year
} else {
// TODO: set ttl=0 when tableCache[sql_md5].may_write is true ?
var ttl = 3600;
res.header('Last-Modified', new Date().toUTCString());
res.header('Cache-Control', 'no-cache,max-age='+ttl+',must-revalidate,public');
}
return result;
},

View File

@ -27,6 +27,13 @@ Supported query string parameters:
'api_key': Needed to authenticate in order to modify the database.
'cache_policy':
Set to "persist" to have the server send an Cache-Control
header requesting caching devices to keep the response
cached as much as possible. This is best used with a
timestamp value in cache_buster for manual control of
updates.
Response formats
----------------

View File

@ -30,6 +30,7 @@ app.setMaxListeners(0);
suite('app.test', function() {
var expected_cache_control = 'no-cache,max-age=3600,must-revalidate,public';
var expected_cache_control_persist = 'public,max-age=31536000';
// use dec_sep for internationalization
var checkDecimals = function(x, dec_sep){
@ -67,6 +68,20 @@ test('GET /api/v1/sql with SQL parameter on SELECT only. No oAuth included ', fu
});
});
test('cache_policy=persist', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&database=cartodb_test_user_1_db&cache_policy=persist',
method: 'GET'
},{ }, function(res) {
assert.equal(res.statusCode, 200, res.body);
// Check cache headers
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/43
assert.equal(res.headers['x-cache-channel'], 'cartodb_test_user_1_db:untitle_table_4');
assert.equal(res.headers['cache-control'], expected_cache_control_persist);
done();
});
});
test('GET /api/v1/sql with SQL parameter on SELECT only. no database param, just id using headers', function(done){
assert.response(app, {
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4',