Caching named map providers by template name and config/auth token

Named Map provider cache buster changes on creation
This commit is contained in:
Raul Ochoa
2015-07-14 21:17:58 +02:00
parent 91ab64dda9
commit 1f7daab677
2 changed files with 41 additions and 12 deletions
+36 -11
View File
@@ -1,24 +1,49 @@
var NamedMapMapConfigProvider = require('../models/mapconfig/named_map_provider');
var templateName = require('../backends/template_maps').templateName;
function NamedMapProviderCache(templateMaps, pgConnection, userLimitsApi, queryTablesApi) {
this.templateMaps = templateMaps;
this.pgConnection = pgConnection;
this.userLimitsApi = userLimitsApi;
this.queryTablesApi = queryTablesApi;
this.providerCache = {};
}
module.exports = NamedMapProviderCache;
NamedMapProviderCache.prototype.get = function(user, templateId, config, authToken, params) {
return new NamedMapMapConfigProvider(
this.templateMaps,
this.pgConnection,
this.userLimitsApi,
this.queryTablesApi,
user,
templateId,
config,
authToken,
params
);
var namedMapKey = createNamedMapKey(user, templateId);
if (!this.providerCache.hasOwnProperty(namedMapKey)) {
this.providerCache[namedMapKey] = {};
}
var providerKey = createProviderKey(config, authToken);
if (!this.providerCache[namedMapKey].hasOwnProperty(providerKey)) {
this.providerCache[namedMapKey][providerKey] = new NamedMapMapConfigProvider(
this.templateMaps,
this.pgConnection,
this.userLimitsApi,
this.queryTablesApi,
user,
templateId,
config,
authToken,
params
);
}
return this.providerCache[namedMapKey][providerKey];
};
NamedMapProviderCache.prototype.invalidate = function(user, templateId) {
delete this.providerCache[createNamedMapKey(user, templateId)];
};
function createNamedMapKey(user, templateId) {
return user + ':' + templateName(templateId);
}
function createProviderKey(config, authToken) {
return NamedMapMapConfigProvider.configHash(config) + ':' + authToken;
}
@@ -22,6 +22,8 @@ function NamedMapMapConfigProvider(templateMaps, pgConnection, userLimitsApi, qu
this.authToken = authToken;
this.params = params;
this.cacheBuster = Date.now();
// use template after call to mapConfig
this.template = null;
@@ -147,7 +149,7 @@ NamedMapMapConfigProvider.prototype.getKey = function() {
};
NamedMapMapConfigProvider.prototype.getCacheBuster = function() {
return 0;
return this.cacheBuster;
};
NamedMapMapConfigProvider.prototype.filter = function(key) {
@@ -182,6 +184,8 @@ function configHash(config) {
return crypto.createHash('md5').update(JSON.stringify(config)).digest('hex').substring(0,8);
}
module.exports.configHash = configHash;
NamedMapMapConfigProvider.prototype.setDBParams = function(cdbuser, params, callback) {
var self = this;
step(