实战案例:博客编辑的完整缓存策略(续)
// app/posts/[id]/edit/page.tsx — 编辑页
import { updatePost } from "@/app/actions";
export default async function EditPage({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params;
return (
<form action={updatePost.bind(null, id)}>
<input name="title" />
<textarea name="content" />
<button type="submit">保存</button>
</form>
);
}
sequenceDiagram
participant User as 用户
participant List as 列表页
participant Edit as 编辑页
participant Action as Server Action
participant Cache as 四层缓存
User->>List: 1. 访问列表页
User->>Edit: 2. 点击编辑
User->>Action: 3. 提交表单
Action->>Action: 更新数据库
Action->>Cache: updateTag('posts') + updateTag('post-123')
Note over Cache: 第2层数据缓存失效<br/>第3层全路由缓存失效<br/>第4层路由器缓存失效
Action->>User: redirect 到详情页
User->>User: ✅ 立刻看到新数据
User->>List: 4. 返回列表页
List->>User: ✅ 显示新数据(缓存已全部失效)