add specific cache key

This commit is contained in:
Simon Tokumine 2012-04-20 14:34:18 -04:00
parent d704dc39d0
commit 9bb7afb7f5

View File

@ -44,7 +44,7 @@ function handleQuery(req, res){
var dp = (req.query.dp) ? req.query.dp: '15'; var dp = (req.query.dp) ? req.query.dp: '15';
// validate input slightly // validate input slightly
dp = (dp=== "") ? '15' : dp; dp = (dp === "") ? '15' : dp;
format = (format === "") ? null : format; format = (format === "") ? null : format;
sql = (sql === "") ? null : sql; sql = (sql === "") ? null : sql;
database = (database === "") ? null : database; database = (database === "") ? null : database;
@ -52,17 +52,17 @@ function handleQuery(req, res){
offset = (_.isNumber(offset)) ? offset * limit : null offset = (_.isNumber(offset)) ? offset * limit : null
// setup step run // setup step run
var that = this;
var start = new Date().getTime(); var start = new Date().getTime();
try { try {
if (!_.isString(sql)) throw new Error("You must indicate a sql query"); if (!_.isString(sql)) throw new Error("You must indicate a sql query");
var pg; var pg, explain_result;
// 1. Get database from redis via the username stored in the host header subdomain // 1. Get database from redis via the username stored in the host header subdomain
// 2. Run the request through OAuth to get R/W user id if signed // 2. Run the request through OAuth to get R/W user id if signed
// 3. Run query with r/w or public user // 3. Get the list of tables affected by the query
// 4. package results and send back // 4. Run query with r/w or public user
// 5. package results and send back
Step( Step(
function getDatabaseName(){ function getDatabaseName(){
Meta.getDatabase(req, this); Meta.getDatabase(req, this);
@ -76,10 +76,19 @@ function handleQuery(req, res){
oAuth.verifyRequest(req, this); oAuth.verifyRequest(req, this);
} }
}, },
function querySql(err, user_id){ function queryExplain(err, user_id){
if (err) throw err; if (err) throw err;
// store postgres connection
pg = new PSQL(user_id, database, limit, offset); pg = new PSQL(user_id, database, limit, offset);
// get all the tables
pg.query('SELECT CDB_QueryTables(\''+sql+'\')', this);
},
function queryResult(err, result){
if (err) throw err;
// store explain result
explain_result = result;
// TODO: refactor formats to external object // TODO: refactor formats to external object
if (format === 'geojson'){ if (format === 'geojson'){
sql = ['SELECT *, ST_AsGeoJSON(the_geom,',dp,') as the_geom FROM (', sql, ') as foo'].join(""); sql = ['SELECT *, ST_AsGeoJSON(the_geom,',dp,') as the_geom FROM (', sql, ') as foo'].join("");
@ -100,7 +109,7 @@ function handleQuery(req, res){
// set cache headers // set cache headers
res.header('Last-Modified', new Date().toUTCString()); res.header('Last-Modified', new Date().toUTCString());
res.header('Cache-Control', 'no-cache,max-age=3600,must-revalidate, public'); res.header('Cache-Control', 'no-cache,max-age=3600,must-revalidate, public');
res.header('X-Cache-Channel', database); res.header('X-Cache-Channel', generateCacheKey(database,explain_result));
return result; return result;
}, },
@ -204,6 +213,10 @@ function setCrossDomain(res){
res.header("Access-Control-Allow-Headers", "X-Requested-With"); res.header("Access-Control-Allow-Headers", "X-Requested-With");
} }
function generateCacheKey(database,tables){
return database + ":" + tables.rows[0].cdb_querytables.split(/^\{(.*)\}$/)[1];
}
function handleException(err, res){ function handleException(err, res){
var msg = (global.settings.environment == 'development') ? {error:[err.message], stack: err.stack} : {error:[err.message]} var msg = (global.settings.environment == 'development') ? {error:[err.message], stack: err.stack} : {error:[err.message]}
if (global.settings.environment !== 'test'){ if (global.settings.environment !== 'test'){