Skip to content

Commit 41d8512

Browse files
committed
チャットをキャッシュ
1 parent de28263 commit 41d8512

File tree

2 files changed

+57
-23
lines changed

2 files changed

+57
-23
lines changed

app/(docs)/@chat/chat/[chatId]/page.tsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
import { getChatOne, initContext } from "@/lib/chatHistory";
1+
import {
2+
cacheKeyForChat,
3+
cacheKeyForPage,
4+
ChatWithMessages,
5+
getChatOne,
6+
initContext,
7+
} from "@/lib/chatHistory";
28
import { getMarkdownSections, getPagesList } from "@/lib/docs";
39
import { ChatAreaContainer, ChatAreaContent } from "./chatArea";
10+
import { unstable_cacheLife, unstable_cacheTag } from "next/cache";
11+
import { isCloudflare } from "@/lib/detectCloudflare";
412

513
export default async function ChatPage({
614
params,
@@ -9,8 +17,8 @@ export default async function ChatPage({
917
}) {
1018
const { chatId } = await params;
1119

12-
const ctx = await initContext();
13-
const chatData = await getChatOne(chatId, ctx);
20+
const context = await initContext();
21+
const chatData = await getChatOneFromCache(chatId, context.userId);
1422

1523
if (!chatData) {
1624
// notFound(); だとページ全体が404になってしまう
@@ -43,3 +51,26 @@ export default async function ChatPage({
4351
</ChatAreaContainer>
4452
);
4553
}
54+
55+
async function getChatOneFromCache(chatId: string, userId?: string) {
56+
"use cache";
57+
unstable_cacheLife("days");
58+
unstable_cacheTag(cacheKeyForChat(chatId));
59+
60+
if (!userId) {
61+
return null;
62+
}
63+
64+
if (isCloudflare()) {
65+
const cache = await caches.open("chatHistory");
66+
const cachedResponse = await cache.match(cacheKeyForChat(chatId));
67+
if (cachedResponse) {
68+
const data = (await cachedResponse.json()) as ChatWithMessages;
69+
return data;
70+
}
71+
}
72+
73+
const context = await initContext({ userId });
74+
const chatData = await getChatOne(chatId, context);
75+
return chatData;
76+
}

app/lib/chatHistory.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@ const CACHE_KEY_BASE = "https://my-code.utcode.net/chatHistory";
2424
export function cacheKeyForPage(path: PagePath, userId: string) {
2525
return `${CACHE_KEY_BASE}/getChat?path=${path.lang}/${path.page}&userId=${userId}`;
2626
}
27+
export function cacheKeyForChat(chatId: string) {
28+
return `${CACHE_KEY_BASE}/getChatOne?chatId=${chatId}`;
29+
}
30+
31+
async function revalidateChat(
32+
chatId: string,
33+
userId: string,
34+
pagePath: string | PagePath
35+
) {
36+
if (typeof pagePath === "string") {
37+
const [lang, page] = pagePath.split("/") as [LangId, PageSlug];
38+
pagePath = { lang, page };
39+
}
40+
revalidateTag(cacheKeyForChat(chatId));
41+
revalidateTag(cacheKeyForPage(pagePath, userId));
42+
if (isCloudflare()) {
43+
const cache = await caches.open("chatHistory");
44+
await cache.delete(cacheKeyForChat(chatId));
45+
await cache.delete(cacheKeyForPage(pagePath, userId));
46+
}
47+
}
2748

2849
interface Context {
2950
drizzle: Awaited<ReturnType<typeof getDrizzle>>;
@@ -105,14 +126,7 @@ export async function addChat(
105126
chatDiffs = [] as never[];
106127
}
107128

108-
revalidateTag(cacheKeyForPage(path, userId));
109-
if (isCloudflare()) {
110-
const cache = await caches.open("chatHistory");
111-
console.log(
112-
`deleting cache for chatHistory/getChat for user ${userId} and docs ${path.lang}/${path.page}`
113-
);
114-
await cache.delete(cacheKeyForPage(path, userId));
115-
}
129+
await revalidateChat(newChat.chatId, userId, path);
116130

117131
return {
118132
...newChat,
@@ -145,18 +159,7 @@ export async function deleteChat(chatId: string, context: Context) {
145159
const targetSection = await drizzle.query.section.findFirst({
146160
where: eq(section.sectionId, deletedChat[0].sectionId),
147161
});
148-
const [lang, page] = (targetSection?.pagePath.split("/") ?? []) as [
149-
LangId,
150-
PageSlug,
151-
];
152-
revalidateTag(cacheKeyForPage({ lang, page }, userId));
153-
if (isCloudflare()) {
154-
const cache = await caches.open("chatHistory");
155-
console.log(
156-
`deleting cache for chatHistory/getChat for user ${userId} and docs ${lang}/${page}`
157-
);
158-
await cache.delete(cacheKeyForPage({ lang, page }, userId));
159-
}
162+
await revalidateChat(chatId, userId, targetSection?.pagePath ?? "");
160163
}
161164

162165
export async function getAllChat(

0 commit comments

Comments
 (0)