Chore: make populateSettings middleware use node-cache

This commit is contained in:
csrapr 2021-05-07 17:23:47 +01:00 committed by Josh Harvey
parent fb4267b0d5
commit 7657da5e16
5 changed files with 29 additions and 45 deletions

View file

@ -1,9 +1,8 @@
const state = require('./state') const state = require('./state')
const settingsCache = require('./settingsCache')
const newSettingsLoader = require('../new-settings-loader') const newSettingsLoader = require('../new-settings-loader')
const helpers = require('../route-helpers') const helpers = require('../route-helpers')
const SETTINGS_CACHE_REFRESH = 60 * 60 * 1000 const { settingsCache } = state
const populateSettings = function (req, res, next) { const populateSettings = function (req, res, next) {
const operatorId = res.locals.operatorId const operatorId = res.locals.operatorId
@ -13,21 +12,16 @@ const populateSettings = function (req, res, next) {
} }
try { try {
// Clear cache every hour const operatorSettings = settingsCache.get(operatorId)
if (Date.now() - settingsCache.getTimestamp(operatorId) > SETTINGS_CACHE_REFRESH) { if (!versionId && operatorSettings) {
settingsCache.clear(operatorId) req.settings = operatorSettings
}
if (!versionId && settingsCache.getCache(operatorId)) {
req.settings = settingsCache.getCache(operatorId)
return next() return next()
} }
if (!versionId && !settingsCache.getCache(operatorId)) { if (!versionId && !operatorSettings) {
return newSettingsLoader.loadLatest() return newSettingsLoader.loadLatest()
.then(settings => { .then(settings => {
settingsCache.setCache(operatorId, settings) settingsCache.set(operatorId, settings)
settingsCache.setTimestamp(operatorId, Date.now())
req.settings = settings req.settings = settings
}) })
.then(() => next()) .then(() => next())

View file

@ -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
}

View file

@ -1,7 +1,13 @@
const NodeCache = require('node-cache')
const SETTINGS_CACHE_REFRESH = 3600
module.exports = (function () { module.exports = (function () {
return { return {
oldVersionId: 'unset', oldVersionId: 'unset',
settingsCache: {}, settingsCache: new NodeCache({
stdTTL: SETTINGS_CACHE_REFRESH,
checkperiod: SETTINGS_CACHE_REFRESH // Clear cache every hour
}),
canLogClockSkewMap: {}, canLogClockSkewMap: {},
canGetLastSeenMap: {}, canGetLastSeenMap: {},
pids: {}, pids: {},

15
package-lock.json generated
View file

@ -12475,6 +12475,21 @@
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.2.tgz", "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.2.tgz",
"integrity": "sha512-Ntyt4AIXyaLIuMHF6IOoTakB3K+RWxwtsHNRxllEoA6vPwP9o4866g6YWDLUdnucilZhmkxiHwHr11gAENw+QA==" "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": { "node-expat": {
"version": "2.3.18", "version": "2.3.18",
"resolved": "https://registry.npmjs.org/node-expat/-/node-expat-2.3.18.tgz", "resolved": "https://registry.npmjs.org/node-expat/-/node-expat-2.3.18.tgz",

View file

@ -60,6 +60,7 @@
"nano-markdown": "^1.2.0", "nano-markdown": "^1.2.0",
"ndjson": "^1.5.0", "ndjson": "^1.5.0",
"nocache": "^2.1.0", "nocache": "^2.1.0",
"node-cache": "^5.1.2",
"numeral": "^2.0.3", "numeral": "^2.0.3",
"otplib": "^12.0.1", "otplib": "^12.0.1",
"p-each-series": "^1.0.0", "p-each-series": "^1.0.0",