Warm redis store once per client session

This commit is contained in:
Sam Becker 2025-08-28 09:20:10 -05:00
parent f7de56a7b9
commit 200f73a5ca
4 changed files with 21 additions and 10 deletions

View File

@ -15,6 +15,7 @@ import useSWR, { useSWRConfig } from 'swr';
import {
HIGH_DENSITY_GRID,
IS_DEVELOPMENT,
IS_PRODUCTION,
MATTE_PHOTOS,
SHOW_ZOOM_CONTROLS,
} from '@/app/config';
@ -39,6 +40,7 @@ import {
SWR_KEYS,
SWRKey,
} from '@/swr';
import { warmRedisAction } from './actions';
export default function AppStateProvider({
children,
@ -121,6 +123,10 @@ export default function AppStateProvider({
setHasLoaded(true);
storeTimezoneCookie();
setUserEmailEager(getAuthEmailCookie());
if (IS_PRODUCTION) {
console.log('Warming redis');
warmRedisAction();
}
const timeout = setTimeout(() => {
setHasLoadedWithAnimations(true);
}, 1000);

5
src/app/actions.ts Normal file
View File

@ -0,0 +1,5 @@
'use server';
import { warmRedisConnection } from '@/platforms/redis';
export const warmRedisAction = async () => warmRedisConnection();

View File

@ -1,17 +1,11 @@
import { generateText, streamText } from 'ai';
import { createStreamableValue } from '@ai-sdk/rsc';
import { createOpenAI } from '@ai-sdk/openai';
import { Redis } from '@upstash/redis';
import { Ratelimit } from '@upstash/ratelimit';
import {
AI_CONTENT_GENERATION_ENABLED,
HAS_REDIS_STORAGE,
OPENAI_BASE_URL,
} from '@/app/config';
import { AI_CONTENT_GENERATION_ENABLED, OPENAI_BASE_URL } from '@/app/config';
import { removeBase64Prefix } from '@/utility/image';
import { cleanUpAiTextResponse } from '@/photo/ai';
const redis = HAS_REDIS_STORAGE ? Redis.fromEnv() : undefined;
import { redis } from '@/platforms/redis';
const RATE_LIMIT_IDENTIFIER = 'openai-image-query';
const MODEL = 'gpt-4o';

View File

@ -1,8 +1,14 @@
import { Redis } from '@upstash/redis';
import { HAS_REDIS_STORAGE } from '@/app/config';
const redis = HAS_REDIS_STORAGE ? Redis.fromEnv() : undefined;
const KEY_TEST = 'test';
export const redis = HAS_REDIS_STORAGE ? Redis.fromEnv() : undefined;
export const warmRedisConnection = () => {
if (redis) { redis.get(KEY_TEST); }
};
export const testRedisConnection = () => redis
? redis.get('test')
? redis.get(KEY_TEST)
: Promise.reject(false);