ai-wpa/lib/redis.ts

50 lines
1.1 KiB
TypeScript

import { createClient } from 'redis'
// Only create Redis client if REDIS_URL is provided
let client: ReturnType<typeof createClient> | null = null
if (process.env.REDIS_URL) {
client = createClient({
url: process.env.REDIS_URL,
})
client.on('error', (err) => {
console.error('Redis Client Error:', err)
})
client.on('connect', () => {
console.log('Connected to Redis')
})
client.on('ready', () => {
console.log('Redis Client Ready')
})
client.on('end', () => {
console.log('Redis Client Disconnected')
})
} else {
console.warn('Redis disabled - REDIS_URL not provided')
}
// Connect to Redis
const connectRedis = async () => {
if (!client) {
console.warn('Redis client not initialized - sessions will use memory store')
return null
}
if (!client.isOpen) {
try {
await client.connect()
} catch (error) {
console.error('Failed to connect to Redis:', error)
// Don't throw error - let app continue without Redis
return null
}
}
return client
}
export { client as redisClient, connectRedis }