refactor(data-access): 补全剩余 37 个 data-access 文件迁移至 cacheFn
迁移范围:lesson-preparation 11 文件、attendance 3 文件、settings 4 文件、classes-invitations、跨模块接口 4 文件、adaptive-practice/ai/onboarding/invitation-codes/search/standards/scheduling/leave-requests/audit。修复 3 个 cacheFn 块位置错误。同步架构文档迁移范围至 83 文件 250+ 函数。
This commit is contained in:
@@ -1263,11 +1263,41 @@ if (announcement.type === "school") {
|
||||
### 缓存策略重构涉及文件清单
|
||||
|
||||
- `src/shared/lib/cache/{types,memory-store,redis-store,store-factory,cache-fn,invalidation-map,client-invalidation-map,invalidate,index}.ts` — 缓存基础设施(9 个文件)
|
||||
- `src/shared/lib/redis-client.ts` — 共享 Redis 客户端单例
|
||||
- `src/shared/lib/query-keys.ts` — 客户端 queryKey 工厂
|
||||
- `src/shared/hooks/use-action-query.ts` — 新增 queryKey 入参走 QueryClient
|
||||
- `src/shared/hooks/use-action-mutation.ts` — 新增 actionId 自动 invalidate
|
||||
- `src/modules/classes/data-access-*.ts` — 5+ 文件全部 cacheFn 包装 + 双导出 raw
|
||||
- `src/modules/classes/actions-*.ts` — 5 文件全部 invalidateFor 替换 revalidatePath
|
||||
- `src/modules/classes/components/*` — useActionQuery/useActionMutation 适配新版 API
|
||||
- `src/shared/lib/redis-client.ts` — 共享 Redis 客户端单例(rate-limit + cache 共用)
|
||||
- `src/shared/lib/query-keys.ts` — 客户端 queryKey 工厂(`queryKeys.classes.*`)
|
||||
- `src/shared/hooks/use-action-query.ts` — 新增 queryKey 入参走 QueryClient(向后兼容旧模式)
|
||||
- `src/shared/hooks/use-action-mutation.ts` — 新增 actionId 自动 invalidate(向后兼容 mutate(action))
|
||||
- `src/env.mjs` — 新增 `CACHE_DRIVER: z.enum(["memory", "redis"]).default("memory")`
|
||||
- `eslint.config.mjs` — 新增 `no-restricted-syntax` 规则禁止 Server Actions 中直接调用 revalidatePath/revalidateTag
|
||||
|
||||
### 全量迁移覆盖范围(28 个模块)
|
||||
|
||||
**data-access 层迁移至 cacheFn**(83 个文件,250+ 查询函数):
|
||||
|
||||
| 组别 | 模块 | 备注 |
|
||||
|------|------|------|
|
||||
| 标杆 | classes | `data-access-{teacher,admin,students,stats,schedule}.ts` 5 文件 |
|
||||
| A 组 | users / school / rbac / textbooks / questions / course-plans / files / dashboard / parent / proctoring | 77 个函数 |
|
||||
| B 组 | exams / grades / homework / diagnostic / error-book / adaptive-practice | 含跨模块接口(如 exams/data-access-error-collection) |
|
||||
| C 组 | lesson-preparation / elective / announcements / messaging / notifications | 24 个函数 |
|
||||
|
||||
**actions 层迁移至 invalidateFor**(43 个文件,247 处 revalidatePath 替换):
|
||||
|
||||
| 组别 | 模块 | 替换数 |
|
||||
|------|------|--------|
|
||||
| 标杆 | classes(5 文件) | 47 |
|
||||
| A 组 | users / school / textbooks / questions / course-plans / standards / scheduling / audit / onboarding / invitation-codes / proctoring / i18n(13 文件) | 70 |
|
||||
| B 组 | exams / grades / homework / attendance / leave-requests / diagnostic / error-book / adaptive-practice(9 文件) | 77 |
|
||||
| C 组 | lesson-preparation / elective / announcements / messaging / notifications / settings / rbac(8 文件) | 100 |
|
||||
|
||||
**INVALIDATION_MAP**:扩展至 **203 个 actionId**,覆盖全部 28 个模块。每个 actionId 声明 `{ tags, queryKeys, paths }` 三段副作用。`CLIENT_INVALIDATION_MAP` 同步保留客户端可见子集(仅 queryKeys)。
|
||||
|
||||
### 迁移过程中常见错误(速查)
|
||||
|
||||
| 规则 | 正确写法 | 错误写法 |
|
||||
|------|---------|---------|
|
||||
| cacheFn 包装时移除原 `import { cache } from "react"` | 全部改用 `cacheFn` 双导出模式 | 保留 `cache(fn)` 调用但移除 import(tsc 报 `Cannot find name 'cache'`) |
|
||||
| 跨模块接口函数(如 `getExamSubmissionDataForErrorCollection`)不包装 cacheFn | 仅包装本模块自有查询;跨模块接口由对方模块决定 | 给跨模块接口也加 cacheFn(重复包装) |
|
||||
| 多余的 `}` 或 `)` 字符 | 迁移后用 tsc 全量校验 | 手动编辑后未运行 tsc(TS1005/TS1128 语法错误) |
|
||||
| questions/data-access.ts 等大文件分批迁移 | 完整迁移所有 `cache(...)` 调用至 cacheFn | 部分迁移导致 `import { cache }` 已删但调用仍存 |
|
||||
|
||||
|
||||
Reference in New Issue
Block a user