diff --git a/packages/controller/src/ControlConnection.ts b/packages/controller/src/ControlConnection.ts index 0ce1d46c..a1c661cc 100644 --- a/packages/controller/src/ControlConnection.ts +++ b/packages/controller/src/ControlConnection.ts @@ -515,17 +515,25 @@ export default class ControlConnection extends BaseConnection { }; } - /** * Handle request to fetch all mods from the Factorio Mod Portal for a given version. * @param request - The request object containing factorioVersion. */ async handleModPortalGetAllRequest(request: lib.ModPortalGetAllRequest) { + const cacheKey = `${request.factorioVersion}-${request.hide_deprecated}`; + const cachedData = this._controller.modPortalCache.get(cacheKey); + + if (cachedData && Date.now() - cachedData.timestamp < Controller.MOD_PORTAL_CACHE_DURATION) { + return new lib.ModPortalGetAllRequest.Response(cachedData.data); + } + try { + logger.info(`Fetching mod portal data for ${cacheKey}`); const mods = await lib.ModStore.fetchAllModsFromPortal( request.factorioVersion, request.hide_deprecated ); + this._controller.modPortalCache.set(cacheKey, { timestamp: Date.now(), data: mods }); // The Response class is defined inline within the Request class return new lib.ModPortalGetAllRequest.Response(mods); } catch (error: any) { diff --git a/packages/controller/src/Controller.ts b/packages/controller/src/Controller.ts index 1517677a..08d2e6d8 100644 --- a/packages/controller/src/Controller.ts +++ b/packages/controller/src/Controller.ts @@ -96,6 +96,13 @@ export default class Controller { clusterLogIndex: lib.LogIndex | null = null; clusterLogBuildInterval: ReturnType | null = null; + // Cache for mod portal requests + static MOD_PORTAL_CACHE_DURATION = 30 * 60 * 1000; // 30 minutes + modPortalCache = new Map(); + + /** @type {?module:lib.ProcessConnector} */ + process = null; + static async bootstrap(config: lib.ControllerConfig) { let databaseDirectory = config.get("controller.database_directory"); await fs.ensureDir(databaseDirectory); @@ -288,7 +295,7 @@ export default class Controller { let tls_cert = this.config.get("controller.tls_certificate"); let tls_key = this.config.get("controller.tls_private_key"); - if (httpsPort && (!tls_cert || !tls_key)) { + if (httpsPort && (!tls_cert || !tls_key)) { throw new lib.StartupError( "tls_certificate and tls_private_key must be configure in order to use https_port" );