diff --git a/lib/middlewares/populateSettings.js b/lib/middlewares/populateSettings.js index 08363675..041a4f3a 100644 --- a/lib/middlewares/populateSettings.js +++ b/lib/middlewares/populateSettings.js @@ -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()) diff --git a/lib/middlewares/settingsCache.js b/lib/middlewares/settingsCache.js deleted file mode 100644 index 2677aa68..00000000 --- a/lib/middlewares/settingsCache.js +++ /dev/null @@ -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 -} diff --git a/lib/middlewares/state.js b/lib/middlewares/state.js index 95b7eb5d..10267d88 100644 --- a/lib/middlewares/state.js +++ b/lib/middlewares/state.js @@ -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: {}, diff --git a/package-lock.json b/package-lock.json index 6fa84eca..4e68a158 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index c1e7fe90..92b0afaa 100644 --- a/package.json +++ b/package.json @@ -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",