perf(排名): 一个班同时打开排名、班级对抗、按人数排的题目列表时只查一次库
这几样谁来看都一样,原来每次请求现算:全服全部时间的排名本机 16 核就要 250ms, 服务器两核还要判题。services/cache.ts 进程内缓存 10 秒,同时到达的相同请求等同一个结果: - 排名榜面的原始数据(「我」排第几、前后一名照旧每次现算):250ms → 6ms - 班级对抗 / 详情 / PK 共用的这学期数据:40ms → 1ms - 题目列表按做对、做过人数排序的全表人数:45ms → 20ms 40 个人同时打开全服排名(缓存是冷的)一共 342ms。代价是做对之后最多晚 10 秒上榜。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
307b0725d0
commit
fb11e86151
5 files changed
+66
-4
No files matched your search
@@ -36,6 +36,7 @@ import { JudgeStatus } from "../judge/status"
|
||||
import {
|
||||
buildProblemProgress,
|
||||
problemStates,
|
||||
allProblemUserCounts,
|
||||
problemUserCounts,
|
||||
} from "../services/problem-progress"
|
||||
import {
|
||||
@@ -180,7 +181,7 @@ problemRoutes.get("/problems", optionalAuth, async (c) => {
|
||||
// 旧的「画流程图 / 语法检查」排序已经拆成类型筛选,传进来落到默认(最新创建)
|
||||
const sort = problemListSortSchema.safeParse(c.req.query("sort") ?? "").data ?? ""
|
||||
const byPeople = sort.endsWith("submission_number") || sort.endsWith("accepted_number")
|
||||
const allCounts = byPeople ? await problemUserCounts() : null
|
||||
const allCounts = byPeople ? await allProblemUserCounts() : null
|
||||
const people = allCounts
|
||||
? sql`case ${schema.problem.id} ${sql.join(
|
||||
[...allCounts].map(
|
||||
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
loadEntrants,
|
||||
PK_MAX_CLASSES,
|
||||
pkPartner,
|
||||
rankEntrants,
|
||||
standings,
|
||||
WEEK_MS,
|
||||
type Standing,
|
||||
@@ -107,7 +108,7 @@ rankingRoutes.get("/rankings/board", optionalAuth, async (c) => {
|
||||
const isTeacher = isTeacherOrAbove(user)
|
||||
|
||||
const [entrants, mine, hiddenUsers] = await Promise.all([
|
||||
loadEntrants(audienceWhere(scope.data, group), start),
|
||||
rankEntrants(scope.data, group, start),
|
||||
user && !isTeacher
|
||||
? db
|
||||
.select({ rankHiddenAt: schema.user.rankHiddenAt })
|
||||
|
||||
Reference in new issue
Block a user