courrier redis

This commit is contained in:
alma 2025-04-27 14:10:11 +02:00
parent 0455ff7f7d
commit b68993d68d
3 changed files with 41 additions and 0 deletions

View File

@ -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,
},
};
}

View File

@ -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,

View File

@ -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;
}