From e919a54c8775911b58faec3405cf630f94e43c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Tue, 18 Oct 2022 17:18:08 +0100 Subject: [PATCH 1/3] feat: allow for caching of specific config versions fix: cached settings access --- lib/middlewares/populateSettings.js | 52 ++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/lib/middlewares/populateSettings.js b/lib/middlewares/populateSettings.js index ae4893bd..e91e0bab 100644 --- a/lib/middlewares/populateSettings.js +++ b/lib/middlewares/populateSettings.js @@ -55,30 +55,64 @@ const populateSettings = function (req, res, next) { } try { - const operatorSettings = settingsCache.get(operatorId) - if (!versionId && (!operatorSettings || !!needsSettingsReload[operatorId])) { + // Priority of configs to retrieve + // 1. Machine is in the middle of a transaction and has the config-version header set, fetch that config from cache or database, depending on whether it exists in cache + // 2. The operator settings changed, so we must update the cache + // 3. There's a cached config, send the cached value + // 4. There's no cached config, cache and send the latest config + + if (versionId) { + const cachedVersionedSettings = settingsCache.get(`${operatorId}-v${versionId}`) + + if (!cachedVersionedSettings) { + logger.debug('Fetching a specific config version cached value') + return newSettingsLoader.load(versionId) + .then(settings => { + settingsCache.set(`${operatorId}-v${versionId}`, settings) + req.settings = settings + }) + .then(() => next()) + .catch(next) + } + + logger.debug('Fetching and caching a specific config version') + req.settings = cachedVersionedSettings + return next() + } + + const operatorSettings = settingsCache.get(`${operatorId}-latest`) + + if (!versionId && !!needsSettingsReload[operatorId]) { + logger.debug('Fetching and caching a new latest config value, as a reload was requested') return newSettingsLoader.loadLatest() .then(settings => { - settingsCache.set(operatorId, settings) + settingsCache.set(`${operatorId}-latest`, settings) delete needsSettingsReload[operatorId] req.settings = settings }) .then(() => next()) .catch(next) } - + if (!versionId && operatorSettings) { + logger.debug('Fetching the latest config value from cache') req.settings = operatorSettings return next() } + + if (!versionId && !operatorSettings) { + logger.debug('Fetching the latest config version because there\'s no cached value') + return newSettingsLoader.loadLatest() + .then(settings => { + settingsCache.set(`${operatorId}-latest`, settings) + req.settings = settings + }) + .then(() => next()) + .catch(next) + } } catch (e) { logger.error(e) } - - newSettingsLoader.load(versionId) - .then(settings => { req.settings = settings }) - .then(() => next()) - .catch(next) } module.exports = populateSettings From 240203b0603be1e30ab45c11755febec9f50339f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Thu, 20 Oct 2022 18:06:47 +0100 Subject: [PATCH 2/3] fix: merge two conditionals --- lib/middlewares/populateSettings.js | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/lib/middlewares/populateSettings.js b/lib/middlewares/populateSettings.js index e91e0bab..9c4e6ec2 100644 --- a/lib/middlewares/populateSettings.js +++ b/lib/middlewares/populateSettings.js @@ -82,12 +82,15 @@ const populateSettings = function (req, res, next) { const operatorSettings = settingsCache.get(`${operatorId}-latest`) - if (!versionId && !!needsSettingsReload[operatorId]) { - logger.debug('Fetching and caching a new latest config value, as a reload was requested') + if (!versionId && (!!needsSettingsReload[operatorId] || !operatorSettings)) { + !!needsSettingsReload[operatorId] + ? logger.debug('Fetching and caching a new latest config value, as a reload was requested') + : logger.debug('Fetching the latest config version because there\'s no cached value') + return newSettingsLoader.loadLatest() .then(settings => { settingsCache.set(`${operatorId}-latest`, settings) - delete needsSettingsReload[operatorId] + if (!!needsSettingsReload[operatorId]) delete needsSettingsReload[operatorId] req.settings = settings }) .then(() => next()) @@ -99,17 +102,6 @@ const populateSettings = function (req, res, next) { req.settings = operatorSettings return next() } - - if (!versionId && !operatorSettings) { - logger.debug('Fetching the latest config version because there\'s no cached value') - return newSettingsLoader.loadLatest() - .then(settings => { - settingsCache.set(`${operatorId}-latest`, settings) - req.settings = settings - }) - .then(() => next()) - .catch(next) - } } catch (e) { logger.error(e) } From 38457bc9d9fc4c0d51985590fc20dd5edd4faa30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Fri, 28 Oct 2022 04:08:41 +0100 Subject: [PATCH 3/3] fix: unneeded conditional checks --- lib/middlewares/populateSettings.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/middlewares/populateSettings.js b/lib/middlewares/populateSettings.js index 9c4e6ec2..5f36fa69 100644 --- a/lib/middlewares/populateSettings.js +++ b/lib/middlewares/populateSettings.js @@ -82,7 +82,7 @@ const populateSettings = function (req, res, next) { const operatorSettings = settingsCache.get(`${operatorId}-latest`) - if (!versionId && (!!needsSettingsReload[operatorId] || !operatorSettings)) { + if (!!needsSettingsReload[operatorId] || !operatorSettings) { !!needsSettingsReload[operatorId] ? logger.debug('Fetching and caching a new latest config value, as a reload was requested') : logger.debug('Fetching the latest config version because there\'s no cached value') @@ -97,11 +97,9 @@ const populateSettings = function (req, res, next) { .catch(next) } - if (!versionId && operatorSettings) { - logger.debug('Fetching the latest config value from cache') - req.settings = operatorSettings - return next() - } + logger.debug('Fetching the latest config value from cache') + req.settings = operatorSettings + return next() } catch (e) { logger.error(e) }