Compare commits

...

2 Commits

Author SHA1 Message Date
arvinxx f44f351ed3 update 2025-08-10 19:09:01 +08:00
arvinxx 8b2964458c try redis for api performance 2025-08-10 18:30:17 +08:00
5 changed files with 27 additions and 2 deletions
+1
View File
@@ -166,6 +166,7 @@
"@trpc/next": "^11.4.4",
"@trpc/react-query": "^11.4.4",
"@trpc/server": "^11.4.4",
"@upstash/redis": "^1.35.3",
"@vercel/analytics": "^1.5.0",
"@vercel/edge-config": "^1.4.0",
"@vercel/functions": "^2.2.8",
+2
View File
@@ -0,0 +1,2 @@
export * from './type';
export * from './upstash';
+3
View File
@@ -0,0 +1,3 @@
import type { upstashCache } from 'drizzle-orm/cache/upstash';
export type DrizzleCache = ReturnType<typeof upstashCache> | undefined;
+14
View File
@@ -0,0 +1,14 @@
import { upstashCache } from 'drizzle-orm/cache/upstash';
/**
* Optional Upstash cache provider for Drizzle.
* Only enabled when UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN exist.
*/
export const getDrizzleCache = (): ReturnType<typeof upstashCache> | undefined => {
const url = process.env.UPSTASH_REDIS_REST_URL;
const token = process.env.UPSTASH_REDIS_REST_TOKEN;
if (!url || !token) return undefined;
return upstashCache({ token, url });
};
+7 -2
View File
@@ -9,6 +9,7 @@ import { isServerMode } from '@/const/version';
import * as schema from '@/database/schemas';
import { LobeChatDatabase } from '../type';
import { getDrizzleCache } from './cache';
export const getDBInstance = (): LobeChatDatabase => {
if (!isServerMode) return {} as any;
@@ -30,7 +31,9 @@ If you don't have it, please run \`openssl rand -base64 32\` to create one.
if (serverDBEnv.DATABASE_DRIVER === 'node') {
const client = new NodePool({ connectionString });
return nodeDrizzle(client, { schema });
const cache = getDrizzleCache();
const options = cache ? { cache, schema } : { schema };
return nodeDrizzle(client, options as any);
}
if (process.env.MIGRATION_DB === '1') {
@@ -39,5 +42,7 @@ If you don't have it, please run \`openssl rand -base64 32\` to create one.
}
const client = new NeonPool({ connectionString });
return neonDrizzle(client, { schema });
const cache = getDrizzleCache();
const options = cache ? { cache, schema } : { schema };
return neonDrizzle(client, options as any);
};