Skip query tables cache for authenticated requests

This commit is contained in:
Raul Ochoa 2016-03-10 19:20:56 +01:00
parent b453f2b2d0
commit 66cd1f400f
3 changed files with 32 additions and 3 deletions

View File

@ -136,8 +136,9 @@ QueryController.prototype.handleQuery = function (req, res) {
checkAborted('queryExplain');
var pg = new PSQL(authDbParams, {}, { destroyOnError: true });
var skipCache = !!dbopts.authenticated;
self.queryTables.getAffectedTablesFromQuery(pg, sql, function(err, result) {
self.queryTables.getAffectedTablesFromQuery(pg, sql, skipCache, function(err, result) {
if (err) {
var errorMessage = (err && err.message) || 'unknown error';
console.error("Error on query explain '%s': %s", sql, errorMessage);

View File

@ -8,11 +8,15 @@ function CachedQueryTables(tableCache) {
module.exports = CachedQueryTables;
CachedQueryTables.prototype.getAffectedTablesFromQuery = function(pg, sql, callback) {
CachedQueryTables.prototype.getAffectedTablesFromQuery = function(pg, sql, skipCache, callback) {
var self = this;
var cacheKey = sqlCacheKey(pg.username(), sql);
var cachedResult = this.tableCache.get(cacheKey);
var cachedResult;
if (!skipCache) {
cachedResult = this.tableCache.get(cacheKey);
}
if (cachedResult) {
cachedResult.hits++;

View File

@ -64,4 +64,28 @@ describe('query-tables-api', function() {
});
});
});
it('should skip cache to retrieve affected tables', function(done) {
var authenticatedRequest = {
url: '/api/v1/sql?' + qs.stringify({
q: 'SELECT * FROM untitle_table_4',
api_key: '1234'
}),
headers: {
host: 'vizzuality.cartodb.com'
},
method: 'GET'
};
assert.response(app, authenticatedRequest, RESPONSE_OK, function(res, err) {
assert.ok(!err, err);
getCacheStatus(function(err, cacheStatus) {
assert.ok(!err, err);
assert.equal(cacheStatus.explain.keys, 1);
assert.equal(cacheStatus.explain.hits, 0);
done();
});
});
});
});