extended / optimized caching

This commit is contained in:
Daniel Sommer 2022-02-10 23:20:55 +01:00
parent ce81de43e6
commit c2793c7e02
5 changed files with 118 additions and 44 deletions

View file

@ -4,6 +4,10 @@
"port": 3000,
"timestamp": "DD.MM.YYYY HH:mm:ss:SS"
},
"cache": {
"active": true,
"lifetime": 15
},
"log": {
"level": "debug"
},

View file

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

View file

@ -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';

View file

@ -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 = {

View file

@ -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++) {