diff --git a/app/api/courrier/route.ts b/app/api/courrier/route.ts index 9c20a1c2..ca640004 100644 --- a/app/api/courrier/route.ts +++ b/app/api/courrier/route.ts @@ -40,7 +40,13 @@ export async function GET(request: Request) { // Try to get from Redis cache first, but only if it's not a search query if (!searchQuery) { const cacheKey = accountId ? `${session.user.id}:${accountId}:${folder}` : `${session.user.id}:${folder}`; - const cachedEmails = await getCachedEmailList(session.user.id, folder, page, perPage); + const cachedEmails = await getCachedEmailList( + session.user.id, + accountId || 'default', + folder, + page, + perPage + ); if (cachedEmails) { console.log(`Using Redis cached emails for ${cacheKey}:${page}:${perPage}`); return NextResponse.json(cachedEmails); @@ -85,12 +91,12 @@ export async function POST(request: Request) { // Invalidate Redis cache for the folder if (folderName) { - await invalidateFolderCache(session.user.id, folderName); + await invalidateFolderCache(session.user.id, 'default', folderName); } else { // If no folder specified, invalidate all folders (using a wildcard pattern) const folders = ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']; for (const folder of folders) { - await invalidateFolderCache(session.user.id, folder); + await invalidateFolderCache(session.user.id, 'default', folder); } } diff --git a/hooks/use-courrier.ts b/hooks/use-courrier.ts index 42515a6c..0eb86f84 100644 --- a/hooks/use-courrier.ts +++ b/hooks/use-courrier.ts @@ -99,12 +99,19 @@ export const useCourrier = () => { } // Add accountId if provided - if (accountId) { + if (accountId && accountId !== 'all-accounts') { queryParams.set('accountId', accountId); } // First try Redis cache with low timeout - const cachedEmails = await getCachedEmailsWithTimeout(session.user.id, currentFolder, currentRequestPage, perPage, 100); + const cachedEmails = await getCachedEmailsWithTimeout( + session.user.id, + currentFolder, + currentRequestPage, + perPage, + 100, + accountId && accountId !== 'all-accounts' ? accountId : undefined + ); if (cachedEmails) { // Ensure cached data has emails array property if (Array.isArray(cachedEmails.emails)) { diff --git a/lib/services/prefetch-service.ts b/lib/services/prefetch-service.ts index 362b84c4..172a1933 100644 --- a/lib/services/prefetch-service.ts +++ b/lib/services/prefetch-service.ts @@ -59,19 +59,20 @@ export async function getCachedEmailsWithTimeout( folder: string, page: number, perPage: number, - timeoutMs: number = 100 + timeoutMs: number = 100, + accountId?: string ): Promise { return new Promise((resolve) => { const timeoutId = setTimeout(() => { - console.log(`Cache access timeout for ${userId}:${folder}:${page}:${perPage}`); + console.log(`Cache access timeout for ${userId}:${folder}:${page}:${perPage}${accountId ? ` for account ${accountId}` : ''}`); resolve(null); }, timeoutMs); - getCachedEmailList(userId, folder, page, perPage) + getCachedEmailList(userId, accountId || 'default', folder, page, perPage) .then(result => { clearTimeout(timeoutId); if (result) { - console.log(`Using cached data for ${userId}:${folder}:${page}:${perPage}`); + console.log(`Using cached data for ${userId}:${folder}:${page}:${perPage}${accountId ? ` for account ${accountId}` : ''}`); // Validate and normalize the data structure if (typeof result === 'object') {