2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-09-14 00:11:47 +08:00
|
|
|
const LruCache = require('lru-cache');
|
2015-07-15 22:51:26 +08:00
|
|
|
|
2019-09-14 00:03:39 +08:00
|
|
|
const NamedMapMapConfigProvider = require('../models/mapconfig/provider/named-map-provider');
|
2019-10-07 16:50:14 +08:00
|
|
|
const { templateName } = require('../backends/template-maps');
|
2019-09-14 00:03:39 +08:00
|
|
|
|
2019-09-13 00:02:13 +08:00
|
|
|
const TEN_MINUTES_IN_MILLISECONDS = 1000 * 60 * 10;
|
2019-09-13 23:42:56 +08:00
|
|
|
const ACTIONS = ['update', 'delete'];
|
2019-09-13 00:02:13 +08:00
|
|
|
|
2019-09-14 00:07:50 +08:00
|
|
|
module.exports = class NamedMapProviderCache {
|
|
|
|
constructor (
|
|
|
|
templateMaps,
|
|
|
|
pgConnection,
|
|
|
|
metadataBackend,
|
|
|
|
userLimitsBackend,
|
|
|
|
mapConfigAdapter,
|
|
|
|
affectedTablesCache
|
|
|
|
) {
|
|
|
|
this.templateMaps = templateMaps;
|
|
|
|
this.pgConnection = pgConnection;
|
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.userLimitsBackend = userLimitsBackend;
|
|
|
|
this.mapConfigAdapter = mapConfigAdapter;
|
|
|
|
this.affectedTablesCache = affectedTablesCache;
|
|
|
|
|
|
|
|
this.providerCache = new LruCache({ max: 2000, maxAge: TEN_MINUTES_IN_MILLISECONDS });
|
|
|
|
|
2019-09-14 00:15:11 +08:00
|
|
|
ACTIONS.forEach(action => templateMaps.on(action, (user, templateId) => this.invalidate(user, templateId)));
|
2019-09-14 00:07:50 +08:00
|
|
|
}
|
2015-07-15 02:53:06 +08:00
|
|
|
|
2019-09-14 00:07:50 +08:00
|
|
|
get (user, templateId, config, authToken, params, callback) {
|
|
|
|
const namedMapKey = createNamedMapKey(user, templateId);
|
|
|
|
const namedMapProviders = this.providerCache.get(namedMapKey) || {};
|
|
|
|
const providerKey = createProviderKey(config, authToken, params);
|
|
|
|
|
|
|
|
if (namedMapProviders.hasOwnProperty(providerKey)) {
|
|
|
|
return callback(null, namedMapProviders[providerKey]);
|
|
|
|
}
|
|
|
|
|
|
|
|
namedMapProviders[providerKey] = new NamedMapMapConfigProvider(
|
|
|
|
this.templateMaps,
|
|
|
|
this.pgConnection,
|
|
|
|
this.metadataBackend,
|
|
|
|
this.userLimitsBackend,
|
|
|
|
this.mapConfigAdapter,
|
|
|
|
this.affectedTablesCache,
|
|
|
|
user,
|
|
|
|
templateId,
|
|
|
|
config,
|
|
|
|
authToken,
|
|
|
|
params
|
|
|
|
);
|
|
|
|
|
|
|
|
this.providerCache.set(namedMapKey, namedMapProviders);
|
2015-09-23 22:45:20 +08:00
|
|
|
|
|
|
|
return callback(null, namedMapProviders[providerKey]);
|
2015-07-15 03:16:49 +08:00
|
|
|
}
|
|
|
|
|
2019-09-14 00:07:50 +08:00
|
|
|
invalidate (user, templateId) {
|
|
|
|
this.providerCache.del(createNamedMapKey(user, templateId));
|
|
|
|
}
|
2015-07-15 02:53:06 +08:00
|
|
|
};
|
2015-07-15 03:16:49 +08:00
|
|
|
|
2019-09-14 00:03:39 +08:00
|
|
|
function createNamedMapKey (user, templateId) {
|
|
|
|
return `${user}:${templateName(templateId)}`;
|
2015-07-15 03:16:49 +08:00
|
|
|
}
|
|
|
|
|
2019-09-14 00:00:13 +08:00
|
|
|
const providerKeyTpl = ctx => `${ctx.authToken}:${ctx.configHash}:${ctx.format}:${ctx.layer}:${ctx.scale_factor}`;
|
2015-07-31 18:24:34 +08:00
|
|
|
|
2019-09-14 00:03:39 +08:00
|
|
|
function createProviderKey (config, authToken, params) {
|
2019-09-13 23:58:23 +08:00
|
|
|
const defaults = {
|
2015-07-31 18:24:34 +08:00
|
|
|
authToken: authToken || '',
|
|
|
|
configHash: NamedMapMapConfigProvider.configHash(config),
|
|
|
|
layer: '',
|
|
|
|
format: '',
|
|
|
|
scale_factor: 1
|
2019-09-13 23:58:23 +08:00
|
|
|
};
|
|
|
|
const ctx = Object.assign({}, defaults, params);
|
|
|
|
|
|
|
|
return providerKeyTpl(ctx);
|
2015-07-15 03:16:49 +08:00
|
|
|
}
|