完善解决编译时初始化连接问题
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
import 'server-only'
|
||||
import { createClient } from 'redis'
|
||||
import { createClient, type RedisClientType } from 'redis'
|
||||
|
||||
declare global {
|
||||
var redis: ReturnType<typeof createClient> | undefined
|
||||
const globalForRedis = globalThis as { redis?: RedisClientType }
|
||||
|
||||
if (!globalForRedis.redis) {
|
||||
globalForRedis.redis = createClient({ url: process.env.REDIS_URL })
|
||||
}
|
||||
|
||||
const client = global.redis || createClient({ url: process.env.REDIS_URL })
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
global.redis = client
|
||||
const redis = globalForRedis.redis
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
await redis.connect()
|
||||
}
|
||||
|
||||
const redis = await client.connect()
|
||||
export default redis
|
||||
|
||||
Reference in New Issue
Block a user