21 lines
637 B
TypeScript
21 lines
637 B
TypeScript
import 'server-only'
|
|
import { createClient, type RedisClientType } from 'redis'
|
|
|
|
const globalForRedis = globalThis as { redis?: RedisClientType }
|
|
|
|
const { REDIS_HOST, REDIS_PORT, REDIS_USERNAME, REDIS_PASSWORD } = process.env
|
|
if (!globalForRedis.redis) {
|
|
const url = (REDIS_USERNAME || REDIS_PASSWORD)
|
|
? `redis://${REDIS_USERNAME}:${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT}`
|
|
: `redis://${REDIS_HOST}:${REDIS_PORT}`
|
|
console.log('test url', url)
|
|
globalForRedis.redis = createClient({ url })
|
|
}
|
|
|
|
const redis = globalForRedis.redis
|
|
if (process.env.NODE_ENV === 'production') {
|
|
await redis.connect()
|
|
}
|
|
|
|
export default redis
|