feat: allow for caching of specific config versions
fix: cached settings access
This commit is contained in:
parent
c003cc81f8
commit
e919a54c87
1 changed files with 43 additions and 9 deletions
|
|
@ -55,30 +55,64 @@ const populateSettings = function (req, res, next) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const operatorSettings = settingsCache.get(operatorId)
|
// Priority of configs to retrieve
|
||||||
if (!versionId && (!operatorSettings || !!needsSettingsReload[operatorId])) {
|
// 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()
|
return newSettingsLoader.loadLatest()
|
||||||
.then(settings => {
|
.then(settings => {
|
||||||
settingsCache.set(operatorId, settings)
|
settingsCache.set(`${operatorId}-latest`, settings)
|
||||||
delete needsSettingsReload[operatorId]
|
delete needsSettingsReload[operatorId]
|
||||||
req.settings = settings
|
req.settings = settings
|
||||||
})
|
})
|
||||||
.then(() => next())
|
.then(() => next())
|
||||||
.catch(next)
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!versionId && operatorSettings) {
|
if (!versionId && operatorSettings) {
|
||||||
|
logger.debug('Fetching the latest config value from cache')
|
||||||
req.settings = operatorSettings
|
req.settings = operatorSettings
|
||||||
return next()
|
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) {
|
} catch (e) {
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
newSettingsLoader.load(versionId)
|
|
||||||
.then(settings => { req.settings = settings })
|
|
||||||
.then(() => next())
|
|
||||||
.catch(next)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = populateSettings
|
module.exports = populateSettings
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue