Add a cache decorator over QueryTables
This commit is contained in:
parent
69fa13ab50
commit
b8460d033e
@ -188,7 +188,7 @@ function App() {
|
||||
var genericController = new GenericController();
|
||||
genericController.route(app);
|
||||
|
||||
var queryController = new QueryController(userDatabaseService, statsd_client);
|
||||
var queryController = new QueryController(userDatabaseService, tableCache, statsd_client);
|
||||
queryController.route(app);
|
||||
|
||||
var jobController = new JobController(userDatabaseService, jobBackend, jobCanceller);
|
||||
|
@ -4,7 +4,7 @@ var _ = require('underscore');
|
||||
var step = require('step');
|
||||
var assert = require('assert');
|
||||
var PSQL = require('cartodb-psql');
|
||||
var QueryTables = require('cartodb-query-tables');
|
||||
var CachedQueryTables = require('../services/cached-query-tables');
|
||||
var AuthApi = require('../auth/auth_api');
|
||||
var queryMayWrite = require('../utils/query_may_write');
|
||||
|
||||
@ -19,9 +19,10 @@ var ONE_YEAR_IN_SECONDS = 31536000; // 1 year time to live by default
|
||||
|
||||
var cdbReq = new CdbRequest();
|
||||
|
||||
function QueryController(userDatabaseService, statsd_client) {
|
||||
function QueryController(userDatabaseService, tableCache, statsd_client) {
|
||||
this.statsd_client = statsd_client;
|
||||
this.userDatabaseService = userDatabaseService;
|
||||
this.queryTables = new CachedQueryTables(tableCache);
|
||||
}
|
||||
|
||||
QueryController.prototype.route = function (app) {
|
||||
@ -136,7 +137,7 @@ QueryController.prototype.handleQuery = function (req, res) {
|
||||
|
||||
var pg = new PSQL(authDbParams, {}, { destroyOnError: true });
|
||||
|
||||
QueryTables.getAffectedTablesFromQuery(pg, sql, function(err, result) {
|
||||
self.queryTables.getAffectedTablesFromQuery(pg, sql, function(err, result) {
|
||||
if (err) {
|
||||
var errorMessage = (err && err.message) || 'unknown error';
|
||||
console.error("Error on query explain '%s': %s", sql, errorMessage);
|
||||
|
38
app/services/cached-query-tables.js
Normal file
38
app/services/cached-query-tables.js
Normal file
@ -0,0 +1,38 @@
|
||||
var QueryTables = require('cartodb-query-tables');
|
||||
|
||||
var generateMD5 = require('../utils/md5');
|
||||
|
||||
function CachedQueryTables(tableCache) {
|
||||
this.tableCache = tableCache;
|
||||
}
|
||||
|
||||
module.exports = CachedQueryTables;
|
||||
|
||||
CachedQueryTables.prototype.getAffectedTablesFromQuery = function(pg, sql, callback) {
|
||||
var self = this;
|
||||
|
||||
var cacheKey = sqlCacheKey(pg.username(), sql);
|
||||
var cachedResult = this.tableCache.get(cacheKey);
|
||||
|
||||
if (cachedResult) {
|
||||
cachedResult.hits++;
|
||||
return callback(null, cachedResult.result);
|
||||
} else {
|
||||
QueryTables.getAffectedTablesFromQuery(pg, sql, function(err, result) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
self.tableCache.set(cacheKey, {
|
||||
result: result,
|
||||
hits: 1
|
||||
});
|
||||
|
||||
return callback(null, result);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function sqlCacheKey(user, sql) {
|
||||
return user + ':' + generateMD5(sql);
|
||||
}
|
Loading…
Reference in New Issue
Block a user