diff --git a/config.json b/config.json index b41d7db..f07e1d1 100644 --- a/config.json +++ b/config.json @@ -4,6 +4,10 @@ "port": 3000, "timestamp": "DD.MM.YYYY HH:mm:ss:SS" }, + "cache": { + "active": true, + "lifetime": 15 + }, "log": { "level": "debug" }, @@ -11,7 +15,7 @@ "host": "192.168.1.24", "port": "12101", "address": "/midi" - }, + }, "modep": { "host": "192.168.1.24", "port": 80 diff --git a/libs/cache.js b/libs/cache.js index 47b02f5..2fc87b5 100644 --- a/libs/cache.js +++ b/libs/cache.js @@ -1,82 +1,134 @@ +const config = require('../config.json'); const logger = require('./logger.js'); +const constants = require('./constants.js'); -var banks = undefined; -var pedalboards = undefined; -var defaultPedalboard = undefined; -var currentPedalboard = undefined; -var currentPedals = undefined; +const active = config.cache.active; +const lifetime = config.cache.lifetime * 60000; + +const cache = new Map(); + +function isActive() { + return active; +} + +function getLifetime() { + return lifetime; +} + +function clear() { + cache = new Map(); +} + +function validLifetime(timestamp) { + if (timestamp == undefined) { + return false; + } + if ((new Date().getTime() - timestamp) >= lifetime) { + return false; + } + return true; +} + +function getValue(key) { + var value = cache.get(key); + if (value == undefined) { + return undefined; + } + if (!validLifetime(value.timestamp)) { + resetValue(key) + return undefined; + } + return cache.get(key).data; +} + +function setValue(key, value) { + if (!active) { + return; + } + logger.debug('caching \'' + key + '\'...'); + cache.set(key, { data: value, timestamp: new Date().getTime() }); +} + +function resetValue(key) { + cache.delete(key); +} function getBanks() { - return banks; + return getValue(constants.CACHE_BANKS); } function clearBanks() { - logger.debug('clearing bank(s)'); - setBanks(undefined); + resetValue(constants.CACHE_BANKS); } function setBanks(value) { - logger.debug('caching ' + value.length + ' bank(s)'); - banks = value; + setValue(constants.CACHE_BANKS, value); } function getPedalboards() { - return pedalboards; + return getValue(constants.CACHE_PEDALBOARDS); } function clearPedalboards() { - logger.debug('clearing pedalboard(s)'); - setPedalboards(undefined); + resetValue(constants.CACHE_PEDALBOARDS); } function setPedalboards(value) { - logger.debug('caching ' + value.length + ' pedalboard(s)'); - pedalboards = value; -} - -function getDefaultPedalboard() { - return defaultPedalboard; -} - -function clearDefaultPedalboard() { - logger.debug('clearing default pedalboard'); - setDefaultPedalboard(undefined); -} - -function setDefaultPedalboard(value) { - logger.debug('caching default pedalboard'); - defaultPedalboard = value; + setValue(constants.CACHE_PEDALBOARDS, value); } function getCurrentPedalboard() { - return currentPedalboard; + return getValue(constants.CACHE_PEDALBOARD_CURRENT); } function clearCurrentPedalboard() { - logger.debug('clearing current pedalboard'); - setCurrentPedalboard(undefined); + resetValue(constants.CACHE_PEDALBOARD_CURRENT); } function setCurrentPedalboard(value) { - logger.debug('caching current pedalboard'); - currentPedalboard = value; + setValue(constants.CACHE_PEDALBOARD_CURRENT, value); +} + +function getDefaultPedalboard() { + return getValue(constants.CACHE_PEDALBOARD_DEFAULT); +} + +function clearDefaultPedalboard() { + resetValue(constants.CACHE_PEDALBOARD_DEFAULT); +} + +function setDefaultPedalboard(value) { + setValue(constants.CACHE_PEDALBOARD_DEFAULT, value); } function getCurrentPedals() { - return currentPedals; + return getValue(constants.CACHE_PEDALS); } -function clearCurrenPedals() { - logger.debug('clearing current pedal(s))'); - setCurrentPedals(undefined); +function clearCurrentPedals() { + resetValue(constants.CACHE_PEDALS); } function setCurrentPedals(value) { - logger.debug('caching ' + value.length + ' current pedal(s)'); - currentPedals = value; + setValue(constants.CACHE_PEDALS, value); +} + +function getInfo() { + return getValue(constants.CACHE_INFO); +} + +function clearInfo() { + resetValue(constants.CACHE_INFO); +} + +function setInfo(value) { + setValue(constants.CACHE_INFO, value); } module.exports = { + isActive, + getLifetime, + clear, getBanks, clearBanks, setBanks, @@ -90,6 +142,9 @@ module.exports = { clearCurrentPedalboard, setCurrentPedalboard, getCurrentPedals, - clearCurrenPedals, - setCurrentPedals + clearCurrentPedals, + setCurrentPedals, + getInfo, + clearInfo, + setInfo } \ No newline at end of file diff --git a/libs/constants.js b/libs/constants.js index 7606088..b008d89 100644 --- a/libs/constants.js +++ b/libs/constants.js @@ -8,4 +8,11 @@ exports.API_PEDALBOARDS_DEFAULT = '/pedalboards/default'; exports.API_PEDALBOARDS_CURRENT = '/pedalboards/current'; exports.API_PEDALS = '/pedals'; +exports.CACHE_INFO = "info"; +exports.CACHE_BANKS = "banks"; +exports.CACHE_PEDALBOARDS = "pedalboards"; +exports.CACHE_PEDALBOARD_DEFAULT = "pedalboard_default"; +exports.CACHE_PEDALBOARD_CURRENT = "pedalboard_current"; +exports.CACHE_PEDALS = "pedals"; + exports.PEDALBOARD_DEFAULT = '/var/modep/pedalboards/default.pedalboard'; \ No newline at end of file diff --git a/libs/info.js b/libs/info.js index 85be06c..9809bfc 100644 --- a/libs/info.js +++ b/libs/info.js @@ -1,4 +1,5 @@ const logger = require('./logger.js'); +const cache = require('./cache'); const os = require('os'); function getHostname() { @@ -52,7 +53,11 @@ function getMemoryInfo() { } function getHostInfo() { - return { + var info = cache.getInfo(); + if (info != undefined) { + return info; + } + info = { hostname: getHostname(), uptime: getUptime(), load: getLoadAverage(), @@ -64,6 +69,8 @@ function getHostInfo() { cpu: getCpuInfo(), memory: getMemoryInfo() } + cache.setInfo(info); + return info; } module.exports = { diff --git a/libs/modep.js b/libs/modep.js index c31422c..12e17e4 100644 --- a/libs/modep.js +++ b/libs/modep.js @@ -22,6 +22,7 @@ function getBanks() { if (banks != undefined) { return resolve(banks); } + // FAKE DATA // var fake = [{ "title": "The Button", "pedalboards": [{ "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/default.pedalboard/default.ttl", "bundle": "/var/modep/pedalboards/default.pedalboard", "title": "Default", "version": 0 }, { "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/FUZZ.pedalboard/FUZZ.ttl", "bundle": "/var/modep/pedalboards/FUZZ.pedalboard", "title": "FUZZ", "version": 1 }] }]; // for (var index = 0; index < fake.length; index++) {