An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat(cache): implement selective caching for individual chat loading only
Yoshiki Miura19596b0bbe79
diff --git a/lib/actions/chat.ts b/lib/actions/chat.tsindex 7b79a0f6..6d95a8fd 100644--- a/lib/actions/chat.ts+++ b/lib/actions/chat.ts@@ -1,6 +1,6 @@'use server'-import { revalidateTag } from 'next/cache'+import { revalidateTag, unstable_cache } from 'next/cache'import { generateChatTitle } from '@/lib/agents/title-generator'import { getCurrentUserId } from '@/lib/auth/get-current-user'@@ -13,6 +13,18 @@ import { getTextFromParts } from '@/lib/utils/message-utils'// Constantsconst DEFAULT_CHAT_TITLE = 'Untitled'+// Cache individual chat loading for 60 seconds+const getCachedChatWithMessages = unstable_cache(+ async (chatId: string, requestingUserId?: string) => {+ return dbActions.loadChatWithMessages(chatId, requestingUserId)+ },+ ['chat-with-messages'],+ {+ tags: (chatId) => [`chat-${chatId}`],+ revalidate: 60+ }+)+/*** Get all chats for the current user*/@@ -44,8 +56,8 @@ export async function loadChat(chatId: string,requestingUserId?: string): Promise<(Chat & { messages: UIMessage[] }) | null> {- // Use optimized function that loads both in parallel- return dbActions.loadChatWithMessages(chatId, requestingUserId)+ // Use cached version for individual chat loading+ return getCachedChatWithMessages(chatId, requestingUserId)}/**diff --git a/lib/streaming/helpers/persist-stream-results.ts b/lib/streaming/helpers/persist-stream-results.tsindex 392b31c9..d72f8a32 100644--- a/lib/streaming/helpers/persist-stream-results.ts+++ b/lib/streaming/helpers/persist-stream-results.ts@@ -1,3 +1,4 @@+import { revalidateTag } from 'next/cache'import { UIMessage } from 'ai'import { upsertMessage } from '@/lib/actions/chat'@@ -33,6 +34,8 @@ export async function persistStreamResults(try {await upsertMessage(chatId, responseMessage, userId)perfTime('upsertMessage (AI response) completed', saveStart)+ // Invalidate cache after saving message+ revalidateTag(`chat-${chatId}`)} catch (error) {console.error('Error saving message:', error)try {@@ -41,6 +44,8 @@ export async function persistStreamResults('save message')perfTime('upsertMessage (AI response) completed after retry', saveStart)+ // Invalidate cache after saving message+ revalidateTag(`chat-${chatId}`)} catch (retryError) {console.error('Failed to save after retries:', retryError)// Don't throw here to avoid breaking the stream@@ -51,6 +56,8 @@ export async function persistStreamResults(if (chatTitle && chatTitle !== DEFAULT_CHAT_TITLE) {try {await updateChatTitle(chatId, chatTitle, userId)+ // Invalidate cache after updating title+ revalidateTag(`chat-${chatId}`)} catch (error) {console.error('Error updating title:', error)// Don't throw here as title update is not critical