Cache mods on controller

This commit is contained in:
Danielv123 committed 2025-04-08 22:42:03 +02:00
1 parent 600a5d5ce1
commit d50230becc
2 files changed
+17 -2

No files matched your search

+9 -1
View File
@@ -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) {
+8 -1
View File
@@ -96,6 +96,13 @@ export default class Controller {
clusterLogIndex: lib.LogIndex | null = null;
clusterLogBuildInterval: ReturnType<typeof setInterval> | null = null;
// Cache for mod portal requests
static MOD_PORTAL_CACHE_DURATION = 30 * 60 * 1000; // 30 minutes
modPortalCache = new Map<string, { timestamp: number, data: any[] }>();
/** @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"
);