Chore: make populateSettings middleware use node-cache
This commit is contained in:
parent
fb4267b0d5
commit
7657da5e16
5 changed files with 29 additions and 45 deletions
|
|
@ -1,9 +1,8 @@
|
|||
const state = require('./state')
|
||||
const settingsCache = require('./settingsCache')
|
||||
const newSettingsLoader = require('../new-settings-loader')
|
||||
const helpers = require('../route-helpers')
|
||||
|
||||
const SETTINGS_CACHE_REFRESH = 60 * 60 * 1000
|
||||
const { settingsCache } = state
|
||||
|
||||
const populateSettings = function (req, res, next) {
|
||||
const operatorId = res.locals.operatorId
|
||||
|
|
@ -13,21 +12,16 @@ const populateSettings = function (req, res, next) {
|
|||
}
|
||||
|
||||
try {
|
||||
// Clear cache every hour
|
||||
if (Date.now() - settingsCache.getTimestamp(operatorId) > SETTINGS_CACHE_REFRESH) {
|
||||
settingsCache.clear(operatorId)
|
||||
}
|
||||
|
||||
if (!versionId && settingsCache.getCache(operatorId)) {
|
||||
req.settings = settingsCache.getCache(operatorId)
|
||||
const operatorSettings = settingsCache.get(operatorId)
|
||||
if (!versionId && operatorSettings) {
|
||||
req.settings = operatorSettings
|
||||
return next()
|
||||
}
|
||||
|
||||
if (!versionId && !settingsCache.getCache(operatorId)) {
|
||||
if (!versionId && !operatorSettings) {
|
||||
return newSettingsLoader.loadLatest()
|
||||
.then(settings => {
|
||||
settingsCache.setCache(operatorId, settings)
|
||||
settingsCache.setTimestamp(operatorId, Date.now())
|
||||
settingsCache.set(operatorId, settings)
|
||||
req.settings = settings
|
||||
})
|
||||
.then(() => next())
|
||||
|
|
|
|||
|
|
@ -1,32 +0,0 @@
|
|||
const _ = require('lodash/fp')
|
||||
const state = require('./state')
|
||||
|
||||
const getTimestamp = (operatorId) => {
|
||||
if (!operatorId) throw new Error('operatorId must not be nil or empty')
|
||||
return state.settingsCache[operatorId] ? state.settingsCache[operatorId].timestamp : null
|
||||
}
|
||||
|
||||
const getCache = (operatorId) => {
|
||||
if (!operatorId) throw new Error('operatorId must not be nil or empty')
|
||||
return state.settingsCache[operatorId] ? state.settingsCache[operatorId].cache : null
|
||||
}
|
||||
|
||||
const setTimestamp = (operatorId, newTimestamp) => {
|
||||
state.settingsCache = _.set([operatorId, 'timestamp'], newTimestamp, state.settingsCache)
|
||||
}
|
||||
|
||||
const setCache = (operatorId, newCache) => {
|
||||
state.settingsCache = _.set([operatorId, 'cache'], newCache, state.settingsCache)
|
||||
}
|
||||
|
||||
const clear = (operatorId) => {
|
||||
state.settingsCache = _.set([operatorId], null, state.settingsCache)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getTimestamp,
|
||||
getCache,
|
||||
setTimestamp,
|
||||
setCache,
|
||||
clear
|
||||
}
|
||||
|
|
@ -1,7 +1,13 @@
|
|||
const NodeCache = require('node-cache')
|
||||
const SETTINGS_CACHE_REFRESH = 3600
|
||||
|
||||
module.exports = (function () {
|
||||
return {
|
||||
oldVersionId: 'unset',
|
||||
settingsCache: {},
|
||||
settingsCache: new NodeCache({
|
||||
stdTTL: SETTINGS_CACHE_REFRESH,
|
||||
checkperiod: SETTINGS_CACHE_REFRESH // Clear cache every hour
|
||||
}),
|
||||
canLogClockSkewMap: {},
|
||||
canGetLastSeenMap: {},
|
||||
pids: {},
|
||||
|
|
|
|||
15
package-lock.json
generated
15
package-lock.json
generated
|
|
@ -12475,6 +12475,21 @@
|
|||
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.2.tgz",
|
||||
"integrity": "sha512-Ntyt4AIXyaLIuMHF6IOoTakB3K+RWxwtsHNRxllEoA6vPwP9o4866g6YWDLUdnucilZhmkxiHwHr11gAENw+QA=="
|
||||
},
|
||||
"node-cache": {
|
||||
"version": "5.1.2",
|
||||
"resolved": "https://registry.npmjs.org/node-cache/-/node-cache-5.1.2.tgz",
|
||||
"integrity": "sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==",
|
||||
"requires": {
|
||||
"clone": "2.x"
|
||||
},
|
||||
"dependencies": {
|
||||
"clone": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz",
|
||||
"integrity": "sha1-G39Ln1kfHo+DZwQBYANFoCiHQ18="
|
||||
}
|
||||
}
|
||||
},
|
||||
"node-expat": {
|
||||
"version": "2.3.18",
|
||||
"resolved": "https://registry.npmjs.org/node-expat/-/node-expat-2.3.18.tgz",
|
||||
|
|
|
|||
|
|
@ -60,6 +60,7 @@
|
|||
"nano-markdown": "^1.2.0",
|
||||
"ndjson": "^1.5.0",
|
||||
"nocache": "^2.1.0",
|
||||
"node-cache": "^5.1.2",
|
||||
"numeral": "^2.0.3",
|
||||
"otplib": "^12.0.1",
|
||||
"p-each-series": "^1.0.0",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue