From b68993d68d058332bdd962b71e26436702db7fb2 Mon Sep 17 00:00:00 2001 From: alma Date: Sun, 27 Apr 2025 14:10:11 +0200 Subject: [PATCH] courrier redis --- app/courrier/page.tsx | 21 +++++++++++++++++++++ hooks/use-courrier.ts | 12 ++++++++++++ lib/services/prefetch-service.ts | 8 ++++++++ 3 files changed, 41 insertions(+) diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 384e17e3..603bbf0f 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -499,4 +499,25 @@ export default function CourrierPage() { /> ); +} + +export async function getServerSideProps(context) { + // Initialize Redis connection early + const redisClient = getRedisClient(); + const session = await getServerSession(context.req, context.res, authOptions); + + let initialData = null; + if (session?.user?.id) { + // Try to get cached data + initialData = await getCachedEmailList(session.user.id, 'INBOX', 1, 20); + + // Start prefetch in background without waiting + prefetchUserEmailData(session.user.id).catch(console.error); + } + + return { + props: { + initialData: initialData || null, + }, + }; } \ No newline at end of file diff --git a/hooks/use-courrier.ts b/hooks/use-courrier.ts index 0ca12a97..f34080ce 100644 --- a/hooks/use-courrier.ts +++ b/hooks/use-courrier.ts @@ -2,6 +2,7 @@ import { useState, useCallback, useEffect } from 'react'; import { useSession } from 'next-auth/react'; import { useToast } from './use-toast'; import { formatEmailForReplyOrForward } from '@/lib/utils/email-formatter'; +import { getCachedEmailsWithTimeout, refreshEmailsInBackground } from '@/lib/services/prefetch-service'; export interface EmailAddress { name: string; @@ -90,6 +91,17 @@ export const useCourrier = () => { setError(null); try { + // First try Redis cache with low timeout + const cachedEmails = await getCachedEmailsWithTimeout(session.user.id, currentFolder, page, perPage, 100); + if (cachedEmails) { + setEmails(cachedEmails); + setIsLoading(false); + + // Still refresh in background for fresh data + refreshEmailsInBackground(); + return; + } + // Build query params const queryParams = new URLSearchParams({ folder: currentFolder, diff --git a/lib/services/prefetch-service.ts b/lib/services/prefetch-service.ts index 36657c5d..71d6032f 100644 --- a/lib/services/prefetch-service.ts +++ b/lib/services/prefetch-service.ts @@ -108,4 +108,12 @@ export async function prefetchFolderEmails( } catch (error) { console.error(`Error prefetching folder ${folder}:`, error); } +} + +export async function warmupRedisCache() { + // Ping Redis to establish connection early + const redis = getRedisClient(); + await redis.ping(); + console.log('Redis connection warmed up'); + return true; } \ No newline at end of file