From 0f33484bd2b4b2bdfa23bf022a7a2da31f049052 Mon Sep 17 00:00:00 2001 From: SpecialX <47072643+wangxiner55@users.noreply.github.com> Date: Sun, 5 Jul 2026 22:27:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(questions):=20=E5=AE=8C=E6=88=90=20question?= =?UTF-8?q?s/data-access.ts=20=E9=81=97=E6=BC=8F=E7=9A=84=20cacheFn=20?= =?UTF-8?q?=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/questions/data-access.ts | 75 ++++++++++++++++------------ 1 file changed, 44 insertions(+), 31 deletions(-) diff --git a/src/modules/questions/data-access.ts b/src/modules/questions/data-access.ts index 1cb6710..bcb870e 100644 --- a/src/modules/questions/data-access.ts +++ b/src/modules/questions/data-access.ts @@ -492,26 +492,34 @@ export type QuestionContentForErrorCollection = { type: string } -export const getQuestionsContentForErrorCollection = cache( - async (questionIds: string[]): Promise> => { - const result = new Map() - const uniqueIds = Array.from(new Set(questionIds.filter((v): v is string => typeof v === "string" && v.length > 0))) - if (uniqueIds.length === 0) return result +export const getQuestionsContentForErrorCollectionRaw = async ( + questionIds: string[], +): Promise> => { + const result = new Map() + const uniqueIds = Array.from(new Set(questionIds.filter((v): v is string => typeof v === "string" && v.length > 0))) + if (uniqueIds.length === 0) return result - const rows = await db - .select({ - id: questions.id, - content: questions.content, - type: questions.type, - }) - .from(questions) - .where(inArray(questions.id, uniqueIds)) + const rows = await db + .select({ + id: questions.id, + content: questions.content, + type: questions.type, + }) + .from(questions) + .where(inArray(questions.id, uniqueIds)) - for (const r of rows) { - result.set(r.id, { content: r.content, type: r.type }) - } - return result + for (const r of rows) { + result.set(r.id, { content: r.content, type: r.type }) } + return result +} +export const getQuestionsContentForErrorCollection = cacheFn( + getQuestionsContentForErrorCollectionRaw, + { + tags: ["questions"], + ttl: 300, + keyParts: ["questions", "getQuestionsContentForErrorCollection"], + }, ) /** @@ -522,23 +530,28 @@ export const getQuestionsContentForErrorCollection = cache( * * 返回 Map,未找到的 ID 不会出现在 Map 中。 */ -export const getQuestionTypeMapByIds = cache( - async (questionIds: string[]): Promise> => { - const result = new Map() - const uniqueIds = Array.from(new Set(questionIds.filter((v): v is string => typeof v === "string" && v.length > 0))) - if (uniqueIds.length === 0) return result +export const getQuestionTypeMapByIdsRaw = async ( + questionIds: string[], +): Promise> => { + const result = new Map() + const uniqueIds = Array.from(new Set(questionIds.filter((v): v is string => typeof v === "string" && v.length > 0))) + if (uniqueIds.length === 0) return result - const rows = await db - .select({ id: questions.id, type: questions.type }) - .from(questions) - .where(inArray(questions.id, uniqueIds)) + const rows = await db + .select({ id: questions.id, type: questions.type }) + .from(questions) + .where(inArray(questions.id, uniqueIds)) - for (const r of rows) { - result.set(r.id, r.type) - } - return result + for (const r of rows) { + result.set(r.id, r.type) } -) + return result +} +export const getQuestionTypeMapByIds = cacheFn(getQuestionTypeMapByIdsRaw, { + tags: ["questions"], + ttl: 300, + keyParts: ["questions", "getQuestionTypeMapByIds"], +}) // --------------------------------------------------------------------------- // 导入/导出接口