From 6a7042c0aab9045bb1e06fa2b5c5ebcd5f7dd5d1 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 28 Apr 2018 14:16:34 +0200 Subject: [PATCH] Improved performance of /api/getFactorioLocale with caching --- lib/getFactorioLocale.js | 26 +++++++++++++++++--------- master.js | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/getFactorioLocale.js b/lib/getFactorioLocale.js index 7a28d58c..72d84dd0 100644 --- a/lib/getFactorioLocale.js +++ b/lib/getFactorioLocale.js @@ -6,6 +6,8 @@ Module for grabbing factorio locale from a local factorio install const fs = require("fs"); const ini = require("ini"); +const cache = {}; + /** Gets raw factorio locale from the .ini file. Not available outside of module through exports. @@ -32,14 +34,20 @@ Gets factorios locale as an object, does not respect mods. */ module.exports.asObject = function getLocaleAsObject(factorioDirectory, languageCode, callback){ if(typeof factorioDirectory != "string" || typeof languageCode != "string" || typeof callback != "function"){ - throw "Error: wrong parameters provided"; + throw new Error("Error: wrong parameters provided"); + } + + if(!cache[languageCode]){ + getLocale(factorioDirectory, languageCode, (err, rawLocale) => { + if(err){ + callback(err); + } else { + let processedLocale = ini.parse(rawLocale); + cache[languageCode] = processedLocale; + callback(undefined, processedLocale); + } + }); + } else { + callback(undefined, cache[languageCode]); } - getLocale(factorioDirectory, languageCode, (err, rawLocale) => { - if(err){ - callback(err); - } else { - let processedLocale = ini.parse(rawLocale) - callback(undefined, processedLocale); - } - }); } diff --git a/master.js b/master.js index a36d6429..fa47c882 100644 --- a/master.js +++ b/master.js @@ -782,7 +782,7 @@ Requires x-access-token header to be set. Find you api token in secret-api-token @memberof clusterioMaster @instance @alias api/runCommand -@param {object} JSON {instanceID:19412312, command:"/c game.print('hello')"} +@param {object} JSON {instanceID:19412312, broadcast:false, command:"/c game.print('hello')"} @returns {object} Status {auth: bool, message: "Informative error", data:{}} */ app.post("/api/runCommand", (req,res) => {