Add TTL cache for game version/loader APIs and enrich upload metadata

This commit is contained in:
CPTProgrammer 2026-07-10 06:54:14 +08:00
parent 7ddbd36c63
commit e8d4994cec
No known key found for this signature in database
5 changed files with 73 additions and 33 deletions

View File

@ -5,31 +5,36 @@ import {
type GameVersionType,
type GameVersion,
} from "@/types";
import { createTtlCache } from "@/lib/utils/cache";
const TTL = 3_600_000; // 1 hour
/**
* CurseForge Game Version Type
* CurseForge Game Version Type 1h
* GET /api/game/version-types
*/
export async function getVersionTypes(
client: CurseForgeClient,
): Promise<GameVersionType[]> {
export const getVersionTypes = createTtlCache(
async (client: CurseForgeClient): Promise<GameVersionType[]> => {
const data = await client.uploadGet<unknown[]>(
"/api/game/version-types",
);
return GameVersionTypeSchema.array().parse(data);
}
},
TTL,
);
/**
* CurseForge Game Version
* CurseForge Game Version 1h
* Minecraft Mod LoaderEnvironmentJava
* gameVersionTypeID
* GET /api/game/versions
*/
export async function getGameVersions(
client: CurseForgeClient,
): Promise<GameVersion[]> {
export const getGameVersions = createTtlCache(
async (client: CurseForgeClient): Promise<GameVersion[]> => {
const data = await client.uploadGet<unknown[]>(
"/api/game/versions",
);
return GameVersionSchema.array().parse(data);
}
},
TTL,
);

View File

@ -5,25 +5,30 @@ import {
type LoaderTag,
type GameVersionTag,
} from "@/types";
import { createTtlCache } from "@/lib/utils/cache";
const TTL = 3_600_000; // 1 hour
/**
* Modrinth
* Modrinth 1h
* GET /tag/loader
*/
export async function getLoaders(
client: ModrinthClient,
): Promise<LoaderTag[]> {
export const getLoaders = createTtlCache(
async (client: ModrinthClient): Promise<LoaderTag[]> => {
const data = await client.get<unknown[]>("/tag/loader");
return LoaderTagSchema.array().parse(data);
}
},
TTL,
);
/**
* Modrinth Minecraft
* Modrinth Minecraft 1h
* GET /tag/game_version
*/
export async function getGameVersions(
client: ModrinthClient,
): Promise<GameVersionTag[]> {
export const getGameVersions = createTtlCache(
async (client: ModrinthClient): Promise<GameVersionTag[]> => {
const data = await client.get<unknown[]>("/tag/game_version");
return GameVersionTagSchema.array().parse(data);
}
},
TTL,
);

16
src/lib/utils/cache.ts Normal file
View File

@ -0,0 +1,16 @@
export function createTtlCache<T>(
factory: (...args: any[]) => Promise<T>,
ttlMs: number,
keyFn?: (...args: any[]) => string,
): (...args: any[]) => Promise<T> {
const cache = new Map<string, { data: T; ts: number }>();
return async (...args) => {
const key = keyFn ? keyFn(...args) : "_";
const entry = cache.get(key);
if (entry && Date.now() - entry.ts < ttlMs) return entry.data;
const data = await factory(...args);
cache.set(key, { data, ts: Date.now() });
return data;
};
}

View File

@ -1,5 +1,5 @@
import { curseforgeClient } from "@/services/clients";
import { uploadFile } from "@/lib/curseforge";
import { uploadFile, getGameVersions } from "@/lib/curseforge";
import { resolveVersionName, buildJarPath } from "@/lib/utils/format";
import { readJarBlob } from "@/lib/utils/file";
import type { UploadMetadata } from "@/types";
@ -26,11 +26,24 @@ export async function publishCurseForgeVersion(
const primaryBlob = readJarBlob(primaryPath);
const gameVersions = await getGameVersions(curseforgeClient);
const resolveId = (name: string): number => {
const found = gameVersions.find((v) => v.name === name);
if (!found) throw new Error(`Game version "${name}" not found in CurseForge`);
return found.id;
};
const metadata: UploadMetadata = {
changelog: input.changelog,
changelogType: "markdown",
displayName,
releaseType: input.versionType,
gameVersions: [
resolveId(mcVersion),
...config.curseforge.loaders.map(resolveId),
...config.curseforge.environment.map(resolveId),
],
relations: config.curseforge.relations,
};

View File

@ -47,6 +47,7 @@ export async function publishModrinthVersion(
project_id: config.modrinth.project_id,
name: versionName,
version_number: versionNumber,
changelog: input.changelog,
game_versions: [mcVersion],
version_type: input.versionType,
loaders: config.modrinth.loaders,