2015-07-31 18:24:34 +08:00
|
|
|
var _ = require('underscore');
|
|
|
|
var dot = require('dot');
|
2016-05-23 19:32:28 +08:00
|
|
|
var NamedMapMapConfigProvider = require('../models/mapconfig/provider/named-map-provider');
|
2015-07-15 03:16:49 +08:00
|
|
|
var templateName = require('../backends/template_maps').templateName;
|
2015-09-23 22:45:20 +08:00
|
|
|
var queue = require('queue-async');
|
2015-07-15 02:53:06 +08:00
|
|
|
|
2015-07-15 22:51:26 +08:00
|
|
|
var LruCache = require("lru-cache");
|
|
|
|
|
2016-05-24 05:29:06 +08:00
|
|
|
function NamedMapProviderCache(templateMaps, pgConnection, metadataBackend, userLimitsApi, mapConfigAdapter) {
|
2015-07-15 02:53:06 +08:00
|
|
|
this.templateMaps = templateMaps;
|
|
|
|
this.pgConnection = pgConnection;
|
2016-04-07 22:18:48 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
2015-07-15 02:53:06 +08:00
|
|
|
this.userLimitsApi = userLimitsApi;
|
2016-05-24 05:29:06 +08:00
|
|
|
this.mapConfigAdapter = mapConfigAdapter;
|
2015-09-24 00:44:11 +08:00
|
|
|
|
2015-07-15 22:51:26 +08:00
|
|
|
this.providerCache = new LruCache({ max: 2000 });
|
2015-07-15 02:53:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = NamedMapProviderCache;
|
|
|
|
|
2015-09-23 20:32:26 +08:00
|
|
|
NamedMapProviderCache.prototype.get = function(user, templateId, config, authToken, params, callback) {
|
2015-07-15 03:16:49 +08:00
|
|
|
var namedMapKey = createNamedMapKey(user, templateId);
|
2015-07-15 22:51:26 +08:00
|
|
|
var namedMapProviders = this.providerCache.get(namedMapKey) || {};
|
2015-07-15 03:16:49 +08:00
|
|
|
|
2015-07-31 18:24:34 +08:00
|
|
|
var providerKey = createProviderKey(config, authToken, params);
|
2015-07-15 22:51:26 +08:00
|
|
|
if (!namedMapProviders.hasOwnProperty(providerKey)) {
|
|
|
|
namedMapProviders[providerKey] = new NamedMapMapConfigProvider(
|
2015-07-15 03:16:49 +08:00
|
|
|
this.templateMaps,
|
|
|
|
this.pgConnection,
|
2016-04-07 22:18:48 +08:00
|
|
|
this.metadataBackend,
|
2015-07-15 03:16:49 +08:00
|
|
|
this.userLimitsApi,
|
2016-05-24 05:29:06 +08:00
|
|
|
this.mapConfigAdapter,
|
2015-07-15 03:16:49 +08:00
|
|
|
user,
|
|
|
|
templateId,
|
|
|
|
config,
|
|
|
|
authToken,
|
|
|
|
params
|
|
|
|
);
|
2015-07-15 22:51:26 +08:00
|
|
|
this.providerCache.set(namedMapKey, namedMapProviders);
|
2015-09-23 22:45:20 +08:00
|
|
|
|
|
|
|
// early exit, if provider did not exist we just return it
|
|
|
|
return callback(null, namedMapProviders[providerKey]);
|
2015-07-15 03:16:49 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 22:45:20 +08:00
|
|
|
var namedMapProvider = namedMapProviders[providerKey];
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
queue(2)
|
|
|
|
.defer(namedMapProvider.getTemplate.bind(namedMapProvider))
|
|
|
|
.defer(this.templateMaps.getTemplate.bind(this.templateMaps), user, templateId)
|
|
|
|
.awaitAll(function templatesQueueDone(err, results) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to reset provider its template has changed
|
|
|
|
// Ideally this should be done in a passive mode where this cache gets notified of template changes
|
|
|
|
var uniqueFingerprints = _.uniq(results.map(self.templateMaps.fingerPrint)).length;
|
|
|
|
if (uniqueFingerprints > 1) {
|
|
|
|
namedMapProvider.reset();
|
|
|
|
}
|
|
|
|
return callback(null, namedMapProvider);
|
|
|
|
});
|
2015-07-15 03:16:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NamedMapProviderCache.prototype.invalidate = function(user, templateId) {
|
2015-07-15 22:51:26 +08:00
|
|
|
this.providerCache.del(createNamedMapKey(user, templateId));
|
2015-07-15 02:53:06 +08:00
|
|
|
};
|
2015-07-15 03:16:49 +08:00
|
|
|
|
|
|
|
function createNamedMapKey(user, templateId) {
|
|
|
|
return user + ':' + templateName(templateId);
|
|
|
|
}
|
|
|
|
|
2015-07-31 18:24:34 +08:00
|
|
|
var providerKey = '{{=it.authToken}}:{{=it.configHash}}:{{=it.format}}:{{=it.layer}}:{{=it.scale_factor}}';
|
|
|
|
var providerKeyTpl = dot.template(providerKey);
|
|
|
|
|
|
|
|
function createProviderKey(config, authToken, params) {
|
|
|
|
var tplValues = _.defaults({}, params, {
|
|
|
|
authToken: authToken || '',
|
|
|
|
configHash: NamedMapMapConfigProvider.configHash(config),
|
|
|
|
layer: '',
|
|
|
|
format: '',
|
|
|
|
scale_factor: 1
|
|
|
|
});
|
|
|
|
return providerKeyTpl(tplValues);
|
2015-07-15 03:16:49 +08:00
|
|
|
}
|