courrier multi account restore compose

This commit is contained in:
alma 2025-04-28 13:16:43 +02:00
parent a0790adf18
commit 5d284682a7
3 changed files with 23 additions and 9 deletions

View File

@ -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 // Try to get from Redis cache first, but only if it's not a search query
if (!searchQuery) { if (!searchQuery) {
const cacheKey = accountId ? `${session.user.id}:${accountId}:${folder}` : `${session.user.id}:${folder}`; 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) { if (cachedEmails) {
console.log(`Using Redis cached emails for ${cacheKey}:${page}:${perPage}`); console.log(`Using Redis cached emails for ${cacheKey}:${page}:${perPage}`);
return NextResponse.json(cachedEmails); return NextResponse.json(cachedEmails);
@ -85,12 +91,12 @@ export async function POST(request: Request) {
// Invalidate Redis cache for the folder // Invalidate Redis cache for the folder
if (folderName) { if (folderName) {
await invalidateFolderCache(session.user.id, folderName); await invalidateFolderCache(session.user.id, 'default', folderName);
} else { } else {
// If no folder specified, invalidate all folders (using a wildcard pattern) // If no folder specified, invalidate all folders (using a wildcard pattern)
const folders = ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']; const folders = ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk'];
for (const folder of folders) { for (const folder of folders) {
await invalidateFolderCache(session.user.id, folder); await invalidateFolderCache(session.user.id, 'default', folder);
} }
} }

View File

@ -99,12 +99,19 @@ export const useCourrier = () => {
} }
// Add accountId if provided // Add accountId if provided
if (accountId) { if (accountId && accountId !== 'all-accounts') {
queryParams.set('accountId', accountId); queryParams.set('accountId', accountId);
} }
// First try Redis cache with low timeout // 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) { if (cachedEmails) {
// Ensure cached data has emails array property // Ensure cached data has emails array property
if (Array.isArray(cachedEmails.emails)) { if (Array.isArray(cachedEmails.emails)) {

View File

@ -59,19 +59,20 @@ export async function getCachedEmailsWithTimeout(
folder: string, folder: string,
page: number, page: number,
perPage: number, perPage: number,
timeoutMs: number = 100 timeoutMs: number = 100,
accountId?: string
): Promise<any | null> { ): Promise<any | null> {
return new Promise((resolve) => { return new Promise((resolve) => {
const timeoutId = setTimeout(() => { 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); resolve(null);
}, timeoutMs); }, timeoutMs);
getCachedEmailList(userId, folder, page, perPage) getCachedEmailList(userId, accountId || 'default', folder, page, perPage)
.then(result => { .then(result => {
clearTimeout(timeoutId); clearTimeout(timeoutId);
if (result) { 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 // Validate and normalize the data structure
if (typeof result === 'object') { if (typeof result === 'object') {